From 6e27ce9ac3a5a596b84ceac1964cf106117db034 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Tue, 20 Dec 2022 13:56:50 +0100 Subject: [PATCH] feat(nextjs): Check for Vercel Edge Function GA (#6565) --- .../templates/apiProxyLoaderTemplate.ts | 20 +++++++++++++------ packages/nextjs/src/index.client.ts | 1 + packages/nextjs/src/index.server.ts | 1 + 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/packages/nextjs/src/config/templates/apiProxyLoaderTemplate.ts b/packages/nextjs/src/config/templates/apiProxyLoaderTemplate.ts index 6aa0782f1233..230eb55e457f 100644 --- a/packages/nextjs/src/config/templates/apiProxyLoaderTemplate.ts +++ b/packages/nextjs/src/config/templates/apiProxyLoaderTemplate.ts @@ -53,13 +53,21 @@ export const config = { }, }; -const isEdgeRuntime = origConfig.runtime === 'experimental-edge'; +// This is a variable that Next.js will string replace during build with a string if run in an edge runtime from Next.js +// v12.2.1-canary.3 onwards: +// https://github.com/vercel/next.js/blob/166e5fb9b92f64c4b5d1f6560a05e2b9778c16fb/packages/next/build/webpack-config.ts#L206 +// https://edge-runtime.vercel.sh/features/available-apis#addressing-the-runtime +declare const EdgeRuntime: string | undefined; -export default userProvidedHandler - ? isEdgeRuntime - ? userProvidedHandler - : Sentry.withSentryAPI(userProvidedHandler, '__ROUTE__') - : undefined; +let exportedHandler; + +if (typeof EdgeRuntime === 'string') { + exportedHandler = userProvidedHandler; +} else { + exportedHandler = userProvidedHandler ? Sentry.withSentryAPI(userProvidedHandler, '__ROUTE__') : undefined; +} + +export default exportedHandler; // Re-export anything exported by the page module we're wrapping. When processing this code, Rollup is smart enough to // not include anything whose name matchs something we've explicitly exported above. diff --git a/packages/nextjs/src/index.client.ts b/packages/nextjs/src/index.client.ts index 24ca3d82e7d0..2d9e29d48ac8 100644 --- a/packages/nextjs/src/index.client.ts +++ b/packages/nextjs/src/index.client.ts @@ -34,6 +34,7 @@ declare const __SENTRY_TRACING__: boolean; // This is a variable that Next.js will string replace during build with a string if run in an edge runtime from Next.js // v12.2.1-canary.3 onwards: // https://github.com/vercel/next.js/blob/166e5fb9b92f64c4b5d1f6560a05e2b9778c16fb/packages/next/build/webpack-config.ts#L206 +// https://edge-runtime.vercel.sh/features/available-apis#addressing-the-runtime declare const EdgeRuntime: string | undefined; const globalWithInjectedValues = global as typeof global & { diff --git a/packages/nextjs/src/index.server.ts b/packages/nextjs/src/index.server.ts index 19ca5dd26122..0c5982dc747d 100644 --- a/packages/nextjs/src/index.server.ts +++ b/packages/nextjs/src/index.server.ts @@ -33,6 +33,7 @@ const domain = domainModule as typeof domainModule & { active: (domainModule.Dom // This is a variable that Next.js will string replace during build with a string if run in an edge runtime from Next.js // v12.2.1-canary.3 onwards: // https://github.com/vercel/next.js/blob/166e5fb9b92f64c4b5d1f6560a05e2b9778c16fb/packages/next/build/webpack-config.ts#L206 +// https://edge-runtime.vercel.sh/features/available-apis#addressing-the-runtime declare const EdgeRuntime: string | undefined; // Exporting this constant means we can compute it without the linter complaining, even if we stop directly using it in