From 58c99a50b1302bfb37dc5a6df9e8fbd54cc0c07d Mon Sep 17 00:00:00 2001 From: panteliselef Date: Wed, 23 Oct 2024 00:42:14 +0200 Subject: [PATCH] fix(nextjs): Improve page detection for Next 15 (#4383) --- .changeset/fluffy-eyes-arrive.md | 5 +++++ packages/nextjs/src/server/nextFetcher.ts | 7 +++++++ packages/nextjs/src/server/protect.ts | 14 +++++++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 .changeset/fluffy-eyes-arrive.md diff --git a/.changeset/fluffy-eyes-arrive.md b/.changeset/fluffy-eyes-arrive.md new file mode 100644 index 0000000000..240930360f --- /dev/null +++ b/.changeset/fluffy-eyes-arrive.md @@ -0,0 +1,5 @@ +--- +"@clerk/nextjs": minor +--- + +Bug fix: Correctly redirect to sign in page in Next 15. diff --git a/packages/nextjs/src/server/nextFetcher.ts b/packages/nextjs/src/server/nextFetcher.ts index 7a73ade19c..8ad2ef96bf 100644 --- a/packages/nextjs/src/server/nextFetcher.ts +++ b/packages/nextjs/src/server/nextFetcher.ts @@ -12,7 +12,14 @@ type NextFetcher = Fetcher & { * Full type can be found https://github.com/vercel/next.js/blob/6185444e0a944a82e7719ac37dad8becfed86acd/packages/next/src/client/components/static-generation-async-storage.external.ts#L4 */ interface StaticGenerationAsyncStorage { + /** + * Available for Next 14 + */ readonly pagePath?: string; + /** + * Available for Next 15 + */ + readonly page?: string; } function isNextFetcher(fetch: Fetcher | NextFetcher): fetch is NextFetcher { diff --git a/packages/nextjs/src/server/protect.ts b/packages/nextjs/src/server/protect.ts index 463dc8db3b..7e17e37817 100644 --- a/packages/nextjs/src/server/protect.ts +++ b/packages/nextjs/src/server/protect.ts @@ -134,7 +134,19 @@ const isAppRouterInternalNavigation = (req: Request) => const isPagePathAvailable = () => { const __fetch = globalThis.fetch; - return Boolean(isNextFetcher(__fetch) ? __fetch.__nextGetStaticStore().getStore()?.pagePath : false); + + if (!isNextFetcher(__fetch)) { + return false; + } + + const { page, pagePath } = __fetch.__nextGetStaticStore().getStore() || {}; + + return Boolean( + // available on next@14 + pagePath || + // available on next@15 + page, + ); }; const isPagesRouterInternalNavigation = (req: Request) => !!req.headers.get(nextConstants.Headers.NextjsData);