mirror of
https://github.com/withastro/astro.git
synced 2024-12-16 21:46:22 -05:00
Fix injection behavior for pages which contain no elements (#638)
* chore: add changeset * fix(#605): inject HMR/styles even when page includes no elements * chore: update test description
This commit is contained in:
parent
a9f2f6f6f9
commit
d93f768c8c
7 changed files with 57 additions and 1 deletions
14
.changeset/twelve-lions-tie.md
Normal file
14
.changeset/twelve-lions-tie.md
Normal file
|
@ -0,0 +1,14 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Add support for components defined in Frontmatter. Previously, the following code would throw an error. Now it is officially supported!
|
||||
|
||||
```astro
|
||||
---
|
||||
const { level = 1 } = Astro.props;
|
||||
const Element = `h${level}`;
|
||||
---
|
||||
|
||||
<Element>Hello world!</Element>
|
||||
```
|
|
@ -24,7 +24,8 @@ export default function (opts: TransformOptions): Transformer {
|
|||
if (hasComponents) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Initialize eoh if there are no elements
|
||||
eoh.enter(node);
|
||||
if (node.attributes && node.attributes.some(({ name }: any) => name.startsWith('client:'))) {
|
||||
hasComponents = true;
|
||||
return;
|
||||
|
@ -36,6 +37,9 @@ export default function (opts: TransformOptions): Transformer {
|
|||
hasComponents = true;
|
||||
}
|
||||
},
|
||||
leave(node) {
|
||||
eoh.leave(node);
|
||||
},
|
||||
},
|
||||
Element: {
|
||||
enter(node) {
|
||||
|
|
|
@ -39,4 +39,14 @@ HMR('Adds script to static pages too', async ({ runtime }) => {
|
|||
assert.ok(/window\.HMR_WEBSOCKET_PORT/.test(html), 'websocket port added');
|
||||
});
|
||||
|
||||
HMR('Adds script to pages even if there aren\'t any elements in the template', async ({ runtime }) => {
|
||||
const result = await runtime.load('/no-elements');
|
||||
if (result.error) throw new Error(result.error);
|
||||
|
||||
const html = result.contents;
|
||||
const $ = doc(html);
|
||||
assert.equal($('[src="/_snowpack/hmr-client.js"]').length, 1);
|
||||
assert.ok(/window\.HMR_WEBSOCKET_PORT/.test(html), 'websocket port added');
|
||||
});
|
||||
|
||||
HMR.run();
|
||||
|
|
8
packages/astro/test/fixtures/astro-hmr/src/components/Demo.astro
vendored
Normal file
8
packages/astro/test/fixtures/astro-hmr/src/components/Demo.astro
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>My Test</title>
|
||||
</head>
|
||||
<body>
|
||||
<div>Hello world</div>
|
||||
</body>
|
||||
</html>
|
4
packages/astro/test/fixtures/astro-hmr/src/pages/no-elements.astro
vendored
Normal file
4
packages/astro/test/fixtures/astro-hmr/src/pages/no-elements.astro
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
---
|
||||
import Demo from '../components/Demo.astro';
|
||||
---
|
||||
<Demo />
|
6
packages/astro/test/fixtures/no-head-el/src/pages/no-elements.astro
vendored
Normal file
6
packages/astro/test/fixtures/no-head-el/src/pages/no-elements.astro
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
import Something from '../components/Something.jsx';
|
||||
import Child from '../components/Child.astro';
|
||||
---
|
||||
<Something client:load />
|
||||
<Child />
|
|
@ -25,4 +25,14 @@ NoHeadEl('Places style and scripts before the first non-head element', async ({
|
|||
assert.equal($('script[src="/_snowpack/hmr-client.js"]').length, 1, 'Only the hmr client for the page');
|
||||
});
|
||||
|
||||
NoHeadEl('Injects HMR script even when there are no elements on the page', async ({ runtime }) => {
|
||||
const result = await runtime.load('/no-elements');
|
||||
if (result.error) throw new Error(result.error);
|
||||
|
||||
const html = result.contents;
|
||||
const $ = doc(html);
|
||||
|
||||
assert.equal($('script[src="/_snowpack/hmr-client.js"]').length, 1, 'Only the hmr client for the page');
|
||||
});
|
||||
|
||||
NoHeadEl.run();
|
||||
|
|
Loading…
Reference in a new issue