Skip to content

Commit

Permalink
fix: use CSR for dev and prod, use localStorage as state storage
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-wicki committed Sep 16, 2021
1 parent 29c09c5 commit 0e8bb54
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 65 deletions.
5 changes: 5 additions & 0 deletions ui/packages/app/web/src/components/NoSSR.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// simple wrapper that needs to be imported via next/dynamic.
// this way we CSR the app in dev like in prod (SSG'd).
const NoSSR = ({ children }) => <>{children}</>

export default NoSSR
27 changes: 14 additions & 13 deletions ui/packages/app/web/src/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import dynamic from 'next/dynamic'
import { Container } from 'react-bootstrap'
import 'react-dates/lib/css/_datepicker.css'
import { StoreProvider, useCreateStore } from 'store'
Expand All @@ -10,22 +11,22 @@ import './App.scss'
import Header from './layouts/Header'
import ThemeProvider from './layouts/ThemeProvider'

const NoSSR = dynamic(() => import('../components/NoSSR'), { ssr: false })

const App = ({ Component, pageProps }) => {
const { persistedState } = pageProps
// this is only point where persisted state can come in. it's either from:
// - cookies headers (server)
// - window.__NEXT_DATA__ (client)
const createStore = useCreateStore(persistedState?.state)
const createStore = useCreateStore()

return (
<StoreProvider createStore={createStore}>
<ThemeProvider>
<Header />
<Container fluid>
<Component {...pageProps} />
</Container>
</ThemeProvider>
</StoreProvider>
<NoSSR>
<StoreProvider createStore={createStore}>
<ThemeProvider>
<Header />
<Container fluid>
<Component {...pageProps} />
</Container>
</ThemeProvider>
</StoreProvider>
</NoSSR>
)
}

Expand Down
2 changes: 1 addition & 1 deletion ui/packages/app/web/src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { QueryServiceClient } from '@parca/client'
import ProfileExplorer from 'components/ProfileExplorer'
import { NextRouter, withRouter } from 'next/router'
import { QueryServiceClient } from '@parca/client'

const apiEndpoint = process.env.NEXT_PUBLIC_API_ENDPOINT

Expand Down
15 changes: 0 additions & 15 deletions ui/packages/app/web/src/store/cookie-storage.ts

This file was deleted.

36 changes: 8 additions & 28 deletions ui/packages/app/web/src/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { useLayoutEffect } from 'react'
import create from 'zustand'
import createContext from 'zustand/context'
import { persist } from 'zustand/middleware'
import CookieStorage from './cookie-storage'
import NoopStorage from './noop-storage'
import createUiState from './ui.state'

let store
Expand All @@ -23,40 +20,23 @@ export const StoreProvider = zustandContext.Provider

export const useStore = zustandContext.useStore

export const initializeStore = (initialState = {}) => {
export const initializeStore = () => {
return create(
persist(
(set, get) => ({
...stateSlices(set, get),
...initialState
}),
{
name: 'parca',
whitelist: whitelistPersist,
getStorage: () => (typeof document !== 'undefined' ? CookieStorage : NoopStorage)
}
)
persist((set, get) => stateSlices(set, get), {
name: 'parca',
whitelist: whitelistPersist
})
)
}

export function useCreateStore(initialState) {
export function useCreateStore() {
// For SSR & SSG, always use a new store.
if (typeof window === 'undefined') {
return () => initializeStore(initialState)
return () => initializeStore()
}

// For CSR, always re-use same store.
store = store ?? initializeStore(initialState)
// And if initialState changes, then merge states in the next render cycle.
// @todo does initialState ever change?
useLayoutEffect(() => {
if (initialState && store) {
store.setState({
...store.getState(),
...initialState
})
}
}, [initialState])
store = store ?? initializeStore()

return () => store
}
8 changes: 0 additions & 8 deletions ui/packages/app/web/src/store/noop-storage.ts

This file was deleted.

0 comments on commit 0e8bb54

Please sign in to comment.