Merge pull request #1029 from sveltejs/gh-1028-followup

get store() to work with nested components in SSR compiler
pull/1030/head
Rich Harris 7 years ago committed by GitHub
commit a5f816f618
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -81,10 +81,8 @@ export default function ssr(
if (storeProps.length > 0) { if (storeProps.length > 0) {
const initialize = `_init([${storeProps.map(prop => `"${prop.slice(1)}"`)}])` const initialize = `_init([${storeProps.map(prop => `"${prop.slice(1)}"`)}])`
if (options.store) { if (options.store || templateProperties.store) {
initialState.push(`options.store.${initialize}`); initialState.push(`options.store.${initialize}`);
} else if (templateProperties.store) {
initialState.push(`%store().${initialize}`);
} }
} }
@ -116,6 +114,7 @@ export default function ssr(
} }
var result = { head: '', addComponent }; var result = { head: '', addComponent };
${templateProperties.store && `options.store = %store();`}
var html = ${name}._render(result, state, options); var html = ${name}._render(result, state, options);
var cssCode = Array.from(components).map(c => c.css && c.css.code).filter(Boolean).join('\\n'); var cssCode = Array.from(components).map(c => c.css && c.css.code).filter(Boolean).join('\\n');

@ -159,7 +159,7 @@ describe("runtime", () => {
target, target,
hydrate, hydrate,
data: config.data, data: config.data,
store: config.store store: (config.store !== true && config.store)
}, config.options || {}); }, config.options || {});
const component = new SvelteComponent(options); const component = new SvelteComponent(options);

@ -0,0 +1 @@
<p>It's nice to see you, {{$name}}.</p>

@ -1,9 +1,17 @@
export default { 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) { test(assert, component, target) {
component.store.set({ name: 'everybody' }); 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>
`);
} }
}; };

@ -1,11 +1,19 @@
<h1>Hello {{$name}}!</h1> <h1>Hello {{$name}}!</h1>
<Nested/>
<script> <script>
import { Store } from '../../../../store.js'; import { Store } from '../../../../store.js';
export default { import Nested from './Nested.html';
store () {
return new Store({ export default {
name: 'world' store () {
}); return new Store({
} name: 'world'
} });
},
components: {
Nested
}
};
</script> </script>

@ -113,7 +113,7 @@ describe("ssr", () => {
try { try {
const component = require(`../runtime/samples/${dir}/main.html`); const component = require(`../runtime/samples/${dir}/main.html`);
const { html } = component.render(config.data, { const { html } = component.render(config.data, {
store: config.store store: (config.store !== true) && config.store
}); });
if (config.html) { if (config.html) {

Loading…
Cancel
Save