Skip to content

[web] Fix --web-content-hash breaking cache when --source-maps is used #193601

Description

@kevmoo

When building a Flutter web app with both --web-content-hash and --source-maps (e.g., flutter build web --wasm --web-content-hash --source-maps or flutter build web --web-content-hash --source-maps), build/web/precache_manifest.json records "urlHashed": false and a mismatched "hash" for main.dart.<hash>.js:

{
  "url": "main.dart.216ed961.js",
  "hash": "fca793b5",
  "size": 6079674,
  "urlHashed": false
}

Root Cause

  1. In _hashAndRenameWebOutput, flutter_tools computes contentHash (216ed961) from the initial main.dart.js bytes (when its trailing comment is still //# sourceMappingURL=main.dart.js.map), derives newBasename = 'main.dart.216ed961.js', and then rewrites the trailing sourceMappingURL directive inside main.dart.js in-place to //# sourceMappingURL=main.dart.216ed961.js.map before renaming the file on disk ( additionally stripping any trailing newline due to \s*$ matching \n before EOF).
  2. Later, during WebReleaseBundle.build, updatePrecacheManifest hashes the final build/web/main.dart.216ed961.js bytes on disk—which now contain the rewritten sourceMappingURL comment—producing a different hash (fca793b5).
  3. Because 'main.dart.216ed961.js'.contains('.fca793b5.') is false, updatePrecacheManifest sets "urlHashed": false and "hash": "fca793b5", breaking cache validation and precache manifest consumers that rely on urlHashed: true and filename/content hash parity.

Other Affected Files

  • main.dart.<hash>.js (Dart2JSTarget and the JS fallback in --wasm builds): Affected when --source-maps is passed.
  • main.dart.<hash>.mjs.map (Dart2WasmTarget): If a .mjs.map file is present alongside main.dart.mjs, Dart2WasmTarget previously derived jsSupportMapName = '$jsSupportName.map'.
  • flutter_service_worker.js / worker.<hash>.js: Uses _hashAndRenameWebOutput with sourceMapFile if present.
  • main.dart.<hash>.wasm: Not affected (_hashAndRenameWebOutput skips sourceMappingURL rewriting when isWasm is true because .wasm embeds main.dart.wasm.map in a binary custom section).

Proposed Fix (Hash Inversion)

In _hashAndRenameWebOutput (packages/flutter_tools/lib/src/build_system/targets/web.dart):

  1. Hash sourceMapFile first (mapHash) and rename it via computeHashedBasename(oldMapBasename, mapHash, file.fileSystem) (producing main.dart.<mapHash>.js.map, main.dart.<mapHash>.mjs.map, worker.<mapHash>.js.map).
  2. Rewrite //# sourceMappingURL=<newMapBasename> in file (using positive lookahead (?=\s*$) so trailing newlines are preserved).
  3. Compute contentHash from the final bytes of file and rename file to main.dart.<contentHash>.js so its filename hash matches its on-disk bytes and updatePrecacheManifest records "urlHashed": true with the matching "hash".
  4. Resolve mainJsMapName in Dart2JSTarget.buildFiles and jsSupportMapName in Dart2WasmTarget.buildFiles via _resolveHashedBasename.

Activity

  1. self-assigned this
    on Sep 30, 2026
  2. added
    P1High-priority issues at the top of the work list
    platform-webWeb applications specifically
    team-webOwned by Web platform team
    toolAffects the "flutter" command-line tool. See also t: labels.
    and removed
    platform-webWeb applications specifically
    team-webOwned by Web platform team
    on Sep 30, 2026
  3. added theissue type on Sep 30, 2026
  4. added a commit that references this issue on Oct 1, 2026
    9286bbf
  5. github-actions commented on Oct 15, 2026

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  6. locked as resolved and limited conversation to collaborators on Oct 15, 2026
  7. added a commit that references this issue on Oct 29, 2026
    097b34b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P1High-priority issues at the top of the work listtoolAffects the "flutter" command-line tool. See also t: labels.

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions