diff --git a/runtime/internal/Sapper.svelte b/runtime/internal/Sapper.svelte index 2203d7656..81a2caef1 100644 --- a/runtime/internal/Sapper.svelte +++ b/runtime/internal/Sapper.svelte @@ -1,12 +1,6 @@ \ No newline at end of file diff --git a/runtime/internal/shared.mjs b/runtime/internal/shared.mjs index 534a6bf02..3a50e0853 100644 --- a/runtime/internal/shared.mjs +++ b/runtime/internal/shared.mjs @@ -1,10 +1,23 @@ import { writable } from 'svelte/store'; +import { noop } from 'svelte/internal'; export const stores = { + page: process.browser ? writable(null) : frozen(null), preloading: writable(false), - page: writable(null) + session: process.browser ? writable(false) : frozen(false) }; -export const CONTEXT_KEY = {}; +export const preload = () => ({}); -export const preload = () => ({}); \ No newline at end of file +function frozen (value) { + function reset(newValue) { + value = newValue; + } + + function subscribe(run) { + run(value); + return noop; + } + + return { reset, subscribe }; +} diff --git a/runtime/src/app/app.ts b/runtime/src/app/app.ts index 2de70527b..b9825e468 100644 --- a/runtime/src/app/app.ts +++ b/runtime/src/app/app.ts @@ -23,7 +23,8 @@ let current_token: {}; let root_preloaded: Promise; let current_branch = []; -const session = writable(initial_data && initial_data.session); +const { session } = stores +initial_data && session.set(initial_data.session); let $session; let session_dirty: boolean; diff --git a/runtime/src/app/index.ts b/runtime/src/app/index.ts index 5d0900e66..e2dfff2bb 100644 --- a/runtime/src/app/index.ts +++ b/runtime/src/app/index.ts @@ -1,10 +1,9 @@ import { getContext } from 'svelte'; -import { CONTEXT_KEY, stores } from '@sapper/internal/shared'; +import { stores } from '@sapper/internal/shared'; export const preloading = { subscribe: stores.preloading.subscribe }; export const page = { subscribe: stores.page.subscribe }; - -export const getSession = () => getContext(CONTEXT_KEY); +export const session = stores.session export { default as start } from './start/index'; export { default as goto } from './goto/index'; diff --git a/runtime/src/server/middleware/get_page_handler.ts b/runtime/src/server/middleware/get_page_handler.ts index c0a39cc0c..afa587f87 100644 --- a/runtime/src/server/middleware/get_page_handler.ts +++ b/runtime/src/server/middleware/get_page_handler.ts @@ -231,14 +231,18 @@ export function get_page_handler( } } - stores.page.set({ + stores.page.reset({ path: req.path, query: req.query, params: params }); + stores.session.reset(session); const { html, head, css } = App.render(props); + stores.session.reset(false); + stores.page.reset(false); + const serialized = { preloaded: `[${preloaded.map(data => try_serialize(data)).join(',')}]`, session: session && try_serialize(session, err => { diff --git a/src/core/create_app.ts b/src/core/create_app.ts index 983b03b2d..4d1e4dfa9 100644 --- a/src/core/create_app.ts +++ b/src/core/create_app.ts @@ -260,19 +260,14 @@ function generate_app(manifest_data: ManifestData, path_to_routes: string) { return ` diff --git a/test/apps/session/src/routes/module.svelte b/test/apps/session/src/routes/module.svelte new file mode 100644 index 000000000..2bf7eb3d1 --- /dev/null +++ b/test/apps/session/src/routes/module.svelte @@ -0,0 +1,11 @@ + + +

{$session.title}

+ + + +preloaded diff --git a/test/apps/session/src/routes/preloaded.svelte b/test/apps/session/src/routes/preloaded.svelte index 6964d9819..344adeca1 100644 --- a/test/apps/session/src/routes/preloaded.svelte +++ b/test/apps/session/src/routes/preloaded.svelte @@ -5,8 +5,7 @@ diff --git a/test/apps/session/src/routes/session.svelte b/test/apps/session/src/routes/session.svelte index 6501114de..3273a3ae7 100644 --- a/test/apps/session/src/routes/session.svelte +++ b/test/apps/session/src/routes/session.svelte @@ -1,6 +1,5 @@

{$session.title}

diff --git a/test/apps/session/test.ts b/test/apps/session/test.ts index 3fd910f12..0296269b9 100644 --- a/test/apps/session/test.ts +++ b/test/apps/session/test.ts @@ -36,6 +36,24 @@ describe('session', function() { assert.equal(await title(), 'changed'); }); + it('works in module context', async () => { + await page.goto(`${base}/module`); + + assert.equal(await title(), 'hello world'); + + await start(); + assert.equal(await title(), 'hello world'); + + await page.click('button'); + assert.equal(await title(), 'clicked'); + + await page.click('a'); + assert.equal(await title(), 'clicked'); + + await page.click('button'); + assert.equal(await title(), 'changed'); + }); + it('preloads session props', async () => { await page.goto(`${base}/preloaded`);