-
-
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.
implement a SSR plugin system to decouple vite-pages ssr from antd ss…
…r steps
- Loading branch information
Showing
13 changed files
with
135 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import React, { createContext, useContext } from 'react' | ||
import ReactDOM from 'react-dom/server' | ||
|
||
import { isSSR } from './utils' | ||
import type { SSRPlugin } from '../../clientTypes' | ||
export type { SSRPlugin } from '../../clientTypes' | ||
|
||
export const SSRPluginContext = createContext<SSRPluginMap>(new Map()) | ||
|
||
export type SSRPluginMap = Map<string, SSRPlugin> | ||
|
||
/** | ||
* render React tree on the server to get the server plugins that are declared by themes or users | ||
*/ | ||
export function collectSSRPlugins(app: React.ReactNode): SSRPlugin[] { | ||
const map: SSRPluginMap = new Map() | ||
ReactDOM.renderToString( | ||
<SSRPluginContext.Provider value={map}>{app}</SSRPluginContext.Provider> | ||
) | ||
return [...map.values()] | ||
} | ||
|
||
/** | ||
* Users or themes use this function to declare SSR plugin. | ||
* This function is exported from vite-plugin-react-pages/client | ||
*/ | ||
export function useSSRPlugin(plugin: SSRPlugin) { | ||
if (isSSR) { | ||
const ctxVal = useContext(SSRPluginContext) | ||
ctxVal?.set(plugin.id, plugin) | ||
} | ||
} | ||
|
||
;(globalThis as any)['__vite_pages_useSSRPlugin'] = useSSRPlugin |
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,6 +1,7 @@ | ||
import { useLayoutEffect, useEffect } from 'react' | ||
|
||
export const isSSR = import.meta.env.SSR | ||
|
||
// fix warning of useLayoutEffect during ssr | ||
// https://gist.github.com/gaearon/e7d97cdf38a2907924ea12e4ebdf3c85 | ||
export const useIsomorphicLayoutEffect = | ||
typeof window !== 'undefined' ? useLayoutEffect : useEffect | ||
export const useIsomorphicLayoutEffect = isSSR ? useEffect : useLayoutEffect |
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
This file was deleted.
Oops, something went wrong.
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,20 @@ | ||
import React from 'react' | ||
import { useSSRPlugin } from 'vite-plugin-react-pages/client' | ||
// https://ant.design/docs/react/customize-theme#server-side-render-ssr | ||
import { createCache, extractStyle, StyleProvider } from '@ant-design/cssinjs' | ||
import { isSSR } from './utils' | ||
|
||
export function useAntdSSRPlugin() { | ||
if (isSSR) { | ||
useSSRPlugin({ | ||
id: 'vite-pages-theme-doc-antd-ssr', | ||
prepare(app) { | ||
const cache = createCache() | ||
return { | ||
app: <StyleProvider cache={cache}>{app}</StyleProvider>, | ||
extractStyle: () => extractStyle(cache), | ||
} | ||
}, | ||
}) | ||
} | ||
} |
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