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

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

Merged
merged 1 commit into from
Mar 17, 2018

Conversation

Rich-Harris
Copy link
Member

This addresses the first piece of the #178 puzzle: an idiomatic way to assign a store to each individual request on the server, and to rehydrate it on the client:

// app/server.js
import { Store } from 'svelte/store.js';
// ...

app.use(sapper({
  routes,
  store: req => {
    return new Store({
      user: req.session.passport && req.session.passport.user
    });
  }
}));
// app/client
import { init } from 'sapper/runtime.js';
import { Store } from 'svelte/store.js';
import { routes } from './manifest/client.js';

init(target, routes, {
  store: data => new Store(data)
});

With this change, any of your components could reference e.g. {{$user.displayName}}.

The server-side store can contain any data that can be serialized with devalue, otherwise on the client data will be undefined.

@Rich-Harris Rich-Harris merged commit cf6621b into master Mar 17, 2018
@Rich-Harris Rich-Harris deleted the gh-178-store branch March 17, 2018 20:10
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant