Repository navigation
Experimental incremental static builds - #17084
Conversation
🦋 Changeset detectedLatest commit: 68f325f The changes in this PR will be included in the next version bump. This PR includes changesets to release 416 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
commit: |
|
@matthewp This is going to be a game changer for large sites! Can't wait to test the preview. Quick technical note on caching dist/ for Cloudflare: turning on Cloudflare's Native Build Cache (Settings > Build > Build cache > Enable) handles node_modules/.astro automatically, but my understanding is that it wipes the dist/ directory between builds. |
|
@deepanshu88 ok good to know. Which folders are cached isn't configurable? I considered caching the files in the node_modules/.astro/ folder but wanted to avoid duplicate writes. |
|
@matthewp Unfortunately, Cloudflare's Native Build Cache isn't granularly configurable via settings or config files. For astro, it uses this cache directory - node_modules/.astro for reuse in subsequent builds. What if we only perform the duplicate write to node_modules/.astro/dist-cache/ when a specific CI-optimized flag or a fallback mode is triggered? Astro boots up and looks at ./dist. if It's completely empty, instead of re-render everything from scratch, Astro checks node_modules/.astro/project-cache/ and if it sees the backup HTML pages from Build1 are safe and sound. Before the actual compilation even begins, Astro instantly copy-pastes those backup HTML files out of node_modules/ and drops them right back into the empty ./dist folder. A 2x write penalty is a one-time infrastructure seeding cost. |
|
Yeah we'll probably need to do something like that. This PR is still early and being tested to make sure the gains are significant enough. Assuming it is we'll figure out the different CI environment stuff. |
|
Sounds like a good plan. Let me know if you need any large scale static sites to benchmark against once the PR is ready for public eyes - happy to help test it. |
|
@deepanshu88 It's ready to test now, with the caveat that you mentioned. So I would just test locally. Note that if you are on Astro 6 there are other changes in Astro 7 that improve perf, so you'd want to test your baseline, then do an Astro 7 upgrade, and then use this. |
Co-authored-by: Armand Philippot <git@armand.philippot.eu>
Co-authored-by: Armand Philippot <git@armand.philippot.eu>
|
Middleware can mutate any page, so I'm not sure what to do about that. It doesn't matter if the dependencies change or not, middleware is completely dynamic. I think this is likely something to document rather than try to fix. I've added a comment to the RFC to think about this more: withastro/roadmap#1404 (comment) |
The Cloudflare prerenderer renders pages in workerd, so the content entries and image transforms a page resolves are collected in the worker rather than the build process. Per-page image replay and content-entry tracking therefore silently failed under Cloudflare, and skipped pages 404'd on their optimized images. Add an optional AstroPrerenderer.takeRenderMetadata() that an out-of-process prerenderer implements to report the entries and transforms collected during its most recent render. generate.ts prefers it over the (empty) in-process collectors. The Cloudflare worker wraps app.render in the collectors and returns a PrerenderEnvelope carrying the response alongside the metadata; render() reconstructs the response and stashes the metadata. workerd's own addStaticImage now calls recordStaticImage, since astro's build-time Vite plugin never runs in the worker.
# Conflicts: # packages/integrations/cloudflare/src/index.ts # packages/integrations/cloudflare/src/prerenderer.ts
The collectors expose their side-channel on globalThis under a Symbol.for key so the build, the bundled content runtime, and out-of-process prerenderers all resolve the same instance. A plain assignment leaves that binding writable and configurable, so any module sharing globalThis during the build could overwrite or delete it. Install the binding once via Object.defineProperty with configurable: false and writable: false, and mutate an inner 'current' field across begin/end instead of reassigning the property.
…ethod The out-of-process incremental metadata was surfaced through a stateful takeRenderMetadata() call that returned data for the most recent render, which relied on render being called exactly once, in order, right before. Widen render()'s return to Response | PrerenderResult so a prerenderer can pair the response with the content entries and image transforms the page resolved. A single normalizePrerenderResult helper resolves both shapes. This is a non-breaking widening: prerenderers returning a bare Response are unchanged, and in-process ones keep doing so since the build collects their metadata directly.
On a compile-image-service incremental build where every page is skipped, the worker renders nothing, so collectStaticImages() returned no transforms and never installed the Node image service. But a skipped page restores its image transforms into the global list, and the asset pipeline regenerates them when their optimized output is not already in the asset cache. With no service installed, getConfiguredImageService() fell back to importing virtual:image-service and threw InvalidImageService. Install the service when restored images are present too, not only when the worker collected transforms. The test now clears the asset cache before the rebuild so the regeneration path runs deterministically instead of depending on an asset-cache hit, which masked the failure on some platforms.
The fixture used the default Sharp image service, so its build-time image generation failed on the Windows CI runner where Sharp's native binary cannot load (the sibling compile-image-service tests skip on Windows for this reason). Switch it to a Sharp-free custom service so the incremental image replay is exercised on every platform. Reverts the previous asset-cache-clearing test change and the skip-path image-service install in the prerenderer, which were based on a misdiagnosis: the failure was Sharp not loading on the initial build, not the incremental skip path.
|
@ematipico everything in the reviews has been implemented. |
ematipico
left a comment
There was a problem hiding this comment.
Awesome! Looking forward to this
RFC: withastro/roadmap#1404
API Summary
getStaticPathscan provide acacheKeyfor each path. This will be used, in addition to the dependency graph, to cache those pages.entry.digestwhich can be used as thecacheKey.Changes
experimental.incrementalBuildflag that skips regenerating static pages whose module dependencies and user-providedcacheKeyare unchanged from the previous build.getStaticPaths()that include acacheKey. Static pages without acacheKeyare always re-rendered.dist/is preserved between builds instead of being emptied. Orphaned output files (pages removed fromgetStaticPaths) are cleaned up automatically.incremental-build.json) is stored innode_modules/.astro/alongside the existing content layer cache. Dependency hashes are computed from the Rolldown module graph including source file contents, so layout/component changes correctly invalidate cached pages.node_modules/.astro/anddist/must be preserved between builds for skipping to take effect. Ifdist/is missing, cached pages are re-rendered.Testing
incremental-build.test.tswith 9 assertions across 6 scenarios: first build, unchanged rebuild (cache hit), missing output file (forces re-render), changed dependency (invalidates all route paths), changedcacheKey(selective re-render), and flag-disabled regression (verifiesdist/is still emptied and no cache manifest is written).Docs
experimental.incrementalBuild.