Skip to content

Commit

Permalink
Fix trailing slash redirect to external domain (#358)
Browse files Browse the repository at this point in the history
* fix trailing slash redirect to external domain

* changeset
  • Loading branch information
conico974 authored Feb 1, 2024
1 parent 186e28f commit c80f1be
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .changeset/tame-hotels-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"open-next": patch
"tests-e2e": patch
---

Fix trailing slash redirect to external domain
20 changes: 15 additions & 5 deletions packages/open-next/src/adapters/routing/matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,13 @@ export function handleRewrites<T extends RewriteDefinition>(
};
}

export function handleRedirects(
event: InternalEvent,
redirects: RedirectDefinition[],
): InternalResult | undefined {
function handleTrailingSlashRedirect(event: InternalEvent) {
if (!NextConfig.skipTrailingSlashRedirect) {
const url = new URL(event.url, "http://localhost");
// Someone is trying to redirect to a different origin, let's not do that
if (url.host !== "localhost") {
return false;
}
if (
NextConfig.trailingSlash &&
!event.headers["x-nextjs-data"] &&
Expand Down Expand Up @@ -211,7 +213,15 @@ export function handleRedirects(
isBase64Encoded: false,
};
}
}
} else return false;
}

export function handleRedirects(
event: InternalEvent,
redirects: RedirectDefinition[],
): InternalResult | undefined {
const trailingSlashRedirect = handleTrailingSlashRedirect(event);
if (trailingSlashRedirect) return trailingSlashRedirect;
const { internalEvent, __rewrite } = handleRewrites(
event,
redirects.filter((r) => !r.internal),
Expand Down
5 changes: 5 additions & 0 deletions packages/tests-e2e/tests/appRouter/trailing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ test("trailingSlash redirect with search parameters", async ({ page }) => {
);
expect(response?.request().url()).toMatch(/\/ssr\?happy=true$/);
});

test("trailingSlash redirect to external domain", async ({ page, baseURL }) => {
const response = await page.goto(`${baseURL}//sst.dev/`);
expect(response?.status()).toBe(404);
});

0 comments on commit c80f1be

Please sign in to comment.