Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Provide session store in @sapper/app #582

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions runtime/internal/Sapper.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
<script>
import { setContext } from 'svelte';
import { CONTEXT_KEY } from './shared';

export let Root;
export let props;
export let session;

setContext(CONTEXT_KEY, session);
</script>

<Root {...props}/>
19 changes: 16 additions & 3 deletions runtime/internal/shared.mjs
Original file line number Diff line number Diff line change
@@ -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 = () => ({});
function frozen (value) {
function reset(newValue) {
value = newValue;
}

function subscribe(run) {
run(value);
return noop;
}

return { reset, subscribe };
}
3 changes: 2 additions & 1 deletion runtime/src/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ let current_token: {};
let root_preloaded: Promise<any>;
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;
Expand Down
5 changes: 2 additions & 3 deletions runtime/src/app/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
6 changes: 5 additions & 1 deletion runtime/src/server/middleware/get_page_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
5 changes: 0 additions & 5 deletions src/core/create_app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,19 +260,14 @@ function generate_app(manifest_data: ManifestData, path_to_routes: string) {
return `
<!-- This file is generated by Sapper — do not edit it! -->
<script>
import { setContext } from 'svelte';
import { CONTEXT_KEY } from './shared';
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 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);
</script>

<Layout segment={segments[0]} {...level0.props}>
Expand Down
11 changes: 11 additions & 0 deletions test/apps/session/src/routes/module.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script context="module">
import { session } from '@sapper/app';
</script>

<h1>{$session.title}</h1>

<button on:click="{() => session.set({ title: 'clicked' })}">
click me
</button>

<a href="/preloaded">preloaded</a>
3 changes: 1 addition & 2 deletions test/apps/session/src/routes/preloaded.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
</script>

<script>
import { getSession } from '@sapper/app';
const session = getSession();
import { session } from '@sapper/app';

export let title;
</script>
Expand Down
3 changes: 1 addition & 2 deletions test/apps/session/src/routes/session.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<script>
import { getSession } from '@sapper/app';
const session = getSession();
import { session } from '@sapper/app';
</script>

<h1>{$session.title}</h1>
Expand Down
18 changes: 18 additions & 0 deletions test/apps/session/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);

Expand Down