add server- and client-side store management (#178)

This commit is contained in:
Rich Harris
2018-03-17 13:45:59 -04:00
parent 67a81a3cac
commit 9812cbd71c
8 changed files with 67 additions and 15 deletions

View File

@@ -1,8 +1,11 @@
import { init, prefetchRoutes } from '../../../runtime.js';
import { Store } from 'svelte/store.js';
import { routes } from './manifest/client.js';
window.init = () => {
return init(document.querySelector('#sapper'), routes);
return init(document.querySelector('#sapper'), routes, {
store: data => new Store(data)
});
};
window.prefetchRoutes = prefetchRoutes;

View File

@@ -3,6 +3,7 @@ import { resolve } from 'url';
import express from 'express';
import serve from 'serve-static';
import sapper from '../../../dist/middleware.ts.js';
import { Store } from 'svelte/store.js';
import { routes } from './manifest/server.js';
let pending;
@@ -77,7 +78,14 @@ const middlewares = [
next();
},
sapper({ routes })
sapper({
routes,
store: () => {
return new Store({
title: 'Stored title'
});
}
})
];
if (BASEPATH) {

View File

@@ -0,0 +1 @@
<h1>{{$title}}</h1>

View File

@@ -521,6 +521,18 @@ function run({ mode, basepath = '' }) {
assert.equal(title, '42');
});
});
it('renders store props', () => {
return nightmare.goto(`${base}/store`)
.page.title()
.then(title => {
assert.equal(title, 'Stored title');
return nightmare.init().page.title();
})
.then(title => {
assert.equal(title, 'Stored title');
});
});
});
describe('headers', () => {