Skip to content

Commit 2a27aca

Browse files
HiDeooematipico
andauthored
fix: head content with component first (#1098)
Co-authored-by: ematipico <[email protected]>
1 parent 7331146 commit 2a27aca

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

.changeset/curvy-poets-jog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@astrojs/compiler": patch
3+
---
4+
5+
Fixes a potential parsing issue with head content defined in a component where another component is rendered first.

internal/parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1509,7 +1509,7 @@ func inBodyIM(p *parser) bool {
15091509
case a.Br:
15101510
p.tok.Type = StartTagToken
15111511
return false
1512-
case a.Template:
1512+
case a.Base, a.Basefont, a.Bgsound, a.Link, a.Meta, a.Noframes, a.Script, a.Style, a.Template, a.Title:
15131513
return inHeadIM(p)
15141514
default:
15151515
p.inBodyEndTagOther(p.tok.DataAtom, p.tok.Data)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
[TestPrinter/head_content_with_component_first - 1]
3+
## Input
4+
5+
```
6+
/-/-/-/
7+
import Analytics from '../components/Analytics.astro';
8+
/-/-/-/
9+
<Analytics />
10+
<title>{title}</title>
11+
<meta name="description" content="a description" />
12+
```
13+
14+
## Output
15+
16+
```js
17+
import {
18+
Fragment,
19+
render as $$render,
20+
createAstro as $$createAstro,
21+
createComponent as $$createComponent,
22+
renderComponent as $$renderComponent,
23+
renderHead as $$renderHead,
24+
maybeRenderHead as $$maybeRenderHead,
25+
unescapeHTML as $$unescapeHTML,
26+
renderSlot as $$renderSlot,
27+
mergeSlots as $$mergeSlots,
28+
addAttribute as $$addAttribute,
29+
spreadAttributes as $$spreadAttributes,
30+
defineStyleVars as $$defineStyleVars,
31+
defineScriptVars as $$defineScriptVars,
32+
renderTransition as $$renderTransition,
33+
createTransitionScope as $$createTransitionScope,
34+
renderScript as $$renderScript,
35+
createMetadata as $$createMetadata
36+
} from "http://localhost:3000/";
37+
import Analytics from '../components/Analytics.astro';
38+
39+
import * as $$module1 from '../components/Analytics.astro';
40+
41+
export const $$metadata = $$createMetadata(import.meta.url, { modules: [{ module: $$module1, specifier: '../components/Analytics.astro', assert: {} }], hydratedComponents: [], clientOnlyComponents: [], hydrationDirectives: new Set([]), hoisted: [] });
42+
43+
const $$Component = $$createComponent(($$result, $$props, $$slots) => {
44+
45+
return $$render`${$$renderComponent($$result,'Analytics',Analytics,{})}
46+
<title>${title}</title>
47+
<meta name="description" content="a description">`;
48+
}, undefined, undefined);
49+
export default $$Component;
50+
```
51+
---

internal/printer/printer_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2066,6 +2066,15 @@ const meta = { title: 'My App' };
20662066
name: "namespace is preserved when inside an expression",
20672067
source: `<svg>{<image />}</svg>`,
20682068
},
2069+
{
2070+
name: "head content with component first",
2071+
source: `---
2072+
import Analytics from '../components/Analytics.astro';
2073+
---
2074+
<Analytics />
2075+
<title>{title}</title>
2076+
<meta name="description" content="a description" />`,
2077+
},
20692078
}
20702079
for _, tt := range tests {
20712080
if tt.only {

0 commit comments

Comments
 (0)