Skip to content

Feature: List rendered components during SSR #5476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion site/content/docs/03-run-time.md
Original file line number Diff line number Diff line change
Expand Up @@ -1051,12 +1051,14 @@ A server-side component exposes a `render` method that can be called with option

You can import a Svelte component directly into Node using [`svelte/register`](docs#svelte_register).

`renderedComponents` contains an array of filenames of components that were rendered during SSR. The filenames come from `svelte.compile({filename: '...' })`, which is generally done behind the scenes by your bundler (eg: Rollup). You can match these file names to your asset modules in order to preload asynchronous components that will be required on the client-side ahead-of-time.

```js
require('svelte/register');

const App = require('./App.svelte').default;

const { head, html, css } = App.render({
const { head, html, css, renderedComponents } = App.render({
answer: 42
});
```
1 change: 1 addition & 0 deletions src/compiler/compile/render_ssr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export default function ssr(
instance_javascript,
...parent_bindings,
css.code && b`$$result.css.add(#css);`,
options.filename && b`$$result.renderedComponents.add('${options.filename}');`,
main
].filter(Boolean);

Expand Down
6 changes: 4 additions & 2 deletions src/runtime/internal/ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ export function create_ssr_component(fn) {
map: null;
code: string;
}>;
} = { title: '', head: '', css: new Set() };
renderedComponents: Set<string>;
} = { title: '', head: '', css: new Set(), renderedComponents: new Set() };

const html = $$render(result, props, {}, options);

Expand All @@ -119,7 +120,8 @@ export function create_ssr_component(fn) {
code: Array.from(result.css).map(css => css.code).join('\n'),
map: null // TODO
},
head: result.title + result.head
head: result.title + result.head,
renderedComponents: Array.from(result.renderedComponents)
};
},

Expand Down
7 changes: 5 additions & 2 deletions test/server-side-rendering/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ describe('ssr', () => {
if (dir[0] === '.') return;

const config = loadConfig(`${__dirname}/samples/${dir}/_config.js`);

// add .solo to a sample directory name to only run that test, or
// .show to always show the output. or both
const solo = config.solo || /\.solo/.test(dir);
Expand Down Expand Up @@ -76,7 +75,11 @@ describe('ssr', () => {
if (css.code) fs.writeFileSync(`${dir}/_actual.css`, css.code);

try {
assert.htmlEqual(html, expectedHtml);
if (config.testForRenderedComponents) {
assert.ok(rendered.renderedComponents[0].includes('main.svelte'));
} else {
assert.htmlEqual(html, expectedHtml);
}
} catch (error) {
if (shouldUpdateExpected()) {
fs.writeFileSync(`${dir}/_expected.html`, html);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
testForRenderedComponents: true
};
Empty file.
Empty file.