Skip to content

Commit

Permalink
prevent calls to window obj to support sapper ssr
Browse files Browse the repository at this point in the history
  • Loading branch information
xec committed May 24, 2020
1 parent 80c0d72 commit 2f13c1a
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/Todo.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script>
import { onMount } from "svelte";
import { quintOut } from "svelte/easing";
import { crossfade } from "svelte/transition";
import { flip } from "svelte/animate";
Expand Down Expand Up @@ -57,15 +58,19 @@
let activetab;
// load initial state from localStorage (if any)
if (localStorage.todoData) {
let vm = JSON.parse(localStorage.todoData) || [];
todos = vm.todos;
tabs = vm.tabs;
uid = todos.reduce((p, c) => (c.id > p ? c.id : p), 0) + 1;
}
onMount(async () => {
if (typeof window !== 'undefined' && localStorage.todoData) {
let vm = JSON.parse(localStorage.todoData) || {};
if (vm.todos) {
todos = vm.todos;
uid = todos.reduce((p, c) => (c.id > p ? c.id : p), 0) + 1;
}
if (vm.tabs) tabs = vm.tabs;
}
})
// keep localStorage synced with current state
$: {
$: if (typeof window !== 'undefined' && window.localStorage) {
localStorage.todoData = JSON.stringify({todos, tabs});
}
</script>
Expand Down

0 comments on commit 2f13c1a

Please sign in to comment.