Skip to content

Commit

Permalink
fix(nextjs): Improve page detection for Next 15 (#4383)
Browse files Browse the repository at this point in the history
  • Loading branch information
panteliselef authored Oct 22, 2024
1 parent 5853261 commit 58c99a5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/fluffy-eyes-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clerk/nextjs": minor
---

Bug fix: Correctly redirect to sign in page in Next 15.
7 changes: 7 additions & 0 deletions packages/nextjs/src/server/nextFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 13 additions & 1 deletion packages/nextjs/src/server/protect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 58c99a5

Please sign in to comment.