-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds
<Template />
to `@rules_prerender/declarative_shadow_dom/preac…
…t.mjs`. This is a replacement for the `<Template />` element in `@rules_prerender/preact` but with the notable difference that it automatically includes the declarative shadow DOM polyfill when `shadowrootmode` is set. This removes the need to remember to do this for every component. This only works because scripts are loaded in the same order they are injected into the HTML (when they don't import each other). As a result, as long as the DSD polyfill is included first, it will be executed first. `<Template />` enforces that a little better than users can, but ultimately we are relying on that ordering to do the right thing. I'm not sure happy with that, but I think it is the nature of polyfills to a certain extent, especially when the dependency on DSD is the prerendered HTML, not the client-side JS where an `import` would solve this problem. For now, I think this is the best we can do. `<Template />` from `@rules_prerender/preact` still exists for now, but will be removed after all internal usages of it are migrated off.
- Loading branch information
Showing
4 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { renderToString } from 'preact-render-to-string'; | ||
import { serialize } from '../../common/models/prerender_annotation.mjs'; | ||
import { Template } from './preact.mjs'; | ||
|
||
describe('preact', () => { | ||
describe('Template', () => { | ||
it('renders a `<template>` element', () => { | ||
const template = Template({ children: 'Hello, World!' }); | ||
|
||
const html = renderToString(template); | ||
|
||
expect(html).toBe('<template>Hello, World!</template>'); | ||
}); | ||
it('renders a `<template>` element with the DSD polyfill when `shadowrootmode` is given', () => { | ||
const template = Template({ shadowrootmode: 'open' }); | ||
|
||
const html = renderToString(template); | ||
|
||
expect(html).toContain('<template shadowrootmode="open">'); | ||
expect(html).toContain(` | ||
<rules_prerender:annotation>${ | ||
serialize({ | ||
type: 'script', | ||
path: 'packages/declarative_shadow_dom/declarative_shadow_dom_polyfill.mjs', | ||
}).replaceAll('"', '"') | ||
}</rules_prerender:annotation> | ||
`.trim()); | ||
}); | ||
|
||
it('renders a `<template>` element with other attributes passed through', () => { | ||
const template = Template({ hidden: true }); | ||
|
||
const html = renderToString(template); | ||
|
||
expect(html).toBe('<template hidden></template>'); | ||
}); | ||
|
||
it('renders DSD polyfill *before* children', () => { | ||
// NOTE: It is very import to render the DSD polyfill *before* | ||
// children because the bundler will generate entry points in the | ||
// same order. If children came first, components would bootstrap | ||
// before the DSD polyfill had executed. | ||
const template = Template({ | ||
shadowrootmode: 'open', | ||
children: 'Hello, World!', | ||
}); | ||
|
||
const html = renderToString(template); | ||
|
||
const annotationIndex = html.indexOf(` | ||
<rules_prerender:annotation>${ | ||
serialize({ | ||
type: 'script', | ||
path: 'packages/declarative_shadow_dom/declarative_shadow_dom_polyfill.mjs', | ||
}).replaceAll('"', '"') | ||
}</rules_prerender:annotation> | ||
`.trim()); | ||
expect(annotationIndex).not.toBe(-1); | ||
|
||
const childrenIndex = html.indexOf('Hello, World!'); | ||
expect(childrenIndex).not.toBe(-1); | ||
|
||
expect(annotationIndex).toBeLessThan(childrenIndex); | ||
}); | ||
}); | ||
}); |