Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PKG -- [fcl] Default to localStorage #1797

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/slow-peaches-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@onflow/fcl": minor
---

Use localStorage as default & export LOCAL_STORAGE/SESSION_STORAGE as helpers for fcl.storage.default configuration key
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/fcl/src/fcl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,5 @@ initServiceRegistry({coreStrategies})
// Automatically load fcl-wc plugin
// Based on the user's config
initFclWcLoader()

export {LOCAL_STORAGE, SESSION_STORAGE} from "./utils/web"
18 changes: 2 additions & 16 deletions packages/fcl/src/utils/web/default-config.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
const isServerSide = () => typeof window === "undefined"

const getSessionStorage = () => {
try {
const SESSION_STORAGE = {
can: !isServerSide(),
get: async key => JSON.parse(sessionStorage.getItem(key)),
put: async (key, value) =>
sessionStorage.setItem(key, JSON.stringify(value)),
}
return SESSION_STORAGE
} catch (error) {
return null
}
}
import {LOCAL_STORAGE} from "./storage"

export const getDefaultConfig = () => {
return {
"discovery.wallet.method.default": "IFRAME/RPC",
"fcl.storage.default": getSessionStorage(),
"fcl.storage.default": LOCAL_STORAGE,
}
}
1 change: 1 addition & 0 deletions packages/fcl/src/utils/web/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export {renderPop} from "./render-pop"
export {renderTab} from "./render-tab"
export {getDefaultConfig} from "./default-config"
export {coreStrategies} from "./coreStrategies"
export {LOCAL_STORAGE, SESSION_STORAGE} from "./storage"
29 changes: 29 additions & 0 deletions packages/fcl/src/utils/web/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const isServerSide = () => typeof window === "undefined"
const safeParseJSON = (str?: string | null) => {
if (str == null) return null
try {
return JSON.parse(str)
} catch (error) {
return null
}
}

export type StorageProvider = {
can: boolean
get: (key: string) => Promise<any>
put: (key: string, value: any) => Promise<void>
}

export const SESSION_STORAGE = {
can: !isServerSide() && !!window.sessionStorage,
get: async (key: string) => safeParseJSON(sessionStorage.getItem(key)),
put: async (key: string, value: any) =>
sessionStorage.setItem(key, JSON.stringify(value)),
}

export const LOCAL_STORAGE = {
can: !isServerSide() && !!window.localStorage,
get: async (key: string) => safeParseJSON(localStorage.getItem(key)),
put: async (key: string, value: any) =>
localStorage.setItem(key, JSON.stringify(value)),
}
Loading