-
Notifications
You must be signed in to change notification settings - Fork 329
feat(instrumentation): support nextConfig.instrumentationClientInject #1416
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
base: main
Are you sure you want to change the base?
Changes from all commits
489bea8
13a640b
6339697
1ae9ab4
2b44231
b37769f
dd39e20
bf0e1ee
5ce7226
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import fs from "node:fs"; | ||
| import path from "node:path"; | ||
|
|
||
| /** Resolve empty-module next to this file (.js in dist, .ts in source). */ | ||
| function resolveInstrumentationClientEmptyModule(dir: string): string { | ||
| const jsPath = path.join(dir, "empty-module.js"); | ||
| if (fs.existsSync(jsPath)) return jsPath; | ||
| return path.join(dir, "empty-module.ts"); | ||
| } | ||
|
|
||
| /** Absolute path to the vinext empty-module fallback for composed client instrumentation. */ | ||
| export const INSTRUMENTATION_CLIENT_EMPTY_MODULE = resolveInstrumentationClientEmptyModule( | ||
| import.meta.dirname, | ||
| ); | ||
|
|
||
| /** | ||
| * Generate a virtual ESM module that implements the Next.js | ||
| * `instrumentationClientInject` contract for client bootstrap. | ||
| * | ||
| * Resolution follows two paths depending on whether injects are configured: | ||
| * | ||
| * **Empty injects (`injects.length === 0`):** Returns `export {}` and the | ||
| * plugin does not serve a virtual module. The `resolve.alias` for | ||
| * `private-next-instrumentation-client` resolves directly to the user's | ||
| * `instrumentation-client` file (or {@link INSTRUMENTATION_CLIENT_EMPTY_MODULE} when absent), | ||
| * so the user's `onRouterTransitionStart` is used as-is with no composition. | ||
| * | ||
| * **Non-empty injects:** The plugin serves this generated module via | ||
| * `resolveId`/`load`. It side-effect-imports each inject in config order, then | ||
| * the user's file last, and exports a single composed `onRouterTransitionStart` | ||
| * that fans out to every module's hook. | ||
| * | ||
| * **Specifier resolution:** Next.js webpack loader resolves every inject against | ||
| * the project root at build time (`this.resolve(rootContext, spec)`) and emits | ||
| * `require(resolvedPath)`. Vinext pre-resolves `./` and `../` in the plugin | ||
| * `config()` hook; bare specifiers rely on Vite resolution at bundle time. | ||
| * | ||
| * @param injects - Module specifiers from `nextConfig.instrumentationClientInject` | ||
| * @param userPath - Absolute path to the user's `instrumentation-client` file, | ||
| * or `null` when the file doesn't exist | ||
| * @param emptyModulePath - Absolute path to the empty-module fallback | ||
| */ | ||
| export function generateInstrumentationClientInjectModule( | ||
| injects: readonly string[], | ||
| userPath: string | null, | ||
| emptyModulePath: string = INSTRUMENTATION_CLIENT_EMPTY_MODULE, | ||
| ): string { | ||
| if (injects.length === 0) { | ||
| return "export {};"; | ||
| } | ||
|
|
||
| const lines: string[] = []; | ||
|
|
||
| for (let i = 0; i < injects.length; i++) { | ||
| lines.push(`import * as __vinj_${i} from ${JSON.stringify(injects[i])};`); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Next.js resolves inject specifiers against the project root before emitting |
||
| } | ||
|
|
||
| const userSlot = injects.length; | ||
| lines.push(`import * as __vinj_${userSlot} from ${JSON.stringify(userPath ?? emptyModulePath)};`); | ||
|
|
||
| const hookCalls: string[] = []; | ||
| for (let i = 0; i <= userSlot; i++) { | ||
| hookCalls.push( | ||
| ` if (typeof __vinj_${i}.onRouterTransitionStart === "function") {`, | ||
| ` __vinj_${i}.onRouterTransitionStart(url, type);`, | ||
| ` }`, | ||
| ); | ||
| } | ||
|
|
||
| lines.push(""); | ||
| lines.push("export function onRouterTransitionStart(url, type) {"); | ||
| lines.push(...hookCalls); | ||
| lines.push(`}`); | ||
| lines.push(""); | ||
|
|
||
| return lines.join("\n"); | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -211,6 +211,12 @@ export type NextConfig = { | |||||||||||||
| output?: "export" | "standalone"; | ||||||||||||||
| /** File extensions treated as routable pages/routes (Next.js pageExtensions) */ | ||||||||||||||
| pageExtensions?: string[]; | ||||||||||||||
| /** | ||||||||||||||
| * Module specifiers that are required for side effects on the client before | ||||||||||||||
| * hydration, in array order, ahead of the user's `instrumentation-client.{ts,js}`. | ||||||||||||||
| * Each entry may be a bare npm package name or a path relative to the project root. | ||||||||||||||
| */ | ||||||||||||||
| instrumentationClientInject?: string[]; | ||||||||||||||
| /** Extra origins allowed to access the dev server. */ | ||||||||||||||
| allowedDevOrigins?: string[]; | ||||||||||||||
| /** Maximum age in seconds for stale ISR entries before blocking regeneration. */ | ||||||||||||||
|
|
@@ -290,6 +296,7 @@ export type ResolvedNextConfig = { | |||||||||||||
| trailingSlash: boolean; | ||||||||||||||
| output: "" | "export" | "standalone"; | ||||||||||||||
| pageExtensions: string[]; | ||||||||||||||
| instrumentationClientInject: string[]; | ||||||||||||||
| cacheComponents: boolean; | ||||||||||||||
| redirects: NextRedirect[]; | ||||||||||||||
| rewrites: { | ||||||||||||||
|
|
@@ -951,6 +958,7 @@ export async function resolveNextConfig( | |||||||||||||
| buildId, | ||||||||||||||
| deploymentId, | ||||||||||||||
| sassOptions: null, | ||||||||||||||
| instrumentationClientInject: [], | ||||||||||||||
| }; | ||||||||||||||
| detectNextIntlConfig(root, resolved); | ||||||||||||||
| return resolved; | ||||||||||||||
|
|
@@ -1138,6 +1146,11 @@ export async function resolveNextConfig( | |||||||||||||
| trailingSlash: config.trailingSlash ?? false, | ||||||||||||||
| output: output === "export" || output === "standalone" ? output : "", | ||||||||||||||
| pageExtensions, | ||||||||||||||
| instrumentationClientInject: Array.isArray(config.instrumentationClientInject) | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: this validates that
Suggested change
This is consistent with how |
||||||||||||||
| ? (config.instrumentationClientInject as unknown[]).filter( | ||||||||||||||
| (x): x is string => typeof x === "string", | ||||||||||||||
| ) | ||||||||||||||
| : [], | ||||||||||||||
| cacheComponents: config.cacheComponents ?? false, | ||||||||||||||
| redirects, | ||||||||||||||
| rewrites, | ||||||||||||||
|
|
||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
injectsis empty, this returns"export {};"regardless of whether the user has aninstrumentation-clientfile. The PR description says this is intentional ("the alias already handles the user file or empty-module"), which is correct — theresolve.aliasat index.ts:1261 mapsprivate-next-instrumentation-clientto the user file or empty-module and takes effect when this plugin returnsnullfromresolveId.However, this means there's an asymmetry in the hook composition path: when injects are present, the composed
onRouterTransitionStartfans out to all modules including the user file. When injects are empty, the user'sonRouterTransitionStartflows directly through the alias. Both paths work, but it's worth documenting this explicitly in the function's JSDoc (the current doc hints at it but doesn't state the two resolution paths clearly).