Display renderedComponents during SSR

pull/5476/head
DominusVilicus 5 years ago
parent 229a16d6e7
commit c1ba761d21

@ -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
});
```

@ -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);

@ -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);
@ -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)
};
},

@ -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);
@ -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);

@ -0,0 +1,3 @@
export default {
testForRenderedComponents: true
};
Loading…
Cancel
Save