Unverified Commit 3bc07c1c authored by Kevin Moore's avatar Kevin Moore Committed by GitHub

flutter-tool, web: update HTML template serviceWorkerVersion to be const (#124826)

flutter-tool, web: update HTML template serviceWorkerVersion to be const
parent 4c41c0db
......@@ -70,8 +70,9 @@ class IndexHtml {
if (serviceWorkerVersion != null) {
_content = _content
.replaceFirst(
'var serviceWorkerVersion = null',
'var serviceWorkerVersion = "$serviceWorkerVersion"',
// Support older `var` syntax as well as new `const` syntax
RegExp('(const|var) serviceWorkerVersion = null'),
'const serviceWorkerVersion = "$serviceWorkerVersion"',
)
// This is for legacy index.html that still uses the old service
// worker loading mechanism.
......
......@@ -34,7 +34,7 @@
<script>
// The value below is injected by flutter build, do not touch.
var serviceWorkerVersion = null;
const serviceWorkerVersion = null;
</script>
<!-- This script adds the flutter initialization JS code -->
<script src="flutter.js" defer></script>
......
......@@ -31,6 +31,28 @@ const String htmlSample2 = '''
<meta charset="utf-8">
<link rel="icon" type="image/png" href="favicon.png"/>
</head>
<body>
<div></div>
<script src="main.dart.js"></script>
<script>
const serviceWorkerVersion = null;
</script>
<script>
navigator.serviceWorker.register('flutter_service_worker.js');
</script>
</body>
</html>
''';
const String htmlSampleLegacyVar = '''
<!DOCTYPE html>
<html>
<head>
<title></title>
<base href="$kBaseHrefPlaceholder">
<meta charset="utf-8">
<link rel="icon" type="image/png" href="favicon.png"/>
</head>
<body>
<div></div>
<script src="main.dart.js"></script>
......@@ -61,7 +83,7 @@ String htmlSample2Replaced({
<div></div>
<script src="main.dart.js"></script>
<script>
var serviceWorkerVersion = "$serviceWorkerVersion";
const serviceWorkerVersion = "$serviceWorkerVersion";
</script>
<script>
navigator.serviceWorker.register('flutter_service_worker.js?v=$serviceWorkerVersion');
......@@ -127,6 +149,21 @@ void main() {
);
});
test('applies substitutions with legacy var version syntax', () {
final IndexHtml indexHtml = IndexHtml(htmlSampleLegacyVar);
indexHtml.applySubstitutions(
baseHref: '/foo/333/',
serviceWorkerVersion: 'v123xyz',
);
expect(
indexHtml.content,
htmlSample2Replaced(
baseHref: '/foo/333/',
serviceWorkerVersion: 'v123xyz',
),
);
});
test('re-parses after substitutions', () {
final IndexHtml indexHtml = IndexHtml(htmlSample2);
expect(indexHtml.getBaseHref(), ''); // Placeholder base href.
......
......@@ -79,7 +79,7 @@ found in the LICENSE file. -->
application. For more information, see:
https://developers.google.com/web/fundamentals/primers/service-workers -->
<script>
var serviceWorkerVersion = null;
const serviceWorkerVersion = null;
var scriptLoaded = false;
function loadMainDartJs() {
if (scriptLoaded) {
......@@ -165,7 +165,7 @@ found in the LICENSE file. -->
<link rel="manifest" href="manifest.json">
<script>
// The value below is injected by flutter build, do not touch.
var serviceWorkerVersion = null;
const serviceWorkerVersion = null;
</script>
<!-- This script adds the flutter initialization JS code -->
<script src="flutter.js" defer></script>
......
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