Unverified Commit a463bb82 authored by David Iglesias's avatar David Iglesias Committed by GitHub

[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.
parent 06ccf554
...@@ -72,8 +72,7 @@ _flutter.loader = null; ...@@ -72,8 +72,7 @@ _flutter.loader = null;
*/ */
constructor(validPatterns, policyName = "flutter-js") { constructor(validPatterns, policyName = "flutter-js") {
const patterns = validPatterns || [ const patterns = validPatterns || [
/\.dart\.js$/, /\.js$/,
/^flutter_service_worker.js$/
]; ];
if (window.trustedTypes) { if (window.trustedTypes) {
this.policy = trustedTypes.createPolicy(policyName, { this.policy = trustedTypes.createPolicy(policyName, {
...@@ -116,10 +115,19 @@ _flutter.loader = null; ...@@ -116,10 +115,19 @@ _flutter.loader = null;
* @returns {Promise} that resolves when the latest serviceWorker is ready. * @returns {Promise} that resolves when the latest serviceWorker is ready.
*/ */
loadServiceWorker(settings) { loadServiceWorker(settings) {
if (!("serviceWorker" in navigator) || settings == null) { if (settings == null) {
// In the future, settings = null -> uninstall service worker? // 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( return Promise.reject(
new Error("Service worker not supported (or configured).") new Error(errorMessage)
); );
} }
const { const {
......
...@@ -881,6 +881,8 @@ void main() { ...@@ -881,6 +881,8 @@ void main() {
flutter_js.generateFlutterJsFile(fileGeneratorsPath); flutter_js.generateFlutterJsFile(fileGeneratorsPath);
expect(flutterJsContents, contains('"use strict";')); expect(flutterJsContents, contains('"use strict";'));
expect(flutterJsContents, contains('main.dart.js')); 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('flutter_service_worker.js?v='));
expect(flutterJsContents, contains('document.createElement("script")')); expect(flutterJsContents, contains('document.createElement("script")'));
expect(flutterJsContents, contains('"application/javascript"')); expect(flutterJsContents, contains('"application/javascript"'));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment