Skip to content

Commit 4497628

Browse files
authored
Improve renderHead behavior (#315)
* feat: improve render-head behavior * chore: add changeset * chore: fix wasm test
1 parent 5c969ff commit 4497628

File tree

4 files changed

+13
-11
lines changed

4 files changed

+13
-11
lines changed

.changeset/quick-lamps-stare.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+
Improve head injection behavior

internal/printer/printer.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ var ADD_ATTRIBUTE = "$$addAttribute"
3737
var SPREAD_ATTRIBUTES = "$$spreadAttributes"
3838
var DEFINE_STYLE_VARS = "$$defineStyleVars"
3939
var DEFINE_SCRIPT_VARS = "$$defineScriptVars"
40-
var RENDER_HEAD = "$$renderHead"
4140
var CREATE_METADATA = "$$createMetadata"
4241
var METADATA = "$$metadata"
4342
var RESULT = "$$result"
@@ -60,7 +59,6 @@ func (p *printer) printInternalImports(importSpecifier string) {
6059
p.print("import {\n ")
6160
p.print(FRAGMENT + ",\n ")
6261
p.print("render as " + TEMPLATE_TAG + ",\n ")
63-
p.print("renderHead as " + RENDER_HEAD + ",\n ")
6462
p.print("createAstro as " + CREATE_ASTRO + ",\n ")
6563
p.print("createComponent as " + CREATE_COMPONENT + ",\n ")
6664
p.print("renderComponent as " + RENDER_COMPONENT + ",\n ")
@@ -94,7 +92,7 @@ func (p *printer) printCSSImports(cssLen int) {
9492

9593
func (p *printer) printRenderHead() {
9694
p.addNilSourceMapping()
97-
p.print(fmt.Sprintf("${%s(%s)}", RENDER_HEAD, RESULT))
95+
p.print("<!--astro:head-->")
9896
}
9997

10098
func (p *printer) printReturnOpen() {

internal/printer/printer_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
var INTERNAL_IMPORTS = fmt.Sprintf("import {\n %s\n} from \"%s\";\n", strings.Join([]string{
1818
FRAGMENT,
1919
"render as " + TEMPLATE_TAG,
20-
"renderHead as " + RENDER_HEAD,
2120
"createAstro as " + CREATE_ASTRO,
2221
"createComponent as " + CREATE_COMPONENT,
2322
"renderComponent as " + RENDER_COMPONENT,
@@ -43,7 +42,7 @@ var STYLE_SUFFIX = "];\nfor (const STYLE of STYLES) $$result.styles.add(STYLE);\
4342
var SCRIPT_PRELUDE = "const SCRIPTS = [\n"
4443
var SCRIPT_SUFFIX = "];\nfor (const SCRIPT of SCRIPTS) $$result.scripts.add(SCRIPT);\n"
4544
var CREATE_ASTRO_CALL = "const $$Astro = $$createAstro(import.meta.url, 'https://astro.build', '.');\nconst Astro = $$Astro;"
46-
var RENDER_HEAD_RESULT = fmt.Sprintf("${%s(%s)}", RENDER_HEAD, RESULT)
45+
var RENDER_HEAD_RESULT = "<!--astro:head-->"
4746

4847
// SPECIAL TEST FIXTURES
4948
var NON_WHITESPACE_CHARS = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_=+[];:'\",.?")
@@ -98,7 +97,7 @@ func TestPrinter(t *testing.T) {
9897
name: "basic renderHead",
9998
source: `<html><head><title>Ah</title></head></html>`,
10099
want: want{
101-
code: `<html><head><title>Ah</title>${$$renderHead($$result)}</head></html>`,
100+
code: `<html><head><title>Ah</title>` + RENDER_HEAD_RESULT + `</head></html>`,
102101
},
103102
},
104103
{
@@ -953,7 +952,7 @@ ${$$renderComponent($$result,'my-element','my-element',{"client:load":true,"clie
953952
name: "Self-closing title II",
954953
source: `<html><head><title /></head><body></body></html>`,
955954
want: want{
956-
code: `<html><head><title></title>${$$renderHead($$result)}</head><body></body></html>`,
955+
code: `<html><head><title></title>` + RENDER_HEAD_RESULT + `</head><body></body></html>`,
957956
},
958957
},
959958
{
@@ -1755,7 +1754,7 @@ const items = ["Dog", "Cat", "Platipus"];
17551754
transform.ExtractStyles(doc)
17561755
transform.Transform(doc, transform.TransformOptions{Scope: hash}) // note: we want to test Transform in context here, but more advanced cases could be tested separately
17571756
result := PrintToJS(code, doc, 0, transform.TransformOptions{
1758-
Scope: "astro-XXXX",
1757+
Scope: "XXXX",
17591758
Site: "https://astro.build",
17601759
InternalURL: "http://localhost:3000/",
17611760
ProjectRoot: ".",

lib/compiler/test/render-head.test.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ async function run() {
1515
},
1616
});
1717

18-
if (!result.code.includes('$$renderHead(')) {
19-
console.log(result.code);
20-
throw new Error('Result did not include $$renderHead(');
18+
console.log(result.code);
19+
if (!result.code.includes(`<!--astro:head-->`)) {
20+
throw new Error('Result did not include <!--astro:head--> placeholder');
2121
}
2222
}
2323

0 commit comments

Comments
 (0)