[tool][web] Makes flutter.js more G3 friendly. (#120504)

* Allow any JS file in flutter loader.

* Nag only if service worker API is completely unavailable.

* Add info about Secure Contexts if that may be the reason why serviceworker is not available.

* Update sanity test.

* If service worker settings are null, do not even check if the API is available.
This commit is contained in:
David Iglesias 2023-02-21 12:51:37 -08:00 committed by GitHub
parent 06ccf55454
commit a463bb82c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 4 deletions

View File

@ -72,8 +72,7 @@ _flutter.loader = null;
*/
constructor(validPatterns, policyName = "flutter-js") {
const patterns = validPatterns || [
/\.dart\.js$/,
/^flutter_service_worker.js$/
/\.js$/,
];
if (window.trustedTypes) {
this.policy = trustedTypes.createPolicy(policyName, {
@ -116,10 +115,19 @@ _flutter.loader = null;
* @returns {Promise} that resolves when the latest serviceWorker is ready.
*/
loadServiceWorker(settings) {
if (!("serviceWorker" in navigator) || settings == null) {
if (settings == null) {
// In the future, settings = null -> uninstall service worker?
console.debug("Null serviceWorker configuration. Skipping.");
return Promise.resolve();
}
if (!("serviceWorker" in navigator)) {
let errorMessage = "Service Worker API unavailable.";
if (!window.isSecureContext) {
errorMessage += "\nThe current context is NOT secure."
errorMessage += "\nRead more: https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts";
}
return Promise.reject(
new Error("Service worker not supported (or configured).")
new Error(errorMessage)
);
}
const {

View File

@ -881,6 +881,8 @@ void main() {
flutter_js.generateFlutterJsFile(fileGeneratorsPath);
expect(flutterJsContents, contains('"use strict";'));
expect(flutterJsContents, contains('main.dart.js'));
expect(flutterJsContents, contains('if (!("serviceWorker" in navigator))'));
expect(flutterJsContents, contains(r'/\.js$/,'));
expect(flutterJsContents, contains('flutter_service_worker.js?v='));
expect(flutterJsContents, contains('document.createElement("script")'));
expect(flutterJsContents, contains('"application/javascript"'));