Skip to content

Commit 554a192

Browse files
committed
fix(shared): detect server runtime without reading process.versions
The Next.js Edge Runtime build-time analyzer flags any reachable read of process.versions even when guarded — this caused a warning when @clerk/backend was reached through middleware, which the next-build integration test asserts against. Detect Edge Runtime via its standard `EdgeRuntime` global instead, and use the absence of `window`+`EdgeRuntime` as the positive signal for "server runtime" without touching process.versions at all.
1 parent 2e0d12a commit 554a192

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

packages/shared/src/__tests__/telemetry.notice.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@ describe('maybeShowTelemetryNotice', () => {
105105
}
106106
});
107107

108+
test('skips in Next.js Edge Runtime', () => {
109+
(globalThis as { EdgeRuntime?: string }).EdgeRuntime = 'edge-runtime';
110+
111+
try {
112+
maybeShowTelemetryNotice();
113+
expect(logSpy).not.toHaveBeenCalled();
114+
} finally {
115+
delete (globalThis as { EdgeRuntime?: string }).EdgeRuntime;
116+
}
117+
});
118+
108119
test('does not throw if console.log fails', () => {
109120
logSpy.mockImplementation(() => {
110121
throw new Error('console broken');

packages/shared/src/telemetry/notice.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,14 @@ const CI_ENV_VARS = [
4040
'CODEBUILD_BUILD_ID',
4141
];
4242

43-
function isNodeRuntime(): boolean {
44-
return typeof window === 'undefined' && typeof process !== 'undefined' && Boolean(process.versions?.node);
43+
function isServerRuntime(): boolean {
44+
// Skip in browsers.
45+
if (typeof window !== 'undefined') return false;
46+
// Skip in Next.js Edge Runtime, which exposes a global `EdgeRuntime` marker. We detect via
47+
// this marker (rather than checking `process.versions`) because the Edge Runtime build-time
48+
// analyzer flags any reachable read of `process.versions` even when it sits behind a guard.
49+
if (typeof (globalThis as { EdgeRuntime?: string }).EdgeRuntime !== 'undefined') return false;
50+
return true;
4551
}
4652

4753
function isCI(): boolean {
@@ -79,15 +85,15 @@ export type MaybeShowTelemetryNoticeOptions = {
7985
};
8086

8187
/**
82-
* Display the one-time telemetry disclosure on Node if it has not already been shown
83-
* in this process. Browser callers are silently skipped. Never throws.
88+
* Display the one-time telemetry disclosure on server runtimes if it has not already been
89+
* shown in this process. Browser and Edge Runtime callers are silently skipped. Never throws.
8490
*/
8591
export function maybeShowTelemetryNotice(options: MaybeShowTelemetryNoticeOptions = {}): void {
8692
if (options.skip) {
8793
return;
8894
}
8995
try {
90-
if (!isNodeRuntime()) {
96+
if (!isServerRuntime()) {
9197
return;
9298
}
9399
if (isCI()) {

0 commit comments

Comments
 (0)