Skip to content

Commit ace3224

Browse files
authored
[Website] Drop the static <link rel="manifest"> tag and generate one in JavaScript instead (#78)
#67 introduced JavaScript logic to update the URL in the `<link rel="manifest">` tag. This PR removes the server-rendered `<link>` tag entirely and generates it in JavaScript instead. I noticed Safari sometimes ignores the runtime updates to the URL of an existing manifest tag. At the same time, it seems to respect the URL of a freshly created manifest. ## Testing instructions In Safari, Chrome, and Firefox: Build Playground, run it via a PHP dev server on a https domain (I've used ngrok), start it with a Blueprint and a custom `?app_name` parameter, and confirm you can install it as a named PWA that runs that Blueprint on boot.
1 parent a3fe1d0 commit ace3224

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

packages/playground/website/index.html

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,31 @@
2121
<meta name="viewport" content="width=device-width, initial-scale=1" />
2222
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
2323
<link rel="stylesheet" href="/src/styles.css" />
24-
<link rel="manifest" href="/manifest.json" />
2524
<script>
26-
// Outside of the local dev server, use the dynamic manifest that
27-
// passes the query parameters through to the "start_url" property.
25+
/**
26+
* Dynamically create <link rel="manifest" /> to enable using Playground with
27+
* a Blueprint as an offline progressive web app.
28+
*
29+
* We don't have a static <link rel="manifest" /> element in the initial HTML
30+
* because Safari would just go with it and ignore the dynamic updates made below.
31+
* It has no problem with dynamically created manifest element, though.
32+
*
33+
* If this method ever fails, we'll need to rename index.html to index.php and
34+
* ensure the <link rel="manifest" /> element is shipped in the initial HTML.
35+
*/
36+
37+
/**
38+
* On the local dev server, use the static manifest.json file.
39+
* Outside of the local dev server, use the dynamic manifest that
40+
* passes the query parameters through to the "start_url" property.
41+
*/
42+
let manifestUrl = '/manifest.json';
43+
44+
/**
45+
* In production, load a manifest URL that includes the current URL
46+
* as the "start_url" parameter.
47+
*/
2848
if (window.location.port !== '5400') {
29-
let manifestUrl = null;
3049
const runningAsPWA = window.matchMedia(
3150
'(display-mode: standalone)'
3251
).matches;
@@ -58,10 +77,11 @@
5877
);
5978
}
6079
}
61-
62-
document.querySelector('link[rel="manifest"]').href =
63-
manifestUrl.toString();
6480
}
81+
const newLink = document.createElement('link');
82+
newLink.rel = 'manifest';
83+
newLink.href = manifestUrl.toString();
84+
document.head.appendChild(newLink);
6585
</script>
6686
<link rel="preconnect" href="https://fonts.googleapis.com" />
6787
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />

0 commit comments

Comments
 (0)