Repository navigation
fix(image): serve /_next/image on Nitro production builds and honor images.minimumCacheTTL - #3741
AhmedElBanna80 wants to merge 13 commits into
Conversation
Reverts the four earlier commits on this branch so the header can be reworked from vinext's actual image-serving paths: - 33ef8eb fix(image): send x-nextjs-cache on /_next/image responses - fcab81a fix(image): answer /_next/image from the Nitro app in production - 793fedd fix(image): match Next.js caching and source headers on the Nitro image path - ec9b824 fix(image): resolve the source path before deciding a Nitro image is immutable
Next.js sends X-Nextjs-Cache on every successful /_next/image response: MISS when the optimizer ran for the request, HIT or STALE when its image cache served the bytes. A 304 and error responses carry no header. vinext keeps no cache of optimized images on any runtime. Every 200 from the image endpoint is transformed (Cloudflare Images binding) or passed through from the source for that request. That is what Next.js does with its image cache disabled (images.maximumDiskCacheSize: 0), where every 200 is a MISS, so the image handlers now send MISS: - handleImageOptimization (Workers, App and Pages Router) sets it with the other headers shared by transformed and passed-through responses. - The Node production server sets it only on responses that carry the image bytes, so a 304 revalidation stays without it. Dev and the App Router without an image optimizer still redirect to the source, which is not an image response. Fixes cloudflare#3733
…mages.minimumCacheTTL Closes cloudflare#3740.
@vinext/cloudflare
create-vinext-app
@vinext/types
vinext
@cloudflare/workers-response-store
commit: |
Performance benchmarksCompared 0 improved · 1 regressed · 6 within ±1.5%
View detailed results and traces 🟢 improvement · 🔴 regression · ⚫ change below 1.5% · paired base/head |
|
One more case this PR does not cover yet: the Pages Router under Nitro. On a Nitro production build of a Pages Router app, vinext/packages/vinext/src/server/pages-request-stage-entry.ts Lines 599 to 600 in c1eee7f The Next.js test that shows it is Repro: build a Pages Router app with I would like to extend this PR to cover the Pages Router as well, by feeding the same Nitro source fetch to the Pages handler when there is no |
…imization # Conflicts: # packages/vinext/src/server/app-rsc-handler.ts # packages/vinext/src/server/image-optimization.ts
|
This pull request was not reviewed automatically because its author does not have write access to this repository. A maintainer with write access can start a review by commenting |
…ization # Conflicts: # packages/vinext/src/entries/app-rsc-entry.ts # packages/vinext/src/index.ts
Closes #3740.
Stacked on #3734, which adds the
x-nextjs-cache: MISSheader. The commits before the last one are that PR; only the last commit is new here. I will rebase once #3734 merges.What this changes
On a Nitro production build,
/_next/imagenow answers with the image instead of a 302 to the source. Workers, the Node server and dev are unchanged.I reworked the earlier attempt to address the points raised on #3734:
minimumCacheTTLcomes from the config.images.minimumCacheTTLis added to theimagestype, passed through the generated entries as part ofimageConfig, and defaults to 14400 as in Next.js. The response ispublic, max-age=<max(source max-age, minimumCacheTTL)>, must-revalidate. Nothing is hard-coded.globalThis.__nitro__check in the handler. The handler takes a new option,resolveImageSourceFetch, which returns a function that fetches a same-origin source. The generated entry sets it only when the Nitro plugin was detected at build time (the existinghasNitroPlugin), through a newserveImagesInProcessentry option. On other hosts the option is not emitted and nothing is read. Tests inject a fake fetch, so they touch no globals.nitro/apphas its ownfetch, but importing it from the RSC environment would bundle a second Nitro app, sogetNitroImageSourceFetchreadsglobalThis.__nitro__.default, which Nitro declares in its types. That is the one remaining read of that global, and it is confined to Nitro builds. If you know a better hook for the host fetch, I will switch to it.images.minimumCacheTTLaside,immutableis used only when the source is under/_next/static/media/or/_next/static/immutable/media/after removing the configuredbasePathand the path part ofassetPrefix. The path is normalised before the check (dot segments and percent-encoding resolved, including a double-encoded slash), so a traversal out of that directory is treated as an ordinary public file. Next.js checksbasePathonly;assetPrefixis included because vinext writes build assets under it.The security properties are kept: the sub-request carries none of the client's headers, only
Content-Type,Content-Length,ETag,Last-ModifiedandCache-Controlare copied from the source (neverSet-Cookie), a non-image source gets a 400, and only same-origin paths are accepted (the existing parameter validation rejects the rest).Tests
tests/app-rsc-handler.test.ts: production answers 200 directly (not 302),Cache-ControlfromminimumCacheTTL, a longer source max-age wins, immutable only for hashed media with and without a pathassetPrefix, traversal (plain,%2e%2e, double-encoded) not immutable, no client headers on the sub-request,Set-Cookienot forwarded, non-image source and cross-origin url rejected, dev and "no host available" still redirect.tests/shims.test.ts:getNitroImageSourceFetch.tests/app-router-next-config-codegen.test.ts: the option is emitted only withserveImagesInProcess.I checked the traversal test by replacing the path normalisation with the raw path; the two immutable tests fail, and pass again after restoring it.
vp checkis clean for the touched files. I could not run the Nitro example end to end here, so I have not run the Next.jsog-apie2e against a real Nitro build.No user-facing docs change:
images.minimumCacheTTLis a Next.js option that is now honoured on this path.