# Content hashing for Parcel v3: implementation plan Status: proposed · Branch: `v3-content-hashing` · Base: `v3` @ `8bb32a53a` ## Goal Put a hash of each bundle's final bytes in the file name of every non-stable bundle, so outputs can be served with immutable long-term cache headers. Requirements: - Correct for every language that embeds references (JS, CSS, HTML, SVG, RSC manifests), for inline bundles (including base64 data URLs), and for any optimizer, first- or third-party. - Source maps are always correct. - There are no text sentinels to search for, so escaping, minifiers and encodings cannot break the scheme. - Invalidation cascades as little as possible: changing one module renames that bundle, plus only the bundles that literally embed its name. - Incremental rebuilds re-package only what actually changed. ## Chosen approach (summary) 1. **Package in dependency order, with final names.** A bundle is packaged only after every bundle whose _final name_ it embeds has been hashed and named. Its hash is then a plain hash of its final output bytes. There are no placeholders to replace, and nothing to scan for or remap. Every optimizer and encoding just works, and source maps are generated from final text. 2. **Two passes only for cycles.** When bundles embed each other's names in a cycle, each member is packaged once with provisional (template) names. One composite hash is taken over the pass-1 outputs, and the members are packaged again with the final names. 3. **Declare bundle dependencies up front, as a contract.** Before packaging, each content type declares which bundles' names (and which inline bundles' contents) it will access. That list drives ordering, cycle detection and incremental invalidation. Name accessors enforce it: reading the name of a bundle that wasn't declared is an error, not a silently wrong name. 4. **Route most references through stable keys, not final names.** JS loads bundles through the runtime (`loadJS`, `loadCSS`, `resolve`) using a _stable key_ (the template name). A manifest mapping `stable key → final URL` is held once, at the top of each execution context: inline in the HTML page, in a stable-named entry, or in a worker entry. Hashed chunks never embed other chunks' final names, so there is no cascade between chunks. The research behind this choice (Rollup, Rolldown, esbuild, webpack, Turbopack, Vite, Farm) is in the session notes. In short: - Sentinel replacement (v2, Rollup) breaks under escaping and under anything that rewrites text. - Content changed after hashing (Vite's `__vite__mapDeps`, Rollup's sourcemap comment) produces the same name for different bytes. - Chunk-to-chunk URLs cascade, whereas a manifest or import map held at the root does not. ## Core invariant > A hashed bundle's output bytes are a deterministic function of (its inputs, the final names it read). > Its hash covers exactly those bytes plus (for cycle members) the pass-1 composite. Nothing > modifies a bundle's bytes after its hash is computed. Every step below exists to uphold this. `debug_assertions` builds check it (see Testing). --- ## Current state (what we're changing) | Area | Today | Location | | ---------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------ | | Naming | Namer runs once, right after bundling, and sets `dist_path`. Non-stable names are `{stem}-{bundle.id:016x}.{ext}`. `bundle.id` is structural (asset identity), not content. | `crates/core/src/bundler.rs:26-34`, `crates/parcel/src/namer.rs:116-123` | | Name access | Packagers call `Bundle::relative_url(from)`, `relative_specifier(from)`, `name()`, `absolute_url()`. There is no tracking. | `crates/core/src/bundle.rs:44-79`; ~30 call sites (JS, CSS, HTML, RSC, library, HMR) | | Packaging | Flat `par_iter` over dirty bundles. Inline bundles are packaged on demand and cached. Optimizers run inside `get_bundle_content`. | `crates/core/src/lib.rs:386-654` | | Incremental | `prev_bundles` is keyed by dist path. **If any output path changes, every bundle is re-packaged.** | `lib.rs:401-476` | | Runtime manifest | `runtime.js` resolves through `importMap[id] \|\| id` and exposes `extendImportMap`, but **nothing fills it**. | `crates/js/src/runtime.js:85-146` | | Cross-chunk JS | `write_bundle_references` emits a static `import "./x.js"` in every bundle with `referenced_bundles` (entries, lazy, shared, worker). The async shim loads only _direct_ references (`// TODO: recursive`). | `crates/js/src/packager/mod.rs:304-325`, `synthetic.rs:270-314` | | Content hash | None exists. `Content::hash` has no callers, and `FileContent::hash` hashes the _path_. | `crates/core/src/content.rs:71-74,191-193` | | Options | No `content_hash` option or flag. | `crates/core/src/options.rs`, `crates/parcel/src/main.rs` | | Source maps | `.map` is written beside the bundle. No `sourceMappingURL` comment is emitted. | `content.rs:315-319` | Pre-existing bugs found while mapping this (fixed in Phase 0): 1. **HTML embeds a URL to an inline bundle.** `prepare_to_package` checks only `dep.bundle_behavior == Inline`, not the target's behavior. So `` gets the relative URL of a bundle that is never written. CSS checks both. (`crates/html/src/lib.rs:477-483`) 2. **`loadCSS` uses a path relative to the importing bundle** (`'./' + relative_url(from)`), but `link.href` resolves against the _document_. This breaks when the bundle and the page are in different directories. (`synthetic.rs:331-347`, `runtime.js:96-137`) 3. **Service workers are not stable-named.** Today they get `sw-.js`, which only works because ids are structural. With content hashing, the registration URL would change on every edit. (`crates/js/src/transformer.rs:303-312`: no `NEEDS_STABLE_NAME`) 4. **Inline CSS (a `