Skip to content

Commit e5a75ae

Browse files
authored
chore: add explicit renderer.head method (#16796)
* chore: add explicit `renderer.head` method * minor tweak
1 parent dc7da40 commit e5a75ae

File tree

5 files changed

+36
-26
lines changed

5 files changed

+36
-26
lines changed

packages/svelte/src/internal/server/dev.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ function print_error(renderer, message) {
3939

4040
// eslint-disable-next-line no-console
4141
console.error(message);
42-
renderer.child(
43-
(renderer) => renderer.push(`<script>console.error(${JSON.stringify(message)})</script>`),
44-
'head'
45-
);
42+
renderer.head((r) => r.push(`<script>console.error(${JSON.stringify(message)})</script>`));
4643
}
4744

4845
/**

packages/svelte/src/internal/server/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ export function render(component, options = {}) {
6969
* @returns {void}
7070
*/
7171
export function head(renderer, fn) {
72-
renderer.child((renderer) => {
72+
renderer.head((renderer) => {
7373
renderer.push(BLOCK_OPEN);
7474
renderer.child(fn);
7575
renderer.push(BLOCK_CLOSE);
76-
}, 'head');
76+
});
7777
}
7878

7979
/**

packages/svelte/src/internal/server/renderer.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,23 +81,33 @@ export class Renderer {
8181
/**
8282
* @param {SSRState} global
8383
* @param {Renderer | undefined} [parent]
84-
* @param {RendererType} [type]
8584
*/
86-
constructor(global, parent, type) {
85+
constructor(global, parent) {
86+
this.#parent = parent;
87+
8788
this.global = global;
8889
this.local = parent ? { ...parent.local } : { select_value: undefined };
89-
this.#parent = parent;
90-
this.type = type ?? parent?.type ?? 'body';
90+
this.type = parent ? parent.type : 'body';
91+
}
92+
93+
/**
94+
* @param {(renderer: Renderer) => void} fn
95+
*/
96+
head(fn) {
97+
const head = new Renderer(this.global, this);
98+
head.type = 'head';
99+
100+
this.#out.push(head);
101+
head.child(fn);
91102
}
92103

93104
/**
94105
* Create a child renderer. The child renderer inherits the state from the parent,
95106
* but has its own content.
96107
* @param {(renderer: Renderer) => MaybePromise<void>} fn
97-
* @param {RendererType} [type]
98108
*/
99-
child(fn, type) {
100-
const child = new Renderer(this.global, this, type);
109+
child(fn) {
110+
const child = new Renderer(this.global, this);
101111
this.#out.push(child);
102112

103113
const parent = ssr_context;
@@ -184,7 +194,7 @@ export class Renderer {
184194
* @deprecated this is needed for legacy component bindings
185195
*/
186196
copy() {
187-
const copy = new Renderer(this.global, this.#parent, this.type);
197+
const copy = new Renderer(this.global, this.#parent);
188198
copy.#out = this.#out.map((item) => (item instanceof Renderer ? item.copy() : item));
189199
copy.promises = this.promises;
190200
return copy;
@@ -457,7 +467,8 @@ export class Renderer {
457467
static #push_accumulated_content(tree, accumulated_content) {
458468
for (const [type, content] of Object.entries(accumulated_content)) {
459469
if (!content) continue;
460-
const child = new Renderer(tree.global, tree, /** @type {RendererType} */ (type));
470+
const child = new Renderer(tree.global, tree);
471+
child.type = /** @type {RendererType} */ (type);
461472
child.push(content);
462473
tree.#out.push(child);
463474
}

packages/svelte/src/internal/server/renderer.test.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ test('collects synchronous body content by default', () => {
2020
test('child type switches content area (head vs body)', () => {
2121
const component = (renderer: Renderer) => {
2222
renderer.push('a');
23-
renderer.child(($$renderer) => {
23+
renderer.head(($$renderer) => {
2424
$$renderer.push('<title>T</title>');
25-
}, 'head');
25+
});
2626
renderer.push('b');
2727
};
2828

@@ -33,12 +33,12 @@ test('child type switches content area (head vs body)', () => {
3333

3434
test('child inherits parent type when not specified', () => {
3535
const component = (renderer: Renderer) => {
36-
renderer.child((renderer) => {
36+
renderer.head((renderer) => {
3737
renderer.push('<meta name="x"/>');
3838
renderer.child((renderer) => {
3939
renderer.push('<style>/* css */</style>');
4040
});
41-
}, 'head');
41+
});
4242
};
4343

4444
const { head, body } = Renderer.render(component as unknown as Component);
@@ -118,7 +118,9 @@ test('local state is shallow-copied to children', () => {
118118
});
119119

120120
test('subsume replaces tree content and state from other', () => {
121-
const a = new Renderer(new SSRState('async'), undefined, 'head');
121+
const a = new Renderer(new SSRState('async'));
122+
a.type = 'head';
123+
122124
a.push('<meta />');
123125
a.local.select_value = 'A';
124126

@@ -140,7 +142,9 @@ test('subsume replaces tree content and state from other', () => {
140142
});
141143

142144
test('subsume refuses to switch modes', () => {
143-
const a = new Renderer(new SSRState('sync'), undefined, 'head');
145+
const a = new Renderer(new SSRState('sync'));
146+
a.type = 'head';
147+
144148
a.push('<meta />');
145149
a.local.select_value = 'A';
146150

@@ -273,12 +277,12 @@ describe('async', () => {
273277

274278
test('push async functions work with head content type', async () => {
275279
const component = (renderer: Renderer) => {
276-
renderer.child(($$renderer) => {
280+
renderer.head(($$renderer) => {
277281
$$renderer.push(async () => {
278282
await Promise.resolve();
279283
return '<title>Async Title</title>';
280284
});
281-
}, 'head');
285+
});
282286
};
283287

284288
const { head, body } = await Renderer.render(component as unknown as Component);
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
import { test } from '../../test';
22

3-
export default test({
4-
props: { adjective: 'custom' }
5-
});
3+
export default test({});

0 commit comments

Comments
 (0)