-
Notifications
You must be signed in to change notification settings - Fork 28.3k
Instrumentation Regression in Next.js 15.x Compared to 14.x #78044
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
Comments
@Lisenish It's due to Next.js started preloading the entries since v15 (related PR: #65289). We can fix this behavior on our side. But I'd like to learn what's the real-world case you're relying on this behavior? Cause the global side effects in page could execute in a lot of places either in static build or now in entry preloading. It sounds risky to me to rely on the contract that. Curious what are you doing with relying on the execution order of instrumentation and the page. |
Hello @huozhi, thanks for looking into this! (and I see you even have a draft PR for this!) Yes, I suspected this was due to the preloading. I also understand your point, and I agree global side-effects are dangerous and probably not the best practice it's better to avoid them, agree completely 👍 With this issue I mainly wanted to highlight that IMHO Next 15 has silently introduced a breaking change with this new behavior, and ideally it should be either fixed or we need to update the docs (even though it was only experimental in Next 14). Why is it a breaking change? Please correct me if I'm wrong but I think it was always assumed that I think you understand if I change the await loadSecrets();
await import("./global-side-effect-file"); it still won't change the behavior, the global side effect will run before If Next team thinks the current behavior is the correct one and won't fix then IMHO we should add a note to Next 15 changelog and update the above docs. Now let me still describe our use case here at the end if you're still curious (IMHO we shouldn't focus on it here since there could be other cases among other users). Our Use CaseIt's actually somewhat similar to what I did in MRE:
We of course, refactored the side-effect part to get rid of it, since we wanted to upgrade to 15 without waiting for the official patch, but it actually required some effort since we have many web apps and we needed to do the same fix across all of them. |
@huozhi Thank you for the quick fix! 🙇 (just in case also confirmed it works now in MRE with the latest canary) |
Link to the code that reproduces this issue
https://github.com/Lisenish/next-15-instrumentation-issue-mre
To Reproduce
npm install
.npm run build
next start
.next-14-version
Current vs. Expected behavior
Next.js 15.x (Regression)
In Next.js 15.x, side effect files appear to execute before instrumentation file complete its execution. This causes environment variables or state setup in the instrumentation file to be unavailable during the side effect execution.
You can see in logs that we have the log line
Running global side effect file.
while instrumentation file didn't finish the executionLogs:
On the app side it results in static value in global var not being set correctly, and it shows "no set value"
Next.js 14.x (Expected Behavior)
Instrumentation files should fully run before other files (e.g., global side effect files) dependent on their side effects. This was the behavior in Next.js 14.x and is the expected behavior.
Logs shows it runs as expected (instrumentation load finishes before any attempts to execute side effects in other files)
On the app side both values are present:
Provide environment information
Operating System: Platform: darwin Arch: arm64 Version: Darwin Kernel Version 24.3.0: Thu Jan 2 20:24:23 PST 2025; root:xnu-11215.81.4~3/RELEASE_ARM64_T6031 Available memory (MB): 36864 Available CPU cores: 14 Binaries: Node: 23.11.0 npm: 10.9.2 Yarn: N/A pnpm: 10.8.0 Relevant Packages: next: 15.3.1-canary.4 // Latest available version is detected (15.3.1-canary.4). eslint-config-next: N/A react: 19.1.0 react-dom: 19.1.0 typescript: 5.8.3 Next.js Config: output: N/A
Which area(s) are affected? (Select all that apply)
Instrumentation
Which stage(s) are affected? (Select all that apply)
next start (local), Other (Deployed)
Additional context
No response
The text was updated successfully, but these errors were encountered: