-
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: efficient HMR updates
- Loading branch information
Showing
14 changed files
with
307 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,18 @@ | ||
import React, { useContext, useMemo } from 'react' | ||
import type { PagesStaticData, PagesInternal, Theme } from '../../client' | ||
import React, { useContext } from 'react' | ||
import { dataCacheCtx } from './ssr/ctx' | ||
import { useTheme } from './state' | ||
import useAppState from './useAppState' | ||
|
||
interface Props { | ||
readonly Theme: Theme | ||
readonly pages: PagesInternal | ||
readonly routePath: string | ||
routePath: string | ||
} | ||
|
||
const PageLoader = ({ pages, routePath: routePathFromProps, Theme }: Props) => { | ||
const PageLoader = React.memo(({ routePath }: Props) => { | ||
const Theme = useTheme() | ||
const loadState = useAppState(routePath) | ||
const dataCache = useContext(dataCacheCtx) | ||
const loadState = useAppState(pages, routePathFromProps) | ||
|
||
const pagesStaticData = useMemo(() => getPublicPages(pages), [pages]) | ||
|
||
return ( | ||
<Theme | ||
loadState={loadState} | ||
loadedData={dataCache} | ||
staticData={pagesStaticData} | ||
/> | ||
) | ||
} | ||
return <Theme loadState={loadState} loadedData={dataCache} /> | ||
}) | ||
|
||
export default PageLoader | ||
|
||
// filter out internal fields inside pages | ||
function getPublicPages(pages: PagesInternal): PagesStaticData { | ||
return Object.fromEntries( | ||
Object.entries(pages).map(([path, { staticData }]) => [path, staticData]) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
declare module '@!virtual-modules/*' | ||
declare module '/@react-pages/*' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export type { Theme } from '../../client' | ||
export { useStaticData } from './state' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
import React from 'react' | ||
import ReactDOM from 'react-dom' | ||
import { Provider as Jotai } from 'jotai' | ||
import SSRContextProvider from './SSRContextProvider' | ||
import App from './App' | ||
|
||
let app = <App /> | ||
if (import.meta.hot) { | ||
app = <Jotai>{app}</Jotai> | ||
} | ||
|
||
ReactDOM.render( | ||
<SSRContextProvider> | ||
<App /> | ||
</SSRContextProvider>, | ||
<SSRContextProvider>{app}</SSRContextProvider>, | ||
document.getElementById('root') | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import { useMemo } from 'react' | ||
import { dequal } from 'dequal' | ||
import type { SetAtom } from 'jotai/core/types' | ||
import { atom, useAtom } from 'jotai' | ||
import { atomFamily, useAtomValue, useUpdateAtom } from 'jotai/utils' | ||
import type { PageLoaded, PagesStaticData, Theme } from '../../client' | ||
|
||
export let useTheme: () => Theme | ||
export let usePagePaths: () => string[] | ||
export let usePageModule: (path: string) => Promise<PageModule> | undefined | ||
export let useStaticData: UseStaticData | ||
|
||
interface PageModule { | ||
['default']: PageLoaded | ||
} | ||
|
||
interface UseStaticData { | ||
(): PagesStaticData | ||
(path: string): Record<string, any> | ||
<T>(path: string, selector: (staticData: Record<string, any>) => T): T | ||
} | ||
|
||
import initialPages from '/@react-pages/pages' | ||
import initialTheme from '/@react-pages/theme' | ||
|
||
const initialPagePaths = Object.keys(initialPages) | ||
|
||
// This HMR code assumes that our Jotai atoms are always managed | ||
// by the same Provider. It also mutates during render, which is | ||
// generally discouraged, but in this case it's okay. | ||
if (import.meta.hot) { | ||
let setTheme: SetAtom<{ Theme: Theme }> | undefined | ||
import.meta.hot!.accept('/@react-pages/theme', (module) => { | ||
setTheme?.({ Theme: module.default }) | ||
}) | ||
|
||
const themeAtom = atom({ Theme: initialTheme }) | ||
useTheme = () => { | ||
const [{ Theme }, set] = useAtom(themeAtom) | ||
setTheme = set | ||
return Theme | ||
} | ||
|
||
let setPages: SetAtom<any> | undefined | ||
import.meta.hot!.accept('/@react-pages/pages', (module) => { | ||
setPages?.(module.default) | ||
}) | ||
|
||
const pagesAtom = atom(initialPages) | ||
const pagePathsAtom = atom(initialPagePaths.sort()) | ||
const staticDataAtom = atom(toStaticData(initialPages)) | ||
|
||
const setPagesAtom = atom(null, (get, set, newPages: any) => { | ||
let newStaticData: Record<string, any> | undefined | ||
|
||
const pages = get(pagesAtom) | ||
for (const path in newPages) { | ||
const page = pages[path] | ||
const newPage = newPages[path] | ||
|
||
// Avoid changing the identity of `page.staticData` unless | ||
// a change is detected. This prevents unnecessary renders | ||
// of components that depend on `useStaticData(path)` call. | ||
if (page && dequal(page.staticData, newPage.staticData)) { | ||
newPage.staticData = page.staticData | ||
} else { | ||
newStaticData ??= {} | ||
newStaticData[path] = newPage.staticData | ||
} | ||
} | ||
|
||
// Update the `pagesAtom` every time, since no hook uses it directly. | ||
set(pagesAtom, newPages) | ||
|
||
// Avoid re-rendering `useStaticData()` callers if no data changed. | ||
if (newStaticData) { | ||
set(staticDataAtom, { | ||
...get(staticDataAtom), | ||
...newStaticData, | ||
}) | ||
} | ||
|
||
// Avoid re-rendering `usePagePaths()` callers if no paths were added/deleted. | ||
const newPagePaths = Object.keys(newPages).sort() | ||
if (!dequal(get(pagePathsAtom), newPagePaths)) { | ||
set(pagePathsAtom, newPagePaths) | ||
} | ||
}) | ||
|
||
const dataPathAtoms = atomFamily((path: string) => (get) => { | ||
const pages = get(pagesAtom) | ||
const page = pages[path] || pages['/404'] | ||
return page?.dataPath || null | ||
}) | ||
|
||
const emptyData: any = {} | ||
const staticDataAtoms = atomFamily((path: string) => (get) => { | ||
const pages = get(pagesAtom) | ||
const page = pages[path] || pages['/404'] | ||
return page?.staticData || emptyData | ||
}) | ||
|
||
usePagePaths = () => { | ||
setPages = useUpdateAtom(setPagesAtom) | ||
return useAtomValue(pagePathsAtom) | ||
} | ||
|
||
// This hook uses dynamic import with a variable, which is not supported | ||
// by Rollup, but that's okay since HMR is for development only. | ||
usePageModule = (pagePath) => { | ||
const dataPath = useAtomValue(dataPathAtoms(pagePath)) | ||
return useMemo(() => { | ||
return dataPath ? import(dataPath /* @vite-ignore */) : void 0 | ||
}, [dataPath]) | ||
} | ||
|
||
useStaticData = (pagePath?: string, selector?: Function) => { | ||
const staticData = pagePath ? staticDataAtoms(pagePath) : staticDataAtom | ||
if (selector) { | ||
const selection = useMemo( | ||
() => atom((get) => selector(get(staticData))), | ||
[staticData] | ||
) | ||
return useAtomValue(selection) | ||
} | ||
return useAtomValue(staticData) | ||
} | ||
} | ||
|
||
// Static mode | ||
else { | ||
useTheme = () => initialTheme | ||
usePagePaths = () => initialPagePaths | ||
usePageModule = (path) => { | ||
const page = initialPages[path] || initialPages['/404'] | ||
return useMemo(() => page?.data(), [page]) | ||
} | ||
useStaticData = (path?: string, selector?: Function) => { | ||
if (path) { | ||
const page = initialPages[path] || initialPages['/404'] | ||
const staticData = page?.staticData || {} | ||
return selector ? selector(staticData) : staticData | ||
} | ||
return toStaticData(initialPages) | ||
} | ||
} | ||
|
||
function toStaticData(pages: Record<string, any>) { | ||
const staticData: Record<string, any> = {} | ||
for (const path in pages) { | ||
staticData[path] = pages[path].staticData | ||
} | ||
return staticData | ||
} |
Oops, something went wrong.