Skip to content

[flutter_tools][web] PWA ServiceWorker breaks on GitHub Pages (rootPath rewrite issue with url-params?) #68449

@JELaVallee

Description

@JELaVallee

Steps to Reproduce

  1. Run flutter create flutter-pwa-bug
  2. Create a GitHub repo and push the project to it
  3. Create a gh-pages empty branch and push to GitHub repo
  4. Configure the GitHub repo to use the gh-pages branch as the GitHub Pages root for the repo
  5. Update the BASE tag's href attribute in {project-root}/web/index.html to be /{gh-repo-name}/
  6. In the flutter-pwa-bug project, run flutter build --release
  7. Push the contents of {project-root}/build/web/ to the gh-pages branch
  8. Open the GitHub Pages URL in Chrome browser

I've made a repository to demonstrate this issue here: https://github.com/JELaVallee/hello-flutter-pwa
With the deployment of such here: https://jelavallee.github.io/hello-flutter-pwa

Expected results:

  1. In the Chrome browser, the web site loads and the application is functional
  2. In the Chrome browser's location field the "+" icon is displayed
  3. Clicking the "+" icon opens the "Install app?" popup dialog and allows you to install the PWA to your computer

Actual results:

  1. The page loads and the application functions as expected
  2. The "+" icon is not displayed in the browser's location field
  3. In the Chrome Developer Tools, Console pane, there is the error: "Uncaught (in promise) TypeError: Request failed ... flutter_service_worker.js?v={cache-buster}"
  4. In the Chrome Developer Tools, Network pane, there is a 404 error for the request: "https://.github.io/?revision={app-cache-buster}" which is thrown by the flutter_service_worker.js when it's loading the app-cache.

Triage Notes

