Remove localStorage support because it breaks ssr

This commit is contained in:
pikiou 2022-02-02 14:11:43 +01:00
parent b21b77e513
commit 3264336aab
5 changed files with 12 additions and 16 deletions

View File

@ -23,7 +23,6 @@ module.exports = {
__SERVER__: false, __SERVER__: false,
__LOCAL__: false, __LOCAL__: false,
__TEST__: true, __TEST__: true,
localStorage: { getItem: () => null, setItem: () => null, removeItem: () => null },
}, },
maxConcurrency: 50, maxConcurrency: 50,
maxWorkers: 1, maxWorkers: 1,

View File

@ -8,13 +8,15 @@ import Cookies from "js-cookie"
import createStore from "../store" import createStore from "../store"
import routes from "../routes" import routes from "../routes"
const storage: any = localStorage
// Get the initial state from server-side rendering // Get the initial state from server-side rendering
const initialState = window.__INITIAL_STATE__ const initialState = window.__INITIAL_STATE__
const id = +(Cookies.get("id") || storage?.getItem("id")) const id = +(Cookies.get("id") || 0)
const jwt = Cookies.get("jwt") || storage?.getItem("jwt") const jwt = Cookies.get("jwt")
if (id && jwt) {
Cookies.set("id", `${id}`, { expires: 3650 })
Cookies.set("jwt", jwt, { expires: 3650 })
}
const { store, history } = createStore({ initialState, id, jwt }) const { store, history } = createStore({ initialState, id, jwt })
const render = (Routes: RouteConfig[]) => const render = (Routes: RouteConfig[]) =>

View File

@ -3,24 +3,18 @@ import Cookies from "js-cookie"
import { VolunteerLogin } from "./volunteers" import { VolunteerLogin } from "./volunteers"
const storage: any = localStorage
export const axiosConfig: AxiosRequestConfig = { export const axiosConfig: AxiosRequestConfig = {
headers: {}, headers: {},
} }
export function setJWT(token: string, id: number): void { export function setJWT(token: string, id: number): void {
axiosConfig.headers.Authorization = `Bearer ${token}` axiosConfig.headers.Authorization = `Bearer ${token}`
storage?.setItem("jwt", token)
storage?.setItem("id", id)
Cookies.set("jwt", token, { expires: 3650 }) Cookies.set("jwt", token, { expires: 3650 })
Cookies.set("id", `${id}`, { expires: 3650 }) Cookies.set("id", `${id}`, { expires: 3650 })
} }
export function unsetJWT(): void { export function unsetJWT(): void {
delete axiosConfig.headers.Authorization delete axiosConfig.headers.Authorization
storage?.removeItem("jwt")
storage?.removeItem("id")
Cookies.remove("jwt") Cookies.remove("jwt")
Cookies.remove("id") Cookies.remove("id")

View File

@ -41,10 +41,12 @@ const createStore = ({ initialState, url, jwt, id }: Arg = {}) => {
return { store, history } return { store, history }
} }
const storage: any = localStorage const id = +(Cookies.get("id") || 0)
const id = +(Cookies.get("id") || storage?.getItem("id")) const jwt = Cookies.get("jwt")
const jwt = Cookies.get("jwt") || storage?.getItem("jwt") if (id && jwt) {
Cookies.set("id", `${id}`, { expires: 3650 })
Cookies.set("jwt", jwt, { expires: 3650 })
}
const { store } = createStore({ id, jwt }) const { store } = createStore({ id, jwt })
export type AppState = ReturnType<typeof store.getState> export type AppState = ReturnType<typeof store.getState>

View File

@ -30,7 +30,6 @@ const config: Configuration = {
raw: true, raw: true,
}), }),
new webpack.DefinePlugin({ new webpack.DefinePlugin({
localStorage: { getItem: () => null, setItem: () => null, removeItem: () => null },
"location.protocol": "http:", "location.protocol": "http:",
}), }),
], ],