Merge pull request #2249 from sveltejs/gh-2182

set/unset parent component correctly in SSR
pull/2250/head
Rich Harris 6 years ago committed by GitHub
commit e87976dc21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -65,28 +65,39 @@ export function debug(file, line, column, values) {
return '';
}
export function create_ssr_component($$render) {
return {
render: (props = {}, options = {}) => {
let on_destroy;
export function create_ssr_component(fn) {
function $$render(result, props, bindings, slots) {
const parent_component = current_component;
// TODO do we need on_ready, since on_mount,
// before_render and after_render don't run?
const $$ = {
on_destroy,
context: new Map(parent_component ? parent_component.$$.context : []),
// these will be immediately discarded
on_mount: [],
on_destroy: [],
before_render: [],
after_render: [],
context: new Map(parent_component ? parent_component.$$.context : []),
callbacks: blankObject()
};
set_current_component({ $$ });
const html = fn(result, props, bindings, slots);
set_current_component(parent_component);
return html;
}
return {
render: (props = {}, options = {}) => {
on_destroy = [];
const result = { head: '', css: new Set() };
const html = $$render(result, props, {}, options);
run_all($$.on_destroy);
run_all(on_destroy);
return {
html,

@ -0,0 +1,8 @@
<script>
import { getContext } from 'svelte';
import { ID } from './Nested.svelte';
const name = getContext('test');
</script>
<div>{name}</div>

@ -0,0 +1,10 @@
<script>
import { getContext, setContext } from 'svelte';
export let name = '';
const parentName = getContext('test');
setContext('test', parentName ? parentName + '/' + name : name);
</script>
<slot></slot>

@ -0,0 +1,6 @@
export default {
html: `
<div>foo/bar</div>
<div>foo/baz</div>
`
};

@ -0,0 +1,13 @@
<script>
import Nested from './Nested.svelte';
import Leaf from './Leaf.svelte';
</script>
<Nested name='foo'>
<Nested name='bar'>
<Leaf/>
</Nested>
<Nested name='baz'>
<Leaf/>
</Nested>
</Nested>
Loading…
Cancel
Save