Skip to content

Commit

Permalink
fix(nextjs): Avoid calling safeParseClerkFile before checking if ke…
Browse files Browse the repository at this point in the history
…yless is allowed (#4981)
  • Loading branch information
panteliselef authored Jan 23, 2025
1 parent 4f36624 commit 9867924
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/weak-monkeys-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/nextjs': patch
---

Avoid calling `safeParseClerkFile` before checking if keyless is allowed.
13 changes: 10 additions & 3 deletions packages/nextjs/src/app-router/server/ClerkProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import React from 'react';

import { PromisifiedAuthProvider } from '../../client-boundary/PromisifiedAuthProvider';
import { getDynamicAuthData } from '../../server/buildClerkProps';
import { safeParseClerkFile } from '../../server/keyless-node';
import type { NextClerkProviderProps } from '../../types';
import { canUseKeyless } from '../../utils/feature-flags';
import { mergeNextClerkPropsWithEnv } from '../../utils/mergeNextClerkPropsWithEnv';
Expand Down Expand Up @@ -80,8 +79,16 @@ export async function ClerkProvider(
</ClientClerkProvider>
);

const runningWithClaimedKeys = propsWithEnvs.publishableKey === safeParseClerkFile()?.publishableKey;
const shouldRunAsKeyless = (!propsWithEnvs.publishableKey || runningWithClaimedKeys) && canUseKeyless;
let [shouldRunAsKeyless, runningWithClaimedKeys] = [false, false];
if (canUseKeyless) {
const locallyStorePublishableKey = await import('../../server/keyless-node.js')
.then(mod => mod.safeParseClerkFile()?.publishableKey)
.catch(() => undefined);

runningWithClaimedKeys =
Boolean(propsWithEnvs.publishableKey) && propsWithEnvs.publishableKey === locallyStorePublishableKey;
shouldRunAsKeyless = !propsWithEnvs.publishableKey || runningWithClaimedKeys;
}

if (shouldRunAsKeyless) {
// NOTE: Create or read keys on every render. Usually this means only on hard refresh or hard navigations.
Expand Down

0 comments on commit 9867924

Please sign in to comment.