Repository navigation
fix: rebuild module imports after content entry deletion - #17713
Conversation
🦋 Changeset detectedLatest commit: 5397992 The changes in this PR will be included in the next version bump. This PR includes changesets to release 420 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 |
Merging this PR will degrade performance by 13.23%
|
| Mode | Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|---|
| 👁 | Simulation | Build: hybrid site (static + server) |
1.4 s | 1.6 s | -14.12% |
| 👁 | Simulation | Build: full server site |
1.4 s | 1.6 s | -14.91% |
| 👁 | Simulation | Rendering: streaming [true], .md file |
1.3 ms | 1.4 ms | -10.61% |
Comparing wakqasahmed:fix/content-modules-stale-import-17707 (5397992) with main (97140b2)
wakqasahmed
left a comment
There was a problem hiding this comment.
Cold-start review (no prior context on this change).
Verdict: looks correct — non-blocking comments only. I verified the fix against the invariant it reconstructs rather than trusting the description.
Correctness of #rebuildModuleImports()
The rebuild is a faithful reconstruction. scopedStore.set() is the only thing that populates #moduleImports inside Astro:
if (deferredRender) {
entry.deferredRender = deferredRender;
if (filePath) {
this.addModuleImport(filePath);
}
}and addModuleImport(fileName) does this.#moduleImports.set(fileName, contentModuleToId(fileName)). The new rebuild uses exactly the same predicate (deferredRender && filePath), the same map key (entry.filePath), and the same value derivation. So keys stay byte-identical to what was written before — important, because content-modules.mjs is looked up at render time by filePath, and a key-shape divergence here would have been a silent runtime break. It isn't one.
Cross-checked the only other producer, packages/astro/src/content/loaders/glob.ts:166 — store.addModuleImport(existingEntry.filePath) on the unchanged-digest fast path, where filePath is the same posixRelative(root, filePath) value. Consistent.
Meta collections (meta:*) hold plain strings, not DataEntry; typedEntry.deferredRender is undefined on those, so they're skipped. Same as #rebuildAssetImports().
Trigger points
Correct and complete for the reported bug:
- dev-time delete/rename goes through
glob.ts:405(unlinkwatcher) andglob.ts:154/glob.ts:354— all land inMutableDataStore.delete(), which now schedules the module rewrite. writeModuleImports()is called once per sync fromcontent-layer.ts:356, immediately afterwriteAssetImports(), so the rebuild-before-write placement is symmetric with assets.
Performance
No regression relative to the established pattern. #writeModulesImportsDebounced() already existed and is debounced at SAVE_DEBOUNCE_MS (500ms) with clearTimeout collapsing, so the untouchedEntries.forEach(store.delete) loop in glob.ts:354 still results in one write. The added cost is one extra O(total entries) walk per debounced write, mirroring #rebuildAssetImports().
Changeset
.changeset/fix-content-modules-stale-import.md — 'astro': patch is the right package and the right bump for a bugfix with no API surface change.
Four inline notes below; none of them block.
| const typedEntry = entry as DataEntry; | ||
| if (typedEntry.deferredRender && typedEntry.filePath) { | ||
| const id = contentModuleToId(typedEntry.filePath); | ||
| if (id) { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
|
||
| async writeModuleImports(filePath: PathLike) { | ||
| this.#modulesFile = filePath; | ||
| this.#rebuildModuleImports(); |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| this.#modulesFile = filePath; | ||
| this.#rebuildModuleImports(); | ||
|
|
||
| if (this.#moduleImports.size === 0) { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| scoped.delete('page-a'); | ||
| await store.writeModuleImports(modulesFilePath); | ||
| await store.waitUntilSaveComplete(); | ||
|
|
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
…ll (withastro#17707) Prior tests called writeModuleImports() explicitly right after delete/clear, which forced the rebuild to run synchronously regardless of whether the new #writeModulesImportsDebounced() calls inside delete()/clear()/clearAll() were wired correctly. Reverting those three trigger lines still passed all existing assertions. Rewrite the delete/clear tests to rely on waitUntilSaveComplete() alone, add a clearAll()-specific test, and add a rename test (delete(oldId) + set(newId)) matching the issue's actual reported scenario. Verified: reverting the three trigger lines makes the delete/clear/clearAll tests fail; restoring them makes all pass.
This comment was marked as spam.
This comment was marked as spam.
|
surprised to see that it had been repaired so quickly no matter how, thanks for your fixing, i had tested on my project, it worked well - cheers! |
|
maybe use |
The zero-size branch in writeModuleImports() fell through to the generator below it, writing the file twice on every call where #moduleImports ends up empty (e.g. after deleting the last deferred-render entry) -- first 'export default new Map();', then a second, differently-formatted empty-map write. That path only became reachable once #moduleImports could shrink back to zero, which this PR introduces. Add the missing return, matching the pattern review comment. Also note in the changeset that addModuleImport() callers without a backing deferredRender entry no longer survive a write, now that #moduleImports is fully derived state.
|
Thanks, I'll review this shortly. |
This PR contains the following updates: | Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) | |---|---|---|---| | [astro](https://astro.build) ([source](https://github.com/withastro/astro/tree/HEAD/packages/astro)) | [`7.2.4` → `7.2.6`](https://renovatebot.com/diffs/npm/astro/7.2.4/7.2.6) |  |  | --- >⚠️ **Warning** > > Some dependencies could not be looked up. Check the [Dependency Dashboard](issues/273) for more information. --- ### Release Notes <details> <summary>withastro/astro (astro)</summary> ### [`v7.2.6`](https://github.com/withastro/astro/blob/HEAD/packages/astro/CHANGELOG.md#726) [Compare Source](https://github.com/withastro/astro/compare/[email protected]@7.2.6) ##### Patch Changes - [#​17812](withastro/astro#17812) [`29af6da`](withastro/astro@29af6da) Thanks [@​matthewp](https://github.com/matthewp)! - Fixes a bug where `new FetchState(request)` could fail in development when server dependencies were optimized ### [`v7.2.5`](https://github.com/withastro/astro/blob/HEAD/packages/astro/CHANGELOG.md#725) [Compare Source](https://github.com/withastro/astro/compare/[email protected]@7.2.5) ##### Patch Changes - [#​17758](withastro/astro#17758) [`5f419e2`](withastro/astro@5f419e2) Thanks [@​astro-factory](https://github.com/apps/astro-factory)! - Fixes a bug where `experimental_getFontFileURL()` rejected valid font URLs when using the Cloudflare adapter - [#​17416](withastro/astro#17416) [`493796b`](withastro/astro@493796b) Thanks [@​iseraph-dev](https://github.com/iseraph-dev)! - Skips no-op pathname writes when normalizing SSR request URLs - [#​17712](withastro/astro#17712) [`bd374b7`](withastro/astro@bd374b7) Thanks [@​fkatsuhiro](https://github.com/fkatsuhiro)! - Updates deprecation messages target from Astro 7 to 8 - [#​17719](withastro/astro#17719) [`dac1768`](withastro/astro@dac1768) Thanks [@​astrobot-houston](https://github.com/astrobot-houston)! - Fixes session ID validation to reject non-UUID cookie values before using them as storage keys - [#​17770](withastro/astro#17770) [`84eb7e7`](withastro/astro@84eb7e7) Thanks [@​astro-factory](https://github.com/apps/astro-factory)! - Fixes `--mode`, `--site`, `--base`, `--out-dir`, `--verbose`, `--silent`, and `--open` flags being silently dropped when using `astro dev --background` or `astro preview --background` - [#​17713](withastro/astro#17713) [`d035290`](withastro/astro@d035290) Thanks [@​wakqasahmed](https://github.com/wakqasahmed)! - Fixes `content-modules.mjs` not removing entries for deleted or renamed content files, which could cause Vite to attempt to resolve non-existent modules As part of this fix, `#moduleImports` is now fully rebuilt from `deferredRender` entries before every write, so a module import added only through the public `addModuleImport()` API without a corresponding `deferredRender` entry in the store will no longer be preserved across writes. - [#​17743](withastro/astro#17743) [`adc750f`](withastro/astro@adc750f) Thanks [@​contactjawad](https://github.com/contactjawad)! - Fixes `Astro.preferredLocale` and `Astro.preferredLocaleList` ignoring `Accept-Language` quality values when they are absent or `0`. An entry without an explicit `q=` now correctly counts as quality `1.0` (per RFC 7231) and an entry with `q=0` is treated as not acceptable, so the highest-quality locale is selected regardless of header order. - [#​17757](withastro/astro#17757) [`660991c`](withastro/astro@660991c) Thanks [@​astro-factory](https://github.com/apps/astro-factory)! - Fixes build errors showing wrong file location, missing line:col, and misleading hints when a plugin error (e.g. from MDX) is wrapped by Vite's build error - [#​17783](withastro/astro#17783) [`60b14ff`](withastro/astro@60b14ff) Thanks [@​matthewp](https://github.com/matthewp)! - Fixes a type error when passing an image from a content collection `image()` schema to a component or `<Image />`. The schema returned by `image()` was missing the `apng` format, so it no longer matched the type of an imported image. - [#​17664](withastro/astro#17664) [`d483125`](withastro/astro@d483125) Thanks [@​astrobot-houston](https://github.com/astrobot-houston)! - Fixes an issue where Astro CSP support didn't correctly handle cases `"unsafe-inline"` resource. Now when `"unsafe-inline"`, Astro won't emit hashes for the directive specified. - [#​17810](withastro/astro#17810) [`0fc5f65`](withastro/astro@0fc5f65) Thanks [@​florian-lefebvre](https://github.com/florian-lefebvre)! - Fixes a regression in the content collections that could cause images to not be resolved - [#​17781](withastro/astro#17781) [`aa33b44`](withastro/astro@aa33b44) Thanks [@​matthewp](https://github.com/matthewp)! - Fixes `memoryCache()` storing responses that set cookies through `Astro.cookies` or `Astro.session` - [#​17787](withastro/astro#17787) [`6661fbe`](withastro/astro@6661fbe) Thanks [@​astro-factory](https://github.com/apps/astro-factory)! - Fixes `server:defer` crashing the dev server with "undefined is not a function" when a deferred component imports from `astro:i18n` - [#​17750](withastro/astro#17750) [`dd0e3ac`](withastro/astro@dd0e3ac) Thanks [@​dobrodob](https://github.com/dobrodob)! - Fixes a regression where `transition:persist` stopped working for `<audio>` and `<video>` elements. - [#​17774](withastro/astro#17774) [`fe1d16d`](withastro/astro@fe1d16d) Thanks [@​astro-factory](https://github.com/apps/astro-factory)! - Adds support for importing `.apng` files as image metadata for use with standard `<img>` elements. Astro's image components reject APNG files to avoid removing their animation - [#​17799](withastro/astro#17799) [`8797754`](withastro/astro@8797754) Thanks [@​astro-factory](https://github.com/apps/astro-factory)! - Fixes i18n `fallbackType: "rewrite"` returning 500 instead of 404 when the fallback locale also has no matching static path for a prerendered dynamic route - [#​17741](withastro/astro#17741) [`99d3d3d`](withastro/astro@99d3d3d) Thanks [@​ericswpark](https://github.com/ericswpark)! - Bumps the Astro compiler to the latest version. [Changelog](https://github.com/withastro/compiler-rs/releases/tag/%40astrojs%2Fcompiler-rs%400.4.0). - [#​17782](withastro/astro#17782) [`3578d45`](withastro/astro@3578d45) Thanks [@​Princesseuh](https://github.com/Princesseuh)! - Improves the performance of the Astro CLI in local by enabling Node's module compilation cache. - [#​17705](withastro/astro#17705) [`2043e4f`](withastro/astro@2043e4f) Thanks [@​astrobot-houston](https://github.com/astrobot-houston)! - Fixes incremental builds serving cached HTML that references stale CSS filenames after a stylesheet-only edit - [#​17754](withastro/astro#17754) [`3d50dfd`](withastro/astro@3d50dfd) Thanks [@​astro-factory](https://github.com/apps/astro-factory)! - Fixes the dev server refusing to start in Docker containers after a restart due to PID reuse in the lock file check - [#​17769](withastro/astro#17769) [`bbda94d`](withastro/astro@bbda94d) Thanks [@​astro-factory](https://github.com/apps/astro-factory)! - Fixes a build failure when defining `vite.environments.ssr` in the Astro config. User-provided environment config for `ssr`, `prerender`, or `client` is now properly deep-merged with Astro's internal environment settings instead of silently breaking the server entry naming. - [#​17776](withastro/astro#17776) [`0874da8`](withastro/astro@0874da8) Thanks [@​astro-factory](https://github.com/apps/astro-factory)! - Fixes the `glob()` content loader failing to load files with colons in their names (e.g., `Guide: Architecture.md`) - Updated dependencies \[[`0762a83`](withastro/astro@0762a83), [`0c99615`](withastro/astro@0c99615)]: - [@​astrojs/markdown-satteri](https://github.com/astrojs/markdown-satteri)@​0.3.8 </details> --- ### Configuration 📅 **Schedule**: (UTC) - Branch creation - At any time (no schedule defined) - Automerge - At any time (no schedule defined) 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate CLI](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0NC40OC4xIiwidXBkYXRlZEluVmVyIjoiNDQuNDguMSIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==--> Co-authored-by: Davide Zarantonello <[email protected]>
Fixes #17707
Changes
content-modules.mjswas only ever appended to via#moduleImports, so deleting or renaming a content file withdeferredRenderleft a stale entry pointing at a non-existent file. This caused Vite to attempt to resolve the missing module (e.g. in dev, after removing an MDX/Markdown page).#rebuildModuleImports(), mirroring the existing#rebuildAssetImports()pattern used for the asset-imports stale-entry fix (astro dev does not recover from invalid content collection image path #16097). It clears#moduleImportsand rebuilds it from the current_collectionsstate.writeModuleImports()now calls#rebuildModuleImports()before writing, anddelete(),clear(), andclearAll()now also trigger a debounced module-imports rewrite (previously they only rewrote asset imports).Testing
Added two unit tests to
packages/astro/test/units/content-collections/mutable-data-store.test.ts:removes stale module imports when an entry is deletedremoves stale module imports when a collection is clearedRan the full
mutable-data-store.test.tssuite locally (8 tests, all passing):Docs
No user-facing API changes; no docs needed.