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

give each app its own page, preloading and session stores, using context #642

Merged
merged 1 commit into from
May 1, 2019

Conversation

Rich-Harris
Copy link
Member

This is an alternative to #582.

To recap: we need a way to access page, preloading and session inside an app — as stores — that's consistent between server and client. One way to do that is to export them from @sapper/app directly, and rely on the fact that we can reset them immediately before server rendering to ensure that session data isn't accidentally leaked between two users accessing the same server.

It's not future-proof, though. If at some point we support asynchronous or streaming rendering, it would be possible for an app to write to the session store in a way that could affect its value for a different user, if the other user read session asynchronously.

The only solution that I can see is to ensure that each user gets their own set of stores for each server-rendered page. We can achieve this with the context API, and expose the stores like so:

<script>
  import { stores } from '@sapper/app';
  const { page, preloading, session } = stores();
</script>

Calling stores() outside component initialisation would be an error.

@Rich-Harris Rich-Harris merged commit 1a36fb5 into master May 1, 2019
@Rich-Harris Rich-Harris deleted the context-stores branch May 1, 2019 13:01
@pngwn pngwn mentioned this pull request Jul 23, 2019
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