Repository navigation
Description
Provide environment information
System:
OS: macOS 26.2
Binaries:
Node: 24.12.0
npm: 11.6.2
npmPackages:
@trpc/server: 11.29.5
Describe the bug
If the function passed to lazy() rejects once, that router never loads again. getProcedureAtPath awaits lazyRouter.load() (router.ts L460), and load is wrapped in once() (L354), which keeps the promise it got even when that promise rejects. After one failed dynamic import, every call into that subtree rethrows the same error and the loader is not called a second time, until the process restarts.
I'd expect a later call to try the loader again.
Link to reproduction
To reproduce
import { initTRPC, lazy } from '@trpc/server';
const t = initTRPC.create();
let attempts = 0;
const user = lazy(async () => {
attempts++;
if (attempts === 1) throw new Error('chunk failed to load');
return t.router({ hello: t.procedure.query(() => 'hi') });
});
const caller = t.router({ user }).createCaller({});
await caller.user.hello().catch(() => {}); // rejects, attempts === 1
await caller.user.hello(); // rejects again, attempts is still 1npm install && npm start in the repro above prints:
call 1: chunk failed to load (loader attempts: 1)
call 2: chunk failed to load (loader attempts: 1)
call 3: chunk failed to load (loader attempts: 1)
Additional information
once() is at packages/server/src/unstable-core-do-not-import/router.ts L102-111. Clearing the stored promise when it rejects fixes it:
if (result === uncalled) {
result = fn();
if (result instanceof Promise) {
result.catch(() => {
result = uncalled;
});
}
}With that change (I patched the built once in the repro's node_modules to check) the same script prints call 2: hi (loader attempts: 2). Concurrent callers still share one in-flight load, since the reset only happens after the rejection.
Happy to send a PR with a test if that direction works for you.
👨👧👦 Contributing
- 🙋♂️ Yes, I'd be down to file a PR fixing this bug!
Activity
fix(server): retry a lazy router load after it rejects (trpc#8150)
github-actions commentedon Nov 24, 2026
This issue has been locked because we are very unlikely to see comments on closed issues. If you are running into a similar issue, please create a new issue. Thank you.