Skip to content

Commit

Permalink
fix: [SSR] Cannot use set() or clear() with LocalStorage on client side
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoenescu committed Sep 7, 2018
1 parent c2f7beb commit fe11a45
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 15 deletions.
51 changes: 51 additions & 0 deletions dev/components/other/web-storage.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<template>
<div class="layout-padding">
<div>{{ storage }}</div>
<q-btn-group>
<q-btn label="Toggle test1" @click="toggle('test1')" />
<q-btn label="Toggle test2" @click="toggle('test2')" />
<q-btn label="Toggle test3" @click="toggle('test3')" />
<q-btn label="Clear" @click="clear" />
</q-btn-group>
</div>
</template>

<script>
import { LocalStorage } from 'quasar'
export default {
data () {
return {
storage: LocalStorage.get.all()
}
},
methods: {
toggle (key) {
if (LocalStorage.has(key)) {
LocalStorage.remove(key)
}
else {
LocalStorage.set(key, `${key}-value`)
}
this.update()
},
clear () {
LocalStorage.clear()
this.update()
},
update () {
this.storage = LocalStorage.get.all()
}
},
mounted () {
this.$nextTick(() => {
this.update()
})
}
}
</script>

<style lang="stylus">
@import '~variables'
</style>
2 changes: 1 addition & 1 deletion src/components/select/QSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ export default {
props: {
cover: true,
disable: !this.editable,
anchorClick: false,
anchorClick: false,
maxHeight: this.popupMaxHeight
},
slot: 'after',
Expand Down
22 changes: 15 additions & 7 deletions src/plugins/local-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@ import { onSSR, hasWebStorage } from './platform.js'
import { getEmptyStorage, getStorage } from '../utils/web-storage.js'

export default {
install ({ $q }) {
install ({ $q, queues }) {
const assignStorage = storage => {
$q.localStorage = storage
Object.assign(this, storage)
}

const clientInit = () => {
if (hasWebStorage()) {
assignStorage(getStorage('local'))
}
}

if (onSSR) {
$q.localStorage = getEmptyStorage()
assignStorage(getEmptyStorage())
queues.takeover.push(clientInit)
return
}

if (hasWebStorage()) {
const storage = getStorage('local')
$q.localStorage = storage
Object.assign(this, storage)
}
clientInit()
}
}
22 changes: 15 additions & 7 deletions src/plugins/session-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@ import { onSSR, hasWebStorage } from './platform.js'
import { getEmptyStorage, getStorage } from '../utils/web-storage.js'

export default {
install ({ $q }) {
install ({ $q, queues }) {
const assignStorage = storage => {
$q.sessionStorage = storage
Object.assign(this, storage)
}

const clientInit = () => {
if (hasWebStorage()) {
assignStorage(getStorage('session'))
}
}

if (onSSR) {
$q.sessionStorage = getEmptyStorage()
assignStorage(getEmptyStorage())
queues.takeover.push(clientInit)
return
}

if (hasWebStorage()) {
const storage = getStorage('session')
$q.sessionStorage = storage
Object.assign(this, storage)
}
clientInit()
}
}

0 comments on commit fe11a45

Please sign in to comment.