From afeedf6bb256f295fce16e3feeb6e7db91578856 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 29 Apr 2019 14:52:19 -0400 Subject: [PATCH] give each app its own page, preloading and session stores, using context --- runtime/internal/Sapper.svelte | 12 ----------- runtime/internal/shared.mjs | 5 ----- runtime/src/app/app.ts | 17 +++++++++++----- runtime/src/app/index.ts | 7 ++----- .../src/server/middleware/get_page_handler.ts | 20 ++++++++++++------- src/core/create_app.ts | 4 ++-- .../basics/src/routes/[...rest]/deep.svelte | 3 ++- .../basics/src/routes/[...rest]/index.svelte | 3 ++- test/apps/basics/src/routes/[slug].svelte | 3 ++- .../basics/src/routes/echo-query/index.svelte | 3 ++- .../encoding/src/routes/echo/page/[slug].html | 5 ++++- .../apps/layout/src/routes/[x]/[y]/[z].svelte | 3 ++- .../layout/src/routes/[x]/[y]/_layout.svelte | 3 ++- .../apps/preloading/src/routes/_layout.svelte | 4 +++- .../src/routes/prefetch/[slug]/index.svelte | 3 ++- test/apps/session/src/routes/preloaded.svelte | 4 ++-- test/apps/session/src/routes/session.svelte | 4 ++-- 17 files changed, 54 insertions(+), 49 deletions(-) delete mode 100644 runtime/internal/Sapper.svelte diff --git a/runtime/internal/Sapper.svelte b/runtime/internal/Sapper.svelte deleted file mode 100644 index 2203d7656..000000000 --- a/runtime/internal/Sapper.svelte +++ /dev/null @@ -1,12 +0,0 @@ - - - \ No newline at end of file diff --git a/runtime/internal/shared.mjs b/runtime/internal/shared.mjs index 534a6bf02..718d67b83 100644 --- a/runtime/internal/shared.mjs +++ b/runtime/internal/shared.mjs @@ -1,10 +1,5 @@ import { writable } from 'svelte/store'; -export const stores = { - preloading: writable(false), - page: writable(null) -}; - export const CONTEXT_KEY = {}; export const preload = () => ({}); \ No newline at end of file diff --git a/runtime/src/app/app.ts b/runtime/src/app/app.ts index 31a819947..597ac9c7f 100644 --- a/runtime/src/app/app.ts +++ b/runtime/src/app/app.ts @@ -1,7 +1,6 @@ import { writable } from 'svelte/store.mjs'; import App from '@sapper/internal/App.svelte'; -import { stores } from '@sapper/internal/shared'; -import { Root, root_preload, ErrorComponent, ignore, components, routes } from '@sapper/internal/manifest-client'; +import { root_preload, ErrorComponent, ignore, components, routes } from '@sapper/internal/manifest-client'; import { Target, ScrollPosition, @@ -24,12 +23,16 @@ let current_token: {}; let root_preloaded: Promise; let current_branch = []; -const session = writable(initial_data && initial_data.session); +const stores = { + page: writable({}), + preloading: writable(null), + session: writable(initial_data && initial_data.session) +}; let $session; let session_dirty: boolean; -session.subscribe(async value => { +stores.session.subscribe(async value => { $session = value; if (!ready) return; @@ -216,7 +219,11 @@ async function render(redirect: Redirect, branch: any[], props: any, page: Page) if (root_component) { root_component.$set(props); } else { - props.session = session; + props.stores = { + page: { subscribe: stores.page.subscribe }, + preloading: { subscribe: stores.preloading.subscribe }, + session: stores.session + }; props.level0 = { props: await root_preloaded }; diff --git a/runtime/src/app/index.ts b/runtime/src/app/index.ts index 5d0900e66..23fea1619 100644 --- a/runtime/src/app/index.ts +++ b/runtime/src/app/index.ts @@ -1,10 +1,7 @@ import { getContext } from 'svelte'; -import { CONTEXT_KEY, stores } from '@sapper/internal/shared'; +import { CONTEXT_KEY } 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 stores = () => getContext(CONTEXT_KEY); 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 b94d14ba6..fd2220715 100644 --- a/runtime/src/server/middleware/get_page_handler.ts +++ b/runtime/src/server/middleware/get_page_handler.ts @@ -204,10 +204,22 @@ export function get_page_handler( }); const props = { + stores: { + page: { + subscribe: writable({ + path: req.path, + query: req.query, + params + }).subscribe + }, + preloading: { + subscribe: writable(null).subscribe + }, + session: writable(session) + }, segments: layout_segments, status: error ? status : 200, error: error ? error instanceof Error ? error : { message: error } : null, - session: writable(session), level0: { props: preloaded[0] }, @@ -231,12 +243,6 @@ export function get_page_handler( } } - stores.page.set({ - path: req.path, - query: req.query, - params: params - }); - const { html, head, css } = App.render(props); const serialized = { diff --git a/src/core/create_app.ts b/src/core/create_app.ts index 2a0df0013..252744c54 100644 --- a/src/core/create_app.ts +++ b/src/core/create_app.ts @@ -271,14 +271,14 @@ function generate_app(manifest_data: ManifestData, path_to_routes: string) { import Layout from '${get_file(path_to_routes, manifest_data.root)}'; import Error from '${get_file(path_to_routes, manifest_data.error)}'; - export let session; + export let stores; export let error; export let status; export let segments; export let level0; ${levels.map(l => `export let level${l} = null;`).join('\n\t\t\t')} - setContext(CONTEXT_KEY, session); + setContext(CONTEXT_KEY, stores); diff --git a/test/apps/basics/src/routes/[...rest]/deep.svelte b/test/apps/basics/src/routes/[...rest]/deep.svelte index 4dfeb444e..7b5ae6323 100644 --- a/test/apps/basics/src/routes/[...rest]/deep.svelte +++ b/test/apps/basics/src/routes/[...rest]/deep.svelte @@ -1,5 +1,6 @@

{$page.params.rest.join(',')}

diff --git a/test/apps/basics/src/routes/[...rest]/index.svelte b/test/apps/basics/src/routes/[...rest]/index.svelte index edd2d8e87..be9494ace 100644 --- a/test/apps/basics/src/routes/[...rest]/index.svelte +++ b/test/apps/basics/src/routes/[...rest]/index.svelte @@ -1,5 +1,6 @@

{$page.params.rest.join(',')}

diff --git a/test/apps/basics/src/routes/[slug].svelte b/test/apps/basics/src/routes/[slug].svelte index e31f6d9fd..2f7d3798a 100644 --- a/test/apps/basics/src/routes/[slug].svelte +++ b/test/apps/basics/src/routes/[slug].svelte @@ -1,5 +1,6 @@

{$page.params.slug.toUpperCase()}

\ No newline at end of file diff --git a/test/apps/basics/src/routes/echo-query/index.svelte b/test/apps/basics/src/routes/echo-query/index.svelte index 614bb4d31..ee2473525 100644 --- a/test/apps/basics/src/routes/echo-query/index.svelte +++ b/test/apps/basics/src/routes/echo-query/index.svelte @@ -1,5 +1,6 @@

{JSON.stringify($page.query)}

\ No newline at end of file diff --git a/test/apps/encoding/src/routes/echo/page/[slug].html b/test/apps/encoding/src/routes/echo/page/[slug].html index e2c9491b2..4e6e1870f 100644 --- a/test/apps/encoding/src/routes/echo/page/[slug].html +++ b/test/apps/encoding/src/routes/echo/page/[slug].html @@ -7,7 +7,10 @@ diff --git a/test/apps/layout/src/routes/[x]/[y]/[z].svelte b/test/apps/layout/src/routes/[x]/[y]/[z].svelte index 38c557539..85ebc8f75 100644 --- a/test/apps/layout/src/routes/[x]/[y]/[z].svelte +++ b/test/apps/layout/src/routes/[x]/[y]/[z].svelte @@ -9,7 +9,8 @@ diff --git a/test/apps/layout/src/routes/[x]/[y]/_layout.svelte b/test/apps/layout/src/routes/[x]/[y]/_layout.svelte index 38fb80313..79102b7f7 100644 --- a/test/apps/layout/src/routes/[x]/[y]/_layout.svelte +++ b/test/apps/layout/src/routes/[x]/[y]/_layout.svelte @@ -9,7 +9,8 @@ diff --git a/test/apps/preloading/src/routes/prefetch/[slug]/index.svelte b/test/apps/preloading/src/routes/prefetch/[slug]/index.svelte index 6eebc4c80..2fba12d89 100644 --- a/test/apps/preloading/src/routes/prefetch/[slug]/index.svelte +++ b/test/apps/preloading/src/routes/prefetch/[slug]/index.svelte @@ -1,5 +1,6 @@

{$page.params.slug}

diff --git a/test/apps/session/src/routes/preloaded.svelte b/test/apps/session/src/routes/preloaded.svelte index 6964d9819..8f64e9f2a 100644 --- a/test/apps/session/src/routes/preloaded.svelte +++ b/test/apps/session/src/routes/preloaded.svelte @@ -5,8 +5,8 @@ diff --git a/test/apps/session/src/routes/session.svelte b/test/apps/session/src/routes/session.svelte index 6501114de..d3adfc8fb 100644 --- a/test/apps/session/src/routes/session.svelte +++ b/test/apps/session/src/routes/session.svelte @@ -1,6 +1,6 @@

{$session.title}