From 3d037e8470d56935bbc07ad7177ac40907def660 Mon Sep 17 00:00:00 2001 From: Jordan Ribbink Date: Thu, 28 Sep 2023 10:05:54 -0700 Subject: [PATCH] Switch to localStorage (#1782) --- .changeset/slow-peaches-vanish.md | 5 +++++ packages/fcl/src/fcl.js | 6 ++++-- packages/fcl/src/utils/web/default-config.js | 17 ++--------------- packages/fcl/src/utils/web/index.js | 11 ++++++----- packages/fcl/src/utils/web/storage.js | 13 +++++++++++++ 5 files changed, 30 insertions(+), 22 deletions(-) create mode 100644 .changeset/slow-peaches-vanish.md create mode 100644 packages/fcl/src/utils/web/storage.js diff --git a/.changeset/slow-peaches-vanish.md b/.changeset/slow-peaches-vanish.md new file mode 100644 index 000000000..ac02855d1 --- /dev/null +++ b/.changeset/slow-peaches-vanish.md @@ -0,0 +1,5 @@ +--- +"@onflow/fcl": minor +--- + +Use localStorage as default & export LOCAL_STORAGE/SESSION_STORAGE as helpers for fcl.storage.default configuration key diff --git a/packages/fcl/src/fcl.js b/packages/fcl/src/fcl.js index 2b3677330..a8917b826 100644 --- a/packages/fcl/src/fcl.js +++ b/packages/fcl/src/fcl.js @@ -1,10 +1,10 @@ -export * from './shared-exports'; +export * from "./shared-exports" import {getMutate} from "./exec/mutate" export const mutate = getMutate({platform: "web"}) import {getCurrentUser} from "./current-user" -const currentUser = getCurrentUser({platform:"web"}) +const currentUser = getCurrentUser({platform: "web"}) export {currentUser} @@ -19,6 +19,8 @@ export const logIn = (opts = {}) => currentUser().authenticate(opts) export const authz = currentUser().authorization +export {LOCAL_STORAGE, SESSION_STORAGE} from "./utils/web" + import {config} from "@onflow/config" import {getDefaultConfig, coreStrategies} from "./utils/web" import {initServiceRegistry} from "./current-user/exec-service/plugins" diff --git a/packages/fcl/src/utils/web/default-config.js b/packages/fcl/src/utils/web/default-config.js index 3bbf571b0..6bc0ae177 100644 --- a/packages/fcl/src/utils/web/default-config.js +++ b/packages/fcl/src/utils/web/default-config.js @@ -1,21 +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, } } diff --git a/packages/fcl/src/utils/web/index.js b/packages/fcl/src/utils/web/index.js index edd0b8cb9..7c943c9f8 100644 --- a/packages/fcl/src/utils/web/index.js +++ b/packages/fcl/src/utils/web/index.js @@ -1,5 +1,6 @@ -export {renderFrame} from './render-frame' -export {renderPop} from './render-pop' -export {renderTab} from './render-tab' -export {getDefaultConfig} from './default-config' -export {coreStrategies} from './coreStrategies' +export {renderFrame} from "./render-frame" +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" diff --git a/packages/fcl/src/utils/web/storage.js b/packages/fcl/src/utils/web/storage.js new file mode 100644 index 000000000..3789191e4 --- /dev/null +++ b/packages/fcl/src/utils/web/storage.js @@ -0,0 +1,13 @@ +const isServerSide = () => typeof window === "undefined" + +export const SESSION_STORAGE = { + can: !isServerSide(), + get: async key => JSON.parse(sessionStorage.getItem(key)), + put: async (key, value) => sessionStorage.setItem(key, JSON.stringify(value)), +} + +export const LOCAL_STORAGE = { + can: !isServerSide(), + get: async key => JSON.parse(localStorage.getItem(key)), + put: async (key, value) => localStorage.setItem(key, JSON.stringify(value)), +}