Skip to content

Commit 7714c93

Browse files
authored
chore: deprecate html in favour of body for render() (#11927)
Closes #11281
1 parent 9f823b9 commit 7714c93

File tree

10 files changed

+35
-23
lines changed

10 files changed

+35
-23
lines changed

.changeset/hot-sloths-clap.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte": patch
3+
---
4+
5+
chore: deprecate html in favour of body for render()

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,6 @@ import { current_component, pop, push } from './context.js';
1313
import { BLOCK_CLOSE, BLOCK_OPEN } from './hydration.js';
1414
import { validate_store } from '../shared/validate.js';
1515

16-
/**
17-
* @typedef {{
18-
* head: string;
19-
* html: string;
20-
* }} RenderOutput
21-
*/
22-
2316
// https://html.spec.whatwg.org/multipage/syntax.html#attributes-2
2417
// https://infra.spec.whatwg.org/#noncharacter
2518
const INVALID_ATTR_NAME_CHAR_REGEX =
@@ -105,7 +98,7 @@ export let on_destroy = [];
10598
/**
10699
* @param {typeof import('svelte').SvelteComponent} component
107100
* @param {{ props: Record<string, any>; context?: Map<any, any> }} options
108-
* @returns {RenderOutput}
101+
* @returns {import('#server').RenderOutput}
109102
*/
110103
export function render(component, options) {
111104
const payload = create_payload();
@@ -132,7 +125,8 @@ export function render(component, options) {
132125

133126
return {
134127
head: payload.head.out || payload.head.title ? payload.head.out + payload.head.title : '',
135-
html: payload.out
128+
html: payload.out,
129+
body: payload.out
136130
};
137131
}
138132

packages/svelte/src/internal/server/types.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,12 @@ export interface Payload {
2020
anchor: number;
2121
};
2222
}
23+
24+
export interface RenderOutput {
25+
/** HTML that goes into the `<head>` */
26+
head: string;
27+
/** @deprecated use `body` instead */
28+
html: string;
29+
/** HTML that goes somewhere into the `<body>` */
30+
body: string;
31+
}

packages/svelte/src/legacy/legacy-server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function asClassComponent(component) {
2727
return {
2828
css: { code: '', map: null },
2929
head: result.head,
30-
html: result.html
30+
html: result.body
3131
};
3232
};
3333
// @ts-expect-error this is present for SSR

packages/svelte/tests/runtime-browser/test-ssr.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ export async function run_ssr_test(
2020
await compile_directory(test_dir, 'server', config.compileOptions);
2121

2222
const Component = (await import(`${test_dir}/_output/server/main.svelte.js`)).default;
23-
const { html } = render(Component, { props: config.props || {} });
23+
const { body } = render(Component, { props: config.props || {} });
2424

25-
fs.writeFileSync(`${test_dir}/_output/rendered.html`, html);
25+
fs.writeFileSync(`${test_dir}/_output/rendered.html`, body);
2626

2727
if (config.ssrHtml) {
28-
assert_html_equal_with_options(html, config.ssrHtml, {
28+
assert_html_equal_with_options(body, config.ssrHtml, {
2929
preserveComments: config.compileOptions?.preserveComments
3030
});
3131
} else if (config.html) {
32-
assert_html_equal_with_options(html, config.html, {
32+
assert_html_equal_with_options(body, config.html, {
3333
preserveComments: config.compileOptions?.preserveComments
3434
});
3535
}

packages/svelte/tests/server-side-rendering/test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@ const { test, run } = suite<SSRTest>(async (config, test_dir) => {
2323
const Component = (await import(`${test_dir}/_output/server/main.svelte.js`)).default;
2424
const expected_html = try_read_file(`${test_dir}/_expected.html`);
2525
const rendered = render(Component, { props: config.props || {} });
26-
const { html, head } = rendered;
26+
const { body, head } = rendered;
2727

28-
fs.writeFileSync(`${test_dir}/_actual.html`, html);
28+
fs.writeFileSync(`${test_dir}/_actual.html`, body);
2929

3030
try {
31-
assert_html_equal_with_options(html, expected_html || '', {
31+
assert_html_equal_with_options(body, expected_html || '', {
3232
preserveComments: config.compileOptions?.preserveComments,
3333
withoutNormalizeHtml: config.withoutNormalizeHtml
3434
});
3535
} catch (error: any) {
3636
if (should_update_expected()) {
37-
fs.writeFileSync(`${test_dir}/_expected.html`, html);
37+
fs.writeFileSync(`${test_dir}/_expected.html`, body);
3838
console.log(`Updated ${test_dir}/_expected.html.`);
3939
} else {
4040
error.message += '\n' + `${test_dir}/main.svelte`;

packages/svelte/types/index.d.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,10 +2112,14 @@ declare module 'svelte/server' {
21122112
props: Record<string, any>;
21132113
context?: Map<any, any>;
21142114
}): RenderOutput;
2115-
type RenderOutput = {
2115+
interface RenderOutput {
2116+
/** HTML that goes into the `<head>` */
21162117
head: string;
2118+
/** @deprecated use `body` instead */
21172119
html: string;
2118-
};
2120+
/** HTML that goes somewhere into the `<body>` */
2121+
body: string;
2122+
}
21192123
}
21202124

21212125
declare module 'svelte/store' {

playgrounds/demo/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ async function createServer() {
2828

2929
const template = fs.readFileSync(path.resolve(__dirname, 'index.html'), 'utf-8');
3030
const transformed_template = await vite.transformIndexHtml(req.originalUrl, template);
31-
const { html: appHtml, head: headHtml } = await vite.ssrLoadModule('./src/entry-server.ts');
31+
const { body: appHtml, head: headHtml } = await vite.ssrLoadModule('./src/entry-server.ts');
3232

3333
const html = transformed_template
3434
.replace(`<!--ssr-html-->`, appHtml)

playgrounds/demo/src/entry-server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ import { render } from 'svelte/server';
44
import App from './App.svelte';
55

66
// @ts-ignore
7-
export const { head, html, css } = render(App, { props: { initialCount: 0 } });
7+
export const { head, body, css } = render(App, { props: { initialCount: 0 } });

sites/svelte-5-preview/src/routes/docs/content/01-api/05-imports.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ Svelte provides reactive `Map`, `Set`, `Date` and `URL` classes. These can be im
119119

120120
### `render`
121121

122-
Only available on the server and when compiling with the `server` option. Takes a component and returns an object with `html` and `head` properties on it, which you can use to populate the HTML when server-rendering your app:
122+
Only available on the server and when compiling with the `server` option. Takes a component and returns an object with `body` and `head` properties on it, which you can use to populate the HTML when server-rendering your app:
123123

124124
```js
125125
// @errors: 2724 2305 2307

0 commit comments

Comments
 (0)