0
Fork 0
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:
Nate Moore 2021-07-09 09:46:19 -05:00 committed by GitHub
parent a9f2f6f6f9
commit d93f768c8c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 57 additions and 1 deletions

View 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>
```

View file

@ -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) {

View file

@ -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();

View file

@ -0,0 +1,8 @@
<html>
<head>
<title>My Test</title>
</head>
<body>
<div>Hello world</div>
</body>
</html>

View file

@ -0,0 +1,4 @@
---
import Demo from '../components/Demo.astro';
---
<Demo />

View file

@ -0,0 +1,6 @@
---
import Something from '../components/Something.jsx';
import Child from '../components/Child.astro';
---
<Something client:load />
<Child />

View file

@ -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();