Repository: flutter
Issue: [web] DeferredLoadException after deploying a new version of app
Labels: c: crash,platform-web,a: release,P2,needs repro info,team-web,triaged-web

### Is there an existing issue for this?

- [X] I have searched the [existing issues](https://github.com/flutter/flutter/issues)
- [X] I have read the [guide to filing a bug](https://flutter.dev/docs/resources/bug-reports)

### Steps to reproduce

This is the same issue as closed https://github.com/flutter/flutter/issues/121210
When deferred loading is used, Flutter generates multiple js chunks, e.g. main.dart.js, main.dart.js_1.part.js, main.dart.js_2.part.js, ...
If browser has the previous versions of these chunks in the cache, it will just use them without fetching new versions from a server.
This issue can be workarounded by different ways, but the best one is to fix how part.js are loaded from main.dart.js by replacing 
**().createScriptURL(l+k) to ().createScriptURL(l+k+"?v=N.N.NN")**
That change will enforce the browser to always load part.js files when a new version is deployed.
Currently possible workarounds:
1. Ask users to clear the browser cache manually when a new version of app is deployed - totally "ugly" and inconvenient.
2. Add cache control attributes on the hosting server, i.e. at least no-cache and etag, but that will force the browser to ask a server every time and get 304 responses in most of the cases - so slow down considering that usually there are hundreds of part.js files.
3. Currently after 'flutter build web' I'm using the following post-processing script to fix this caching problem, but actually Flutter has to have this functionality embedded:

```bash
#!/bin/bash

# Based on: https://github.com/doonfrs/flutter-build-web-cache-problem/blob/main/build-web.sh
# and https://www.reddit.com/r/FlutterDev/comments/1euenbd/avoiding_caching_issues_after_a_new_web_flutter/
#
# Run only ONCE after 'flutter build web' as: ./prepare.sh
# Expected folder structure:
#   build 
#     web
#   prepare.sh
#   pubspec.yaml

echo "Reading version from pubspec.yaml without + sign"
version=$(grep version: pubspec.yaml | sed 's/version: //g' | sed 's/+//g')
echo "$version"

echo
echo "Calculating MD5"
md5_0=($(md5sum build/web/main.dart.js))
echo "main.dart.js   $md5_0"

md5_1=($(md5sum build/web/flutter.js))
echo "flutter.js   $md5_1"

md5_2=($(md5sum build/web/index.html))
echo "index.html   $md5_2"

md5_3=($(md5sum build/web/flutter_bootstrap.js))
echo "flutter_bootstrap.js   $md5_3"
echo

echo "Patching version in js partial urls in main.dart.js"
sed -i 's/\.createScriptURL([[:graph:]]\++[[:space:]]*\+[[:graph:]]/&+"?v='"$version"'"/g' build/web/main.dart.js

echo "Patching main.dart.js path in flutter.js and flutter_bootstrap.js (Flutter 3.22, 3.24)"
sed -i 's|\(${w}main\.dart\.js\)|\1?v='"$version"'|g' build/web/flutter.js
sed -i 's|\(??"main\.dart\.js\)|\1?v='"$version"'|g' build/web/flutter.js
sed -i 's|\(:"main\.dart\.js\)|\1?v='"$version"'|g' build/web/flutter.js

sed -i 's|\(${w}main\.dart\.js\)|\1?v='"$version"'|g' build/web/flutter_bootstrap.js
sed -i 's|\(??"main\.dart\.js\)|\1?v='"$version"'|g' build/web/flutter_bootstrap.js
sed -i 's|\(:"main\.dart\.js\)|\1?v='"$version"'|g' build/web/flutter_bootstrap.js

echo "Patching main.dart.js path in flutter.js (Flutter 3.19)"
sed -i 's|\(${l}main\.dart\.js\)|\1?v='"$version"'|g' build/web/flutter.js

echo "Patching main.dart.js path in flutter.js (Flutter 3.7, 3.10, 3.13, 3.16)"
sed -i 's|\(${baseUri}main\.dart\.js\)|\1?v='"$version"'|g' build/web/flutter.js

echo "Patching main.dart.js path in flutter.js (for obsolete Flutter 3.3)"
sed -i 's|\(= "main\.dart\.js\)|\1?v='"$version"'|g' build/web/flutter.js

echo "Patching index.html for flutter.js"
sed -i 's/"flutter\.js"/"flutter.js?v='"$version"'"/' build/web/index.html

echo "Patching index.html for flutter.js"
sed -i 's/"flutter_bootstrap\.js"/"flutter_bootstrap.js?v='"$version"'"/' build/web/index.html

echo "Patching assets loader with v= in main.dart.js"
sed -i "s/self\.window\.fetch(a),/self.window.fetch(a + '?v=$version'),/g" build/web/main.dart.js

# --profile unobfuscated code: 
# fontFace = A.createDomFontFace(family, "url(" + t1.getAssetUrl$1(asset) + ")", descriptors);
#
echo "Patching html font loading with v= in main.dart.js"
sed -i 's/"url("+[[:graph:]]\++"/&?v='"$version"'/g' build/web/main.dart.js

echo "Adding v= to manifest.json"
sed -i 's/"manifest.json"/"manifest.json?v='"$version"'"/' build/web/index.html

echo "Adding v= to favicon.png"
sed -i 's/"favicon.png"/"favicon.png?v='"$version"'"/' build/web/index.html

echo
echo "Calculating new MD5"
md5_n0=($(md5sum build/web/main.dart.js))
echo "main.dart.js   $md5_n0"

md5_n1=($(md5sum build/web/flutter.js))
echo "flutter.js   $md5_n1"

md5_n2=($(md5sum build/web/index.html))
echo "index.html   $md5_n2"

md5_n3=($(md5sum build/web/flutter_bootstrap.js))
echo "flutter.js   $md5_n1"
echo

echo "Patching MD5 in flutter_service_worker.js"
sed -i 's/'"$md5_0"'/'"$md5_n0"'/' build/web/flutter_service_worker.js
sed -i 's/'"$md5_1"'/'"$md5_n1"'/' build/web/flutter_service_worker.js
sed -i 's/'"$md5_2"'/'"$md5_n2"'/' build/web/flutter_service_worker.js
sed -i 's/'"$md5_3"'/'"$md5_n3"'/' build/web/flutter_service_worker.js

```

### Expected results

Version query parameter has to be added to all js files generated during compilation.

### Actual results

DeferredLoadException: 'Loading main.dart.js_1.part.js failed: the code with hash ...'

### Code sample

<details><summary>Code sample</summary>

```dart
[Paste your code here]
```

</details>


### Screenshots or Video

**That is how loading looks currently in Flutter:**

![image](https://github.com/flutter/flutter/assets/4129763/575a57f9-d6ad-4f53-a987-f8cb2e651762)

**That is how it has to be to prevent invalid browser caching and DeferredLoadException:**

![image](https://github.com/flutter/flutter/assets/4129763/578343b2-2cdb-4cc6-a488-80bb70b86593)



### Logs

<details><summary>Logs</summary>

```console
[Paste your logs here]
```

</details>


### Flutter Doctor output

<details><summary>Doctor output</summary>

```console
[√] Flutter (Channel stable, 3.7.12, on Microsoft Windows [Version 10.0.19045.2965], locale en-US)
    • Flutter version 3.7.12 on channel stable at c:\Programs\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 4d9e56e694 (5 weeks ago), 2023-04-17 21:47:46 -0400
    • Engine revision 1a65d409c7
    • Dart version 2.19.6
    • DevTools version 2.20.1

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at C:\Users\slavap\AppData\Local\Android\sdk
    • Platform android-31, build-tools 31.0.0
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 11.0.12+7-b1504.28-7817840)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • CHROME_EXECUTABLE = c:\Programs\chrome-debug.bat

[X] Visual Studio - develop for Windows
    X Visual Studio not installed; this is necessary for Windows development.
      Download at https://visualstudio.microsoft.com/downloads/.
      Please install the "Desktop development with C++" workload, including all of its default components

[√] Android Studio (version 2021.2)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.12+7-b1504.28-7817840)

[√] VS Code (version 1.78.2)
    • VS Code at C:\Users\slavap\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.64.0

[√] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.19045.2965]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 113.0.5672.127
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 113.0.1774.35

[√] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 1 category.
```

</details>