2026-06-02infra
Worker CPU-time reductions (route preloading, DB pool teardown, image optimization)
- Change: Three independent changes to keep the Cloudflare Worker idle between requests and stop tripping "Worker exceeded CPU time limit":
- Route preloading disabled.
open-next.config.tsroutePreloadingBehaviorchanged from"withWaitUntil"to"none". The previous setting rannextServer.unstable_preloadEntries()(loads/compiles every route's server bundle) insidewaitUntilon every request; that work counts against the invocation's CPU budget and blew the limit on cold isolates. - Request-scoped DB pool now closed explicitly.
lib/db.tscreateDbnow returns{ db, pool }; the per-request (ctx-keyed) Hyperdrive pool registersafter(() => pool.end())so the Postgres socket is torn down once the response finishes streaming instead of lingering on the idle timeout. TheglobalThisfallback pool (dev/build/scripts) is unchanged. next/imageoptimization disabled.next.config.tsimagesswitched from a custom loader (app/image-loader.ts→/api/imgCloudflare Images transform) tounoptimized: true.<Image>now serves the original R2 bytes directly; the loader file and/api/imgroute remain on disk but are dormant.
- Route preloading disabled.
- Why: On-the-fly route entry preloading and per-
<Image>resize/webp encoding both ran on the Worker CPU, and the DB pool never closed — together they produced intermittent CPU-limit errors during navigation. The goal is for the Worker to do only the requested page's work and then go idle. - Affected Modules:
open-next.config.ts,lib/db.ts,next.config.ts(and dormant:app/image-loader.ts,app/api/img/route.ts) - Trade-offs:
- Pro: Worker stays idle between requests; no per-request preload or image-transform CPU; Postgres connections are released promptly, easing the Hyperdrive 6-connection-per-invocation budget.
- Con: No route warming (first hit of a cold route pays its own load cost); images are no longer resized/format-converted or width-tailored, so larger original bytes are sent to the browser and
r2.devserves them directly (revisit with a custom domain / Cloudflare image resizing at the edge if bandwidth becomes a concern).