Skip to content

Commit

Permalink
chore(nextjs): Decrease frequency of logs related to Keyless (#4953)
Browse files Browse the repository at this point in the history
  • Loading branch information
panteliselef authored Jan 21, 2025
1 parent 3c0de05 commit 71e971a
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/eighty-clocks-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/nextjs': patch
---

Decrease frequency of logs related to Keyless.
10 changes: 10 additions & 0 deletions packages/nextjs/src/app-router/keyless-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ export async function createOrReadKeylessAction(): Promise<null | Omit<Accountle
return null;
}

const { keylessLogger, createKeylessModeMessage } = await import('../server/keyless-log-cache.js');

/**
* Notify developers.
*/
keylessLogger?.log({
cacheKey: result.publishableKey,
msg: createKeylessModeMessage(result),
});

const { claimUrl, publishableKey, secretKey, apiKeysUrl } = result;

void (await cookies()).set(getKeylessCookieName(), JSON.stringify({ claimUrl, publishableKey, secretKey }), {
Expand Down
22 changes: 21 additions & 1 deletion packages/nextjs/src/app-router/server/ClerkProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,11 @@ export async function ClerkProvider(
if (shouldRunAsKeyless) {
// NOTE: Create or read keys on every render. Usually this means only on hard refresh or hard navigations.
const newOrReadKeys = await import('../../server/keyless-node.js').then(mod => mod.createOrReadKeyless());
const { keylessLogger, createConfirmationMessage, createKeylessModeMessage } = await import(
'../../server/keyless-log-cache.js'
);

if (newOrReadKeys) {
const KeylessCookieSync = await import('../client/keyless-cookie-sync.js').then(mod => mod.KeylessCookieSync);
const clientProvider = (
<ClientClerkProvider
{...mergeNextClerkPropsWithEnv({
Expand All @@ -97,8 +99,26 @@ export async function ClerkProvider(
);

if (runningWithClaimedKeys) {
/**
* Notify developers.
*/
keylessLogger?.log({
cacheKey: `${newOrReadKeys.publishableKey}_claimed`,
msg: createConfirmationMessage(),
});

output = clientProvider;
} else {
const KeylessCookieSync = await import('../client/keyless-cookie-sync.js').then(mod => mod.KeylessCookieSync);

/**
* Notify developers.
*/
keylessLogger?.log({
cacheKey: newOrReadKeys.publishableKey,
msg: createKeylessModeMessage(newOrReadKeys),
});

output = <KeylessCookieSync {...newOrReadKeys}>{clientProvider}</KeylessCookieSync>;
}
}
Expand Down
10 changes: 10 additions & 0 deletions packages/nextjs/src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,13 @@ interface Window {

declare const PACKAGE_NAME: string;
declare const PACKAGE_VERSION: string;

declare module globalThis {
// eslint-disable-next-line no-var
var __clerk_internal_keyless_logger:
| {
__cache: Map<string, { expiresAt: number }>;
log: (param: { cacheKey: string; msg: string }) => void;
}
| undefined;
}
40 changes: 40 additions & 0 deletions packages/nextjs/src/server/keyless-log-cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { AccountlessApplication } from '@clerk/backend';
import { isDevelopmentEnvironment } from '@clerk/shared/utils';
// 10 minutes in milliseconds
const THROTTLE_DURATION_MS = 10 * 60 * 1000;

function createClerkDevLogger() {
if (!isDevelopmentEnvironment()) {
return;
}

if (!global.__clerk_internal_keyless_logger) {
global.__clerk_internal_keyless_logger = {
__cache: new Map<string, { expiresAt: number }>(),

log: function ({ cacheKey, msg }: { cacheKey: string; msg: string }) {
if (this.__cache.has(cacheKey) && Date.now() < (this.__cache.get(cacheKey)?.expiresAt || 0)) {
return;
}

console.log(msg);

this.__cache.set(cacheKey, {
expiresAt: Date.now() + THROTTLE_DURATION_MS,
});
},
};
}

return globalThis.__clerk_internal_keyless_logger;
}

export const createKeylessModeMessage = (keys: AccountlessApplication) => {
return `\n\x1b[35m\n[Clerk]:\x1b[0m You are running in keyless mode.\nYou can \x1b[35mclaim your keys\x1b[0m by visiting ${keys.claimUrl}\n`;
};

export const createConfirmationMessage = () => {
return `\n\x1b[35m\n[Clerk]:\x1b[0m Your application is running with your claimed keys.\nYou can safely remove the \x1b[35m.clerk/\x1b[0m from your project.\n`;
};

export const keylessLogger = createClerkDevLogger();
15 changes: 0 additions & 15 deletions packages/nextjs/src/server/keyless-node.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { AccountlessApplication } from '@clerk/backend';
import { logger } from '@clerk/shared/logger';

/**
* Attention: Only import this module when the node runtime is used.
Expand Down Expand Up @@ -87,10 +86,6 @@ export function safeParseClerkFile(): AccountlessApplication | undefined {
}
}

const createMessage = (keys: AccountlessApplication) => {
return `\n\x1b[35m\n[Clerk]:\x1b[0m You are running in keyless mode.\nYou can \x1b[35mclaim your keys\x1b[0m by visiting ${keys.claimUrl}\n`;
};

/**
* Using both an in-memory and file system lock seems to be the most effective solution.
*/
Expand Down Expand Up @@ -154,11 +149,6 @@ async function createOrReadKeyless(): Promise<AccountlessApplication | undefined
if (envVarsMap?.publishableKey && envVarsMap?.secretKey) {
unlockFileWriting();

/**
* Notify developers.
*/
logger.logOnce(createMessage(envVarsMap));

return envVarsMap;
}

Expand All @@ -168,11 +158,6 @@ async function createOrReadKeyless(): Promise<AccountlessApplication | undefined
const client = createClerkClientWithOptions({});
const accountlessApplication = await client.__experimental_accountlessApplications.createAccountlessApplication();

/**
* Notify developers.
*/
logger.logOnce(createMessage(accountlessApplication));

writeFileSync(CONFIG_PATH, JSON.stringify(accountlessApplication), {
encoding: 'utf8',
mode: '0777',
Expand Down

0 comments on commit 71e971a

Please sign in to comment.