While working to narrow down this issue, I've made the following observations:

  1. Hard-coding of "/" as one of the app-cache keys here: web.dart:Line 438
    1. In a site deployment like GitHub Pages (w/o a CNAME binding, of course) this absolute root path isn't what we want
    2. This cannot be easily over-ridden and isn't respecting the BASE:href attribute in the index.html
    3. I've been able to hack this to work in the generated flutter_service_worker.js as seen in this commit which is not currently deployed...
  2. The "fetch" call in flutter_service_worker.js (generated in web.dart:Line 633 is throwing errors which yields the "Uncaught (in promise) TypeError" documented above on the 404.
  3. GitHub Pages does not rewrite the rootPath URL to "index.html" when the URL has params applied to it.
    1. When tested on a GitHub Pages site with a bound CNAME record, the request to "https://{domain}/?revision={app-cache-buster}" the server still returns a 404 error response. (EDIT: This was a bad test on my part, GitHub Pages handles these rewrites properly...)

Suggesting that we either make "/" key in observation-1 be parsed from the BASE:href in the coded {project-root}/web/index.html or making the cache loading/checking code in the generated flutter_service_worker.js a bit more defensive when handling response errors vs. just throwing them up-stack.

Having one heck of a time properly testing this in the web_test harness. Going to look at a flutter_service_worker.js error handling fix first since it seems less "intrusive", but thinking that a fix for observation-1 would be ideal.

Possibly related to #50580 or #52901

Logs
$ flutter build web --release --verbose
[ +125 ms] executing: [/home/jelavallee/workspace/sandbox/flutter/flutter/] git -c log.showSignature=false log -n 1 --pretty=format:%H
[  +62 ms] Exit code 0 from: git -c log.showSignature=false log -n 1 --pretty=format:%H
[        ] 4fa4f91d5cc65a5a98c7ccd91bb7d47814048a57
[        ] executing: [/home/jelavallee/workspace/sandbox/flutter/flutter/] git tag --points-at HEAD
[  +30 ms] Exit code 0 from: git tag --points-at HEAD
[        ] 1.23.0-13.0.pre
[  +17 ms] executing: [/home/jelavallee/workspace/sandbox/flutter/flutter/] git rev-parse --abbrev-ref --symbolic @{u}
[  +18 ms] Exit code 0 from: git rev-parse --abbrev-ref --symbolic @{u}
[        ] origin/dev
[        ] executing: [/home/jelavallee/workspace/sandbox/flutter/flutter/] git ls-remote --get-url origin
[   +4 ms] Exit code 0 from: git ls-remote --get-url origin
[        ] https://github.com/flutter/flutter.git
[ +160 ms] executing: [/home/jelavallee/workspace/sandbox/flutter/flutter/] git rev-parse --abbrev-ref HEAD
[  +19 ms] Exit code 0 from: git rev-parse --abbrev-ref HEAD
[        ] dev
[ +174 ms] Artifact Instance of 'AndroidMavenArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'AndroidGenSnapshotArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'AndroidInternalBuildArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'IOSEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterWebSdk' is not required, skipping update.
[   +3 ms] Artifact Instance of 'WindowsEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'MacOSEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'LinuxEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'LinuxFuchsiaSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'MacOSFuchsiaSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterRunnerSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterRunnerDebugSymbols' is not required, skipping update.
[ +113 ms] Artifact Instance of 'MaterialFonts' is not required, skipping update.
[        ] Artifact Instance of 'GradleWrapper' is not required, skipping update.
[        ] Artifact Instance of 'AndroidMavenArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'AndroidGenSnapshotArtifacts' is not required, skipping update.
[   +4 ms] Artifact Instance of 'AndroidInternalBuildArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'IOSEngineArtifacts' is not required, skipping update.
[   +1 ms] Artifact Instance of 'FlutterSdk' is not required, skipping update.
[        ] Artifact Instance of 'WindowsEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'MacOSEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'LinuxEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'LinuxFuchsiaSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'MacOSFuchsiaSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterRunnerSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterRunnerDebugSymbols' is not required, skipping update.
[        ] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FontSubsetArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'PubDependencies' is not required, skipping update.
[ +233 ms] Generating
/home/jelavallee/workspace/sandbox/hello-flutter-pwa/android/app/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java
[ +129 ms] Generating
/home/jelavallee/workspace/sandbox/hello-flutter-pwa/android/app/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java
[   +5 ms] Compiling lib/main.dart for the Web...
[   +4 ms] Initializing file store
[   +7 ms] Done initializing file store
[ +106 ms] Skipping target: gen_localizations
[  +66 ms] Skipping target: web_entrypoint
[ +569 ms] Skipping target: dart2js
[  +48 ms] web_release_bundle: Starting due to {InvalidatedReason.outputMissing}
[ +168 ms] web_release_bundle: Complete
[ +100 ms] web_service_worker: Starting due to {InvalidatedReason.inputChanged, InvalidatedReason.outputMissing}
[  +80 ms] web_service_worker: Complete
[  +10 ms] Persisting file store
[  +15 ms] Done persisting file store
[  +57 ms] Compiling lib/main.dart for the Web... (completed in 1.2s)
[   +2 ms] "flutter web" took 1,757ms.
[ +112 ms] ensureAnalyticsSent: 108ms
[  +62 ms] Running shutdown hooks
[        ] Shutdown hooks complete
[        ] exiting with code 0
 $ flutter analyze
Analyzing hello-flutter-pwa...                                          
No issues found! (ran in 2.4s)
$ flutter doctor -v
[✓] Flutter (Channel dev, 1.23.0-13.0.pre, on Linux, locale en_US.UTF-8)
    • Flutter version 1.23.0-13.0.pre at /home/jelavallee/workspace/sandbox/flutter/flutter
    • Framework revision 4fa4f91d5c (12 days ago), 2020-10-06 12:31:25 -0700
    • Engine revision 443cd5a1e1
    • Dart version 2.11.0 (build 2.11.0-186.0.dev)

 
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at /home/jelavallee/Android/Sdk
    • Platform android-29, build-tools 29.0.2
    • ANDROID_HOME = /home/jelavallee/Android/Sdk
    • Java binary at: /opt/android-studio/jre/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • CHROME_EXECUTABLE = /usr/bin/chromium

[✓] Android Studio (version 3.5)
    • Android Studio at /opt/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 1.8.0_202-release-1483-b49-5587405)

[✓] VS Code (version 1.50.0)
    • VS Code at /usr/share/code
    • Flutter extension version 3.15.1

[✓] Connected device (3 available)
    • nami (mobile)    • emulator-5554 • android-x64    • Android 9 (API 28)
    • Web Server (web) • web-server    • web-javascript • Flutter Tools
    • Chrome (web)     • chrome        • web-javascript • Chromium 83.0.4103.116 built on Debian 10.4, running on Debian 10.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work lista: releaseChallenges faced when attempting to productionize an appengineflutter/engine related. See also e: labels.found in release: 1.23Found to occur in 1.23found in release: 1.24Found to occur in 1.24has reproducible stepsThe issue has been confirmed reproducible and is ready to work onplatform-webWeb applications specifically

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions