| 1 |
<!doctype html> |
| 2 |
<html> |
| 3 |
<head> |
| 4 |
<!-- As an optimization, immediately begin fetching the JavaScript modules |
| 5 |
that we know for sure we'll eventually need. It's important we don't |
| 6 |
execute them yet, though. --> |
| 7 |
<link rel="modulepreload" href="./_js/components.js" /> |
| 8 |
|
| 9 |
<!-- On browsers that don't yet support native declarative shadow DOM, a |
| 10 |
paint can occur after some or all pre-rendered HTML has been parsed, |
| 11 |
but before the declarative shadow DOM polyfill has taken effect. This |
| 12 |
paint is undesirable because it won't include any component shadow DOM. |
| 13 |
To prevent layout shifts that can result from this render, we use a |
| 14 |
"dsd-pending" attribute to ensure we only paint after we know |
| 15 |
shadow DOM is active. --> |
| 16 |
<style> |
| 17 |
body[dsd-pending] { |
| 18 |
display: none; |
| 19 |
} |
| 20 |
</style> |
| 21 |
</head> |
| 22 |
|
| 23 |
<body dsd-pending> |
| 24 |
<script> |
| 25 |
if (HTMLTemplateElement.prototype.hasOwnProperty('shadowRoot')) { |
| 26 |
// This browser has native declarative shadow DOM support, so we can |
| 27 |
// allow painting immediately. |
| 28 |
document.body.removeAttribute('dsd-pending'); |
| 29 |
} |
| 30 |
</script> |
| 31 |
|
| 32 |
<!-- Pre-rendered Lit components will be generated here. --> |
| 33 |
{{ content }} |
| 34 |
|
| 35 |
<!-- At this point, browsers with native shadow DOM support will already |
| 36 |
be able to paint the initial fully styled state your components, |
| 37 |
without executing a single line of JavaScript! However, the components |
| 38 |
aren't interactive yet -- that's what hydration is for. --> |
| 39 |
|
| 40 |
<!-- Use a type=module script so that we can use dynamic module imports. |
| 41 |
Note this pattern will not work in IE11. --> |
| 42 |
<script type="module"> |
| 43 |
(async () => { |
| 44 |
// Start fetching the Lit hydration support module (note the absence |
| 45 |
// of "await" -- we don't want to block yet). |
| 46 |
const litHydrateSupportInstalled = import( |
| 47 |
'@lit-labs/ssr-client/lit-element-hydrate-support.js' |
| 48 |
); |
| 49 |
|
| 50 |
// Check if we require the declarative shadow DOM polyfill. As of |
| 51 |
// February 2022, Chrome and Edge have native support, but Firefox |
| 52 |
// and Safari don't yet. |
| 53 |
if (!HTMLTemplateElement.prototype.hasOwnProperty('shadowRoot')) { |
| 54 |
// Fetch the declarative shadow DOM polyfill. |
| 55 |
const {hydrateShadowRoots} = await import( |
| 56 |
'@webcomponents/template-shadowroot/template-shadowroot.js' |
| 57 |
); |
| 58 |
|
| 59 |
// Apply the polyfill. This is a one-shot operation, so it is important |
| 60 |
// it happens after all HTML has been parsed. |
| 61 |
hydrateShadowRoots(document.body); |
| 62 |
|
| 63 |
// At this point, browsers without native declarative shadow DOM |
| 64 |
// support can paint the initial state of your components! |
| 65 |
document.body.removeAttribute('dsd-pending'); |
| 66 |
} |
| 67 |
|
| 68 |
// The Lit hydration support module must be installed before we can |
| 69 |
// load any component definitions. Wait until it's ready. |
| 70 |
await litHydrateSupportInstalled; |
| 71 |
|
| 72 |
// Load component definitions. As each component definition loads, your |
| 73 |
// pre-rendered components will come to life and become interactive. |
| 74 |
import('./_js/components.js'); |
| 75 |
})(); |
| 76 |
</script> |
| 77 |
</body> |
| 78 |
</html> |