-
-
Notifications
You must be signed in to change notification settings - Fork 127
Description
Environment
Next.js: 16.0.8
oRPC: 1.12.2
pnpm: 10.20.0
Node.js 22.20.0
Reproduction
https://github.com/inducingchaos/code-reproductions/tree/orpc-nextjs-monorepo-hmr-staleness
Describe the bug
See the reproduction README for more details.
Basically, whenever you make changes on the backend, Turbopack does not recompile them and you end up with your dev server running code that doesn't physically exist in your codebase.
What I've figured out (disclaimer: I don't know much about bundlers or how object references work):
- I don't think it's a
transpilePackagesissue, since the app dir bundles dependencies automatically (see Documentation: transpiling step seems not necessary with the pre-installed Next.js application vercel/turborepo#7058 (comment)) - AI thinks that "The issue is that
client.tsusesconst client = globalThis.$client ?? createORPCClient(link), which captures the reference at module initialization. When HMR updatesglobalThis.$clientinclient.server.ts, theclientconst inclient.tskeeps the old reference because that module doesn't re-evaluate." However, this initialization pattern is the exact one recommended in https://orpc.dev/docs/adapters/next#optimize-ssr - and I recall it working before. - This only seems to affect API routes that consume the API client. Pages/components that use it seem to HMR correctly.
I found a lot more nuanced details while testing, but how the project constructs the dependency/import graph really affects it (e.g., at some point saving the router file logged v2 at the module level but not in the handlers, as well as updating the Next.js API endpoints would trigger the handler code to be successfully updated). Because of this, it's really hard to provide concrete facts about the issue.
I've been trying to resolve this since Tuesday evening with Opus 4.5 and finally got it working in my main app using a Proxy instance (thanks to the idea from https://github.com/unnoq/orpc/discussions/744), but wasn't able to get this solution to work in the repro.
Any ideas?
Additional context
No response