init the store the right way

docks66-json-schema
Yanick Champoux 2023-05-04 16:45:40 -04:00
parent 43addc4072
commit c728f8c09d
2 changed files with 12 additions and 8 deletions

View File

@ -5,17 +5,17 @@ import { writable } from "svelte/store";
export type Api = ReturnType<typeof ship.createStore>; export type Api = ReturnType<typeof ship.createStore>;
export const createApi = () => { export const createApi = () => {
const state = browser const options: Partial<{ preloadedState: object }> = {};
? JSON.parse(localStorage.getItem("ship") || "null")
: undefined;
const api = ship.createStore({ const preloadedState = browser && localStorage.getItem("ship");
preloadedState: state, if (preloadedState) options.preloadedState = JSON.parse(preloadedState);
});
api.dispatch.restore(state); const api = ship.createStore(options);
const svelteStore = writable(state); const svelteStore = writable();
if (preloadedState) {
svelteStore.set(JSON.parse(preloadedState));
}
if (browser) { if (browser) {
api.subscribe(() => { api.subscribe(() => {

View File

@ -1,8 +1,12 @@
import preprocess from "svelte-preprocess"; import preprocess from "svelte-preprocess";
import adapter from "@sveltejs/adapter-static";
/** @type {import('@sveltejs/kit').Config} */ /** @type {import('@sveltejs/kit').Config} */
const config = { const config = {
preprocess: preprocess(), preprocess: preprocess(),
kit: {
adapter: adapter({}),
},
}; };
export default config; export default config;