Skip to content

Commit 09aaf35

Browse files
conico974vicb
andauthored
Fix node timers (#619)
* fix for timers * move to banner * chnageset * also import corresponding clear function from node * fix dev * Apply suggestions from code review Co-authored-by: Victor Berchet <victor@suumit.com> --------- Co-authored-by: Victor Berchet <victor@suumit.com>
1 parent f5264d2 commit 09aaf35

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

.changeset/famous-weeks-deliver.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
global timer functions now use the one from node:timers

examples/playground15/instrumentation.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@ export function register() {
33
// variable as recommended in the official docs:
44
// https://nextjs.org/docs/app/building-your-application/optimizing/instrumentation#importing-runtime-specific-code
55

6+
const timeout = setTimeout(() => {
7+
console.log("This is a delayed log from the instrumentation register callback");
8+
}, 0);
9+
610
if (process.env.NEXT_RUNTIME === "nodejs") {
711
globalThis["__NODEJS_INSTRUMENTATION_SETUP"] =
812
"this value has been set by calling the instrumentation `register` callback in the nodejs runtime";
13+
// This is to test that we have access to the node version of setTimeout
14+
timeout.unref();
15+
clearTimeout(timeout);
916
}
1017

1118
if (process.env.NEXT_RUNTIME === "edge") {

packages/cloudflare/src/cli/build/bundle-server.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ export async function bundleServer(buildOpts: BuildOptions): Promise<void> {
144144
// This define should be safe to use for Next 14.2+, earlier versions (13.5 and less) will cause trouble
145145
"process.env.__NEXT_EXPERIMENTAL_REACT": `${needsExperimentalReact(nextConfig)}`,
146146
},
147+
banner: {
148+
// We need to import them here, assigning them to `globalThis` does not work because node:timers use `globalThis` and thus create an infinite loop
149+
// See https://github.com/cloudflare/workerd/blob/d6a764c/src/node/internal/internal_timers.ts#L56-L70
150+
js: `import {setInterval, clearInterval, setTimeout, clearTimeout, setImmediate, clearImmediate} from "node:timers"`,
151+
},
147152
platform: "node",
148153
});
149154

0 commit comments

Comments
 (0)