Skip to content

Commit

Permalink
Merge pull request #1029 from sveltejs/gh-1028-followup
Browse files Browse the repository at this point in the history
get store() to work with nested components in SSR compiler
  • Loading branch information
Rich-Harris authored Dec 16, 2017
2 parents 733e949 + f7c540b commit a5f816f
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 15 deletions.
5 changes: 2 additions & 3 deletions src/generators/server-side-rendering/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,8 @@ export default function ssr(

if (storeProps.length > 0) {
const initialize = `_init([${storeProps.map(prop => `"${prop.slice(1)}"`)}])`
if (options.store) {
if (options.store || templateProperties.store) {
initialState.push(`options.store.${initialize}`);
} else if (templateProperties.store) {
initialState.push(`%store().${initialize}`);
}
}

Expand Down Expand Up @@ -116,6 +114,7 @@ export default function ssr(
}
var result = { head: '', addComponent };
${templateProperties.store && `options.store = %store();`}
var html = ${name}._render(result, state, options);
var cssCode = Array.from(components).map(c => c.css && c.css.code).filter(Boolean).join('\\n');
Expand Down
2 changes: 1 addition & 1 deletion test/runtime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ describe("runtime", () => {
target,
hydrate,
data: config.data,
store: config.store
store: (config.store !== true && config.store)
}, config.options || {});

const component = new SvelteComponent(options);
Expand Down
1 change: 1 addition & 0 deletions test/runtime/samples/store-root/Nested.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>It's nice to see you, {{$name}}.</p>
12 changes: 10 additions & 2 deletions test/runtime/samples/store-root/_config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
export default {
html: `<h1>Hello world!</h1>`,
store: true, // TODO remove this in v2

html: `
<h1>Hello world!</h1>
<p>It's nice to see you, world.</p>
`,

test(assert, component, target) {
component.store.set({ name: 'everybody' });

assert.htmlEqual(target.innerHTML, `<h1>Hello everybody!</h1>`);
assert.htmlEqual(target.innerHTML, `
<h1>Hello everybody!</h1>
<p>It's nice to see you, everybody.</p>
`);
}
};
24 changes: 16 additions & 8 deletions test/runtime/samples/store-root/main.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
<h1>Hello {{$name}}!</h1>
<Nested/>

<script>
import { Store } from '../../../../store.js';
export default {
store () {
return new Store({
name: 'world'
});
}
}
import { Store } from '../../../../store.js';
import Nested from './Nested.html';

export default {
store () {
return new Store({
name: 'world'
});
},

components: {
Nested
}
};
</script>
2 changes: 1 addition & 1 deletion test/server-side-rendering/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ describe("ssr", () => {
try {
const component = require(`../runtime/samples/${dir}/main.html`);
const { html } = component.render(config.data, {
store: config.store
store: (config.store !== true) && config.store
});

if (config.html) {
Expand Down

0 comments on commit a5f816f

Please sign in to comment.