diff --git a/.changeset/famous-weeks-deliver.md b/.changeset/famous-weeks-deliver.md new file mode 100644 index 000000000..f996e6fd6 --- /dev/null +++ b/.changeset/famous-weeks-deliver.md @@ -0,0 +1,5 @@ +--- +"@opennextjs/cloudflare": patch +--- + +global timer functions now use the one from node:timers diff --git a/examples/playground15/instrumentation.js b/examples/playground15/instrumentation.js index 828854aed..dbbd21bad 100644 --- a/examples/playground15/instrumentation.js +++ b/examples/playground15/instrumentation.js @@ -3,9 +3,16 @@ export function register() { // variable as recommended in the official docs: // https://nextjs.org/docs/app/building-your-application/optimizing/instrumentation#importing-runtime-specific-code + const timeout = setTimeout(() => { + console.log("This is a delayed log from the instrumentation register callback"); + }, 0); + if (process.env.NEXT_RUNTIME === "nodejs") { globalThis["__NODEJS_INSTRUMENTATION_SETUP"] = "this value has been set by calling the instrumentation `register` callback in the nodejs runtime"; + // This is to test that we have access to the node version of setTimeout + timeout.unref(); + clearTimeout(timeout); } if (process.env.NEXT_RUNTIME === "edge") { diff --git a/packages/cloudflare/src/cli/build/bundle-server.ts b/packages/cloudflare/src/cli/build/bundle-server.ts index 56f29608f..84317a5b5 100644 --- a/packages/cloudflare/src/cli/build/bundle-server.ts +++ b/packages/cloudflare/src/cli/build/bundle-server.ts @@ -144,6 +144,11 @@ export async function bundleServer(buildOpts: BuildOptions): Promise { // This define should be safe to use for Next 14.2+, earlier versions (13.5 and less) will cause trouble "process.env.__NEXT_EXPERIMENTAL_REACT": `${needsExperimentalReact(nextConfig)}`, }, + banner: { + // We need to import them here, assigning them to `globalThis` does not work because node:timers use `globalThis` and thus create an infinite loop + // See https://github.com/cloudflare/workerd/blob/d6a764c/src/node/internal/internal_timers.ts#L56-L70 + js: `import {setInterval, clearInterval, setTimeout, clearTimeout, setImmediate, clearImmediate} from "node:timers"`, + }, platform: "node", });