Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TrailingSlash using rewrite for a fetch call results on infinite loop #280

Merged
merged 2 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/open-next/src/adapters/routing/matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,12 @@
!event.rawPath.endsWith("/") &&
!event.headers["x-nextjs-data"]
) {
const headersLocation = event.url.split('?');

Check failure on line 179 in packages/open-next/src/adapters/routing/matcher.ts

View workflow job for this annotation

GitHub Actions / validate

Replace `'?'` with `"?"`
return {
type: event.type,
statusCode: 308,
headers: {
Location: event.url + "/",
Location: `${headersLocation[0]}/${headersLocation[1] ? `?${headersLocation[1]}` : ''}`,

Check failure on line 184 in packages/open-next/src/adapters/routing/matcher.ts

View workflow job for this annotation

GitHub Actions / validate

Replace `headersLocation[1]·?·`?${headersLocation[1]}`·:·''` with `⏎··········headersLocation[1]·?·`?${headersLocation[1]}`·:·""⏎········`
},
body: "",
isBase64Encoded: false,
Expand All @@ -191,11 +192,12 @@
event.rawPath.endsWith("/") &&
event.rawPath !== "/"
) {
const headersLocation = event.url.split('?');

Check failure on line 195 in packages/open-next/src/adapters/routing/matcher.ts

View workflow job for this annotation

GitHub Actions / validate

Replace `'?'` with `"?"`
return {
type: event.type,
statusCode: 308,
headers: {
Location: event.url.replace(/\/$/, ""),
Location: `${headersLocation[0].replace(/\/$/, "")}${headersLocation[1] ? `?${headersLocation[1]}` : ''}`,

Check failure on line 200 in packages/open-next/src/adapters/routing/matcher.ts

View workflow job for this annotation

GitHub Actions / validate

Replace `headersLocation[1]·?·`?${headersLocation[1]}`·:·''` with `⏎··········headersLocation[1]·?·`?${headersLocation[1]}`·:·""⏎········`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the internalEvent.url in the final return below also split the ??

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I code-reviewed that part, and it is already handled by handleRewrites fn.
On handleRewrite, we are doing it like this:

const urlQueryString = new URLSearchParams(convertQuery(query)).toString();
const queryString = urlQueryString ? `?${urlQueryString}` : "";
let rewrittenUrl = rawPath;
url: `${rewrittenUrl}${queryString}`,

This should be working as expected because we do not add or remove any slash. On my changes, I've gone for split('?') because there are fewer lines of code. But happy to match the style. I'm not sure if there are some code style guidelines somewhere :D

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fine, doesn't have to match style.

},
body: "",
isBase64Encoded: false,
Expand Down
4 changes: 2 additions & 2 deletions packages/tests-e2e/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export APP_PAGES_ROUTER_URL=$(jq -r '.["e2e-example-AppPagesRouter"].url' .sst/o
```
3. Run the test
```bash
cd ../packages/tests-e2e
npm run e2e:dev
cd ../../packages/tests-e2e
pnpm run e2e:dev
```


Expand Down
15 changes: 15 additions & 0 deletions packages/tests-e2e/tests/appRouter/trailing.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { expect, test } from "@playwright/test";

test("trailingSlash redirect", async ({ page }) => {
const response = await page.goto("/ssr/");

expect(response?.request().redirectedFrom()?.url()).toMatch(/\/ssr\/$/);
expect(response?.request().url()).toMatch(/\/ssr$/);
});

test("trailingSlash redirect with search parameters", async ({ page }) => {
const response = await page.goto("/ssr/?happy=true");

expect(response?.request().redirectedFrom()?.url()).toMatch(/\/ssr\/\?happy=true$/);

Check failure on line 13 in packages/tests-e2e/tests/appRouter/trailing.test.ts

View workflow job for this annotation

GitHub Actions / validate

Replace `/\/ssr\/\?happy=true$/` with `⏎····/\/ssr\/\?happy=true$/,⏎··`
expect(response?.request().url()).toMatch(/\/ssr\?happy=true$/);
});
7 changes: 7 additions & 0 deletions packages/tests-e2e/tests/pagesRouter/trailing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@
expect(response?.request().redirectedFrom()?.url()).toMatch(/\/ssr$/);
expect(response?.request().url()).toMatch(/\/ssr\/$/);
});

test("trailingSlash redirect with search parameters", async ({ page }) => {
const response = await page.goto("/ssr?happy=true");

expect(response?.request().redirectedFrom()?.url()).toMatch(/\/ssr\?happy=true$/);

Check failure on line 13 in packages/tests-e2e/tests/pagesRouter/trailing.test.ts

View workflow job for this annotation

GitHub Actions / validate

Replace `/\/ssr\?happy=true$/` with `⏎····/\/ssr\?happy=true$/,⏎··`
expect(response?.request().url()).toMatch(/\/ssr\/\?happy=true$/);
});

Check failure on line 15 in packages/tests-e2e/tests/pagesRouter/trailing.test.ts

View workflow job for this annotation

GitHub Actions / validate

Insert `⏎`
Loading