Skip to content

Commit 34c78cb

Browse files
authored
TrailingSlash using rewrite for a fetch call results on infinite loop (#280)
* TrailingSlash using rewrite for a fetch call results on infinite loop Fixes: #279
1 parent 9e9e135 commit 34c78cb

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

packages/open-next/src/adapters/routing/matcher.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,14 @@ export function handleRedirects(
176176
!event.rawPath.endsWith("/") &&
177177
!event.headers["x-nextjs-data"]
178178
) {
179+
const headersLocation = event.url.split("?");
179180
return {
180181
type: event.type,
181182
statusCode: 308,
182183
headers: {
183-
Location: event.url + "/",
184+
Location: `${headersLocation[0]}/${
185+
headersLocation[1] ? `?${headersLocation[1]}` : ""
186+
}`,
184187
},
185188
body: "",
186189
isBase64Encoded: false,
@@ -191,11 +194,14 @@ export function handleRedirects(
191194
event.rawPath.endsWith("/") &&
192195
event.rawPath !== "/"
193196
) {
197+
const headersLocation = event.url.split("?");
194198
return {
195199
type: event.type,
196200
statusCode: 308,
197201
headers: {
198-
Location: event.url.replace(/\/$/, ""),
202+
Location: `${headersLocation[0].replace(/\/$/, "")}${
203+
headersLocation[1] ? `?${headersLocation[1]}` : ""
204+
}`,
199205
},
200206
body: "",
201207
isBase64Encoded: false,

packages/tests-e2e/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ export APP_PAGES_ROUTER_URL=$(jq -r '.["e2e-example-AppPagesRouter"].url' .sst/o
3737
```
3838
3. Run the test
3939
```bash
40-
cd ../packages/tests-e2e
41-
npm run e2e:dev
40+
cd ../../packages/tests-e2e
41+
pnpm run e2e:dev
4242
```
4343

4444

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { expect, test } from "@playwright/test";
2+
3+
test("trailingSlash redirect", async ({ page }) => {
4+
const response = await page.goto("/ssr/");
5+
6+
expect(response?.request().redirectedFrom()?.url()).toMatch(/\/ssr\/$/);
7+
expect(response?.request().url()).toMatch(/\/ssr$/);
8+
});
9+
10+
test("trailingSlash redirect with search parameters", async ({ page }) => {
11+
const response = await page.goto("/ssr/?happy=true");
12+
13+
expect(response?.request().redirectedFrom()?.url()).toMatch(
14+
/\/ssr\/\?happy=true$/,
15+
);
16+
expect(response?.request().url()).toMatch(/\/ssr\?happy=true$/);
17+
});

packages/tests-e2e/tests/pagesRouter/trailing.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,12 @@ test("trailingSlash redirect", async ({ page }) => {
66
expect(response?.request().redirectedFrom()?.url()).toMatch(/\/ssr$/);
77
expect(response?.request().url()).toMatch(/\/ssr\/$/);
88
});
9+
10+
test("trailingSlash redirect with search parameters", async ({ page }) => {
11+
const response = await page.goto("/ssr?happy=true");
12+
13+
expect(response?.request().redirectedFrom()?.url()).toMatch(
14+
/\/ssr\?happy=true$/,
15+
);
16+
expect(response?.request().url()).toMatch(/\/ssr\/\?happy=true$/);
17+
});

0 commit comments

Comments
 (0)