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

The instrumentation hook is only called after visiting a route #59999

Closed
1 task done
daveyjones opened this issue Dec 28, 2023 · 3 comments · Fixed by #63536
Closed
1 task done

The instrumentation hook is only called after visiting a route #59999

daveyjones opened this issue Dec 28, 2023 · 3 comments · Fixed by #63536
Labels
bug Issue was opened via the bug report template. linear: next Confirmed issue that is tracked by the Next.js team. locked

Comments

@daveyjones
Copy link

daveyjones commented Dec 28, 2023

Link to the code that reproduces this issue

https://github.com/daveyjones/nextjs-instrumentation-bug

To Reproduce

git clone [email protected]:daveyjones/nextjs-instrumentation-bug.git
cd nextjs-instrumentation-bug
npm install
npm run dev # The register function IS called
npm run build && npm start # The register function IS NOT called until you visit http://localhost:3000

Current vs. Expected behavior

The register function should be called automatically after running npm run build && npm start. You should not have to visit a route for the function to run.

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.1.0: Mon Oct  9 21:28:12 PDT 2023; root:xnu-10002.41.9~6/RELEASE_ARM64_T8103
Binaries:
  Node: 20.9.0
  npm: 10.2.4
  Yarn: 1.22.19
  pnpm: N/A
Relevant Packages:
  next: 14.0.5-canary.29
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.3.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

App Router

Additional context

This behavior is also reported in the comments of #49897 (reference). However, since #49897 is focused on standalone mode (which is not a factor here), I felt it was best to create a separate issue. It may be the case that resolving this issue will also resolve #49897.

NEXT-2738

@daveyjones daveyjones added the bug Issue was opened via the bug report template. label Dec 28, 2023
@ships
Copy link

ships commented Mar 5, 2024

I arrived at this issue after knocking my head on a related problem, but which turns out to be different -- leaving comment here for other seekers.

My issue was that instrumentation hooks were not running at all in a docker context (without standalone) even though they were running on next start locally.

next build puts the instrumentation hook into the built directory, but still relies on next.config.js to specify the experimental flag for those hooks to run. In my context I had copied the build directory into my docker image but left next.config.js behind.

After keeping next.config.js around, I was able to get the hooks running, but they still run only once requests are received. However, I don't have the issue of it happening repeatedly -- register are only invoked on the first API route handled.

@samcx samcx added the linear: next Confirmed issue that is tracked by the Next.js team. label Mar 7, 2024
@anuraags
Copy link

This is actually a pretty major bug that took me a while to debug. I had to create a copy of my next.config.js into next.prod.config.js that is loaded into the docker context just to get my instrumentation working.

I have no issue with running the register function for every request, but I think the running of the instrumentation should be baked into the next build step.

styfle added a commit that referenced this issue Mar 21, 2024
This ensures that the instrumentation hook for Node.js will run
immediately during `next start` instead of waiting for the first request
like does it `next dev`.

However, if there is a separate instrumentation hook for Edge, that will
still be lazy evaluated and wait until the first request.

Fixes #59999 
Fixes NEXT-2738
Copy link
Contributor

github-actions bot commented Apr 5, 2024

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot added the locked label Apr 5, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 5, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. linear: next Confirmed issue that is tracked by the Next.js team. locked
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants