Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(nextjs): Decrease frequency of logs related to Keyless #4953

Merged
merged 10 commits into from
Jan 21, 2025
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.
9 changes: 9 additions & 0 deletions packages/nextjs/src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,12 @@ interface Window {

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

// declare module global {
// // eslint-disable-next-line no-var
// var logger: {
// loggedMessages: Set<string>;
// logOnce: (msg: string) => void;
// warnOnce: (msg: string) => void;
// };
// }
47 changes: 43 additions & 4 deletions packages/nextjs/src/server/keyless-node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
import type { AccountlessApplication } from '@clerk/backend';
import { logger } from '@clerk/shared/logger';

import { unstable_cache } from 'next/cache';

// if (!global.logger) {
// global.logger = {
// loggedMessages: new Set<string>(),
// /**
// * A custom logger that ensures messages are logged only once.
// * Reduces noise and duplicated messages when logs are in a hot codepath.
// */
// warnOnce: function (msg: string) {
// if (this.loggedMessages.has(msg)) {
// return;
// }
//
// this.loggedMessages.add(msg);
// console.warn(msg);
// },
// logOnce: function (msg: string) {
// console.log([...this.loggedMessages.entries()]);
// if (this.loggedMessages.has(msg)) {
// return;
// }
//
// console.log(msg);
// this.loggedMessages.add(msg);
// console.log('--------- Adding to cache');
// },
// };
// }
/**
* Attention: Only import this module when the node runtime is used.
* We are using conditional imports to mitigate bundling issues with Next.js server actions on version prior to 14.1.0.
Expand Down Expand Up @@ -87,6 +114,16 @@ 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`;
};

const notifyOnce = unstable_cache(
(keys: AccountlessApplication) => {
console.log(createMessage(keys));
return Promise.resolve();
},
['keyless-notification'],
// 10 minutes in seconds
{ revalidate: 10 * 60 },
);

async function createOrReadKeyless(): Promise<AccountlessApplication | undefined> {
if (!nodeRuntime.fs) {
// This should never happen.
Expand Down Expand Up @@ -132,7 +169,8 @@ async function createOrReadKeyless(): Promise<AccountlessApplication | undefined
/**
* Notify developers.
*/
logger.logOnce(createMessage(envVarsMap));
// global.logger.logOnce(createMessage(envVarsMap));
await notifyOnce(envVarsMap);

return envVarsMap;
}
Expand All @@ -146,7 +184,8 @@ async function createOrReadKeyless(): Promise<AccountlessApplication | undefined
/**
* Notify developers.
*/
logger.logOnce(createMessage(accountlessApplication));
// global.logger.logOnce(createMessage(accountlessApplication));
await notifyOnce(accountlessApplication);

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