From f639bbed4290c146ff1d4bea845e30fdd4768562 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 15 Dec 2017 20:58:06 -0500 Subject: [PATCH] get store() to work with nested components in SSR compiler --- src/generators/server-side-rendering/index.ts | 5 ++-- test/runtime/index.js | 2 +- test/runtime/samples/store-root/Nested.html | 1 + test/runtime/samples/store-root/_config.js | 12 ++++++++-- test/runtime/samples/store-root/main.html | 24 ++++++++++++------- test/server-side-rendering/index.js | 2 +- 6 files changed, 31 insertions(+), 15 deletions(-) create mode 100644 test/runtime/samples/store-root/Nested.html diff --git a/src/generators/server-side-rendering/index.ts b/src/generators/server-side-rendering/index.ts index 4d4399f695..827d5720e1 100644 --- a/src/generators/server-side-rendering/index.ts +++ b/src/generators/server-side-rendering/index.ts @@ -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}`); } } @@ -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'); diff --git a/test/runtime/index.js b/test/runtime/index.js index fa3482337b..2635602db2 100644 --- a/test/runtime/index.js +++ b/test/runtime/index.js @@ -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); diff --git a/test/runtime/samples/store-root/Nested.html b/test/runtime/samples/store-root/Nested.html new file mode 100644 index 0000000000..98d2077280 --- /dev/null +++ b/test/runtime/samples/store-root/Nested.html @@ -0,0 +1 @@ +

It's nice to see you, {{$name}}.

\ No newline at end of file diff --git a/test/runtime/samples/store-root/_config.js b/test/runtime/samples/store-root/_config.js index 775c0c0d8b..72658d0038 100644 --- a/test/runtime/samples/store-root/_config.js +++ b/test/runtime/samples/store-root/_config.js @@ -1,9 +1,17 @@ export default { - html: `

Hello world!

`, + store: true, // TODO remove this in v2 + + html: ` +

Hello world!

+

It's nice to see you, world.

+ `, test(assert, component, target) { component.store.set({ name: 'everybody' }); - assert.htmlEqual(target.innerHTML, `

Hello everybody!

`); + assert.htmlEqual(target.innerHTML, ` +

Hello everybody!

+

It's nice to see you, everybody.

+ `); } }; diff --git a/test/runtime/samples/store-root/main.html b/test/runtime/samples/store-root/main.html index 01f7434fc5..de16851d9a 100644 --- a/test/runtime/samples/store-root/main.html +++ b/test/runtime/samples/store-root/main.html @@ -1,11 +1,19 @@

Hello {{$name}}!

+ + diff --git a/test/server-side-rendering/index.js b/test/server-side-rendering/index.js index 385f781b6e..5682779703 100644 --- a/test/server-side-rendering/index.js +++ b/test/server-side-rendering/index.js @@ -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) {