Skip to content

Commit

Permalink
feat: SSR support for Web Storage plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoenescu committed Jun 18, 2018
1 parent 3bd2f9d commit 78d5d23
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
16 changes: 13 additions & 3 deletions src/plugins/platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,20 +169,30 @@ function getPlatform (userAgent) {
return browser
}

function getClientProperties () {
let webStorage = false
let webStorage

export function hasWebStorage () {
if (webStorage !== void 0) {
return webStorage
}

try {
if (window.localStorage) {
webStorage = true
return true
}
}
catch (e) {}

webStorage = false
return false
}

function getClientProperties () {
return {
has: {
touch: (() => !!('ontouchstart' in document.documentElement) || window.navigator.msMaxTouchPoints > 0)(),
webStorage
webStorage: hasWebStorage()
},
within: {
iframe: window.self !== window.top
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/web-storage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { onSSR } from './platform'
import { onSSR, hasWebStorage } from './platform'

function encode (value) {
if (Object.prototype.toString.call(value) === '[object Date]') {
Expand Down Expand Up @@ -130,7 +130,7 @@ export const LocalStorage = {
return
}

if ($q.platform.has.webStorage) {
if (hasWebStorage()) {
const storage = getStorage('local')
$q.localStorage = storage
Object.assign(this, storage)
Expand All @@ -145,7 +145,7 @@ export const SessionStorage = {
return
}

if ($q.platform.has.webStorage) {
if (hasWebStorage()) {
const storage = getStorage('session')
$q.sessionStorage = storage
Object.assign(this, storage)
Expand Down

0 comments on commit 78d5d23

Please sign in to comment.