ssr-plugin-react 6.2.41 → 6.2.44
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +7 -0
- package/CHANGELOG.md +398 -0
- package/cjs/config/base.d.ts.map +1 -1
- package/cjs/config/base.js +2 -0
- package/cjs/config/base.js.map +1 -0
- package/cjs/config/client.js +1 -0
- package/cjs/config/client.js.map +1 -0
- package/cjs/config/index.js +1 -0
- package/cjs/config/index.js.map +1 -0
- package/cjs/config/server.js +1 -0
- package/cjs/config/server.js.map +1 -0
- package/cjs/entry/client-entry.d.ts.map +1 -1
- package/cjs/entry/client-entry.js +24 -14
- package/cjs/entry/client-entry.js.map +1 -0
- package/cjs/entry/context.d.ts +3 -1
- package/cjs/entry/context.d.ts.map +1 -1
- package/cjs/entry/context.js +7 -3
- package/cjs/entry/context.js.map +1 -0
- package/cjs/entry/create-context.d.ts.map +1 -1
- package/cjs/entry/create-context.js +4 -4
- package/cjs/entry/create-context.js.map +1 -0
- package/cjs/entry/create-router.js +1 -0
- package/cjs/entry/create-router.js.map +1 -0
- package/cjs/entry/server-entry.d.ts +2 -1
- package/cjs/entry/server-entry.d.ts.map +1 -1
- package/cjs/entry/server-entry.js +20 -7
- package/cjs/entry/server-entry.js.map +1 -0
- package/cjs/index.js +1 -0
- package/cjs/index.js.map +1 -0
- package/cjs/tools/vite.js +1 -0
- package/cjs/tools/vite.js.map +1 -0
- package/cjs/tools/webpack.js +1 -0
- package/cjs/tools/webpack.js.map +1 -0
- package/esm/config/base.d.ts.map +1 -1
- package/esm/config/base.js +2 -0
- package/esm/config/base.js.map +1 -0
- package/esm/config/client.js +1 -0
- package/esm/config/client.js.map +1 -0
- package/esm/config/index.js +1 -0
- package/esm/config/index.js.map +1 -0
- package/esm/config/server.js +1 -0
- package/esm/config/server.js.map +1 -0
- package/esm/entry/client-entry.d.ts.map +1 -1
- package/esm/entry/client-entry.js +24 -14
- package/esm/entry/client-entry.js.map +1 -0
- package/esm/entry/context.d.ts +3 -1
- package/esm/entry/context.d.ts.map +1 -1
- package/esm/entry/context.js +9 -5
- package/esm/entry/context.js.map +1 -0
- package/esm/entry/create-context.d.ts.map +1 -1
- package/esm/entry/create-context.js +5 -5
- package/esm/entry/create-context.js.map +1 -0
- package/esm/entry/create-router.js +1 -0
- package/esm/entry/create-router.js.map +1 -0
- package/esm/entry/server-entry.d.ts +2 -1
- package/esm/entry/server-entry.d.ts.map +1 -1
- package/esm/entry/server-entry.js +20 -7
- package/esm/entry/server-entry.js.map +1 -0
- package/esm/index.js +1 -0
- package/esm/index.js.map +1 -0
- package/esm/tools/vite.js +1 -0
- package/esm/tools/vite.js.map +1 -0
- package/esm/tools/webpack.js +1 -0
- package/esm/tools/webpack.js.map +1 -0
- package/package.json +2 -1
- package/src/config/base.ts +1 -0
- package/src/entry/client-entry.tsx +24 -23
- package/src/entry/context.tsx +8 -10
- package/src/entry/create-context.ts +4 -6
- package/src/entry/server-entry.tsx +20 -14
package/src/config/base.ts
CHANGED
|
@@ -138,6 +138,7 @@ const getBaseConfig = (chain: WebpackChain, isServer: boolean) => {
|
|
|
138
138
|
color: isServer ? '#f173ac' : '#45b97c'
|
|
139
139
|
}))
|
|
140
140
|
chain.plugin('ssrDefine').use(webpack.DefinePlugin, [{
|
|
141
|
+
...process.env,
|
|
141
142
|
__isBrowser__: !isServer,
|
|
142
143
|
...(isServer ? define?.server : define?.client),
|
|
143
144
|
...define?.base
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { createElement } from 'react'
|
|
2
2
|
import * as ReactDOM from 'react-dom'
|
|
3
3
|
import { BrowserRouter, Route, Switch } from 'react-router-dom'
|
|
4
4
|
import { preloadComponent, isMicro } from 'ssr-client-utils'
|
|
5
|
+
import { setStoreContext } from 'ssr-common-utils'
|
|
5
6
|
import { wrapComponent } from 'ssr-hoc-react'
|
|
6
7
|
import { LayoutProps, ReactRoutesType } from 'ssr-types-react'
|
|
8
|
+
import { STORE_CONTEXT as Context } from '_build/create-context'
|
|
7
9
|
import { Routes } from './create-router'
|
|
8
10
|
import { AppContext } from './context'
|
|
9
11
|
|
|
@@ -13,32 +15,31 @@ const clientRender = async (): Promise<void> => {
|
|
|
13
15
|
const IApp = App ?? function (props: LayoutProps) {
|
|
14
16
|
return props.children!
|
|
15
17
|
}
|
|
18
|
+
setStoreContext(Context)
|
|
16
19
|
// 客户端渲染||hydrate
|
|
17
20
|
const baseName = isMicro() ? window.clientPrefix : window.prefix
|
|
18
21
|
const routes = await preloadComponent(FeRoutes, baseName)
|
|
19
22
|
ReactDOM[window.__USE_SSR__ ? 'hydrate' : 'render'](
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
</AppContext>
|
|
41
|
-
</BrowserRouter>
|
|
23
|
+
createElement(BrowserRouter, {
|
|
24
|
+
basename: baseName
|
|
25
|
+
}, createElement(AppContext as any, {
|
|
26
|
+
children: createElement(Switch, null,
|
|
27
|
+
createElement(IApp as any, null, createElement(Switch, null, // 使用高阶组件wrapComponent使得csr首次进入页面以及csr/ssr切换路由时调用getInitialProps
|
|
28
|
+
routes.map(item => {
|
|
29
|
+
const { fetch, component, path } = item
|
|
30
|
+
component.fetch = fetch
|
|
31
|
+
component.layoutFetch = layoutFetch
|
|
32
|
+
const WrappedComponent = wrapComponent(component)
|
|
33
|
+
return createElement(Route, {
|
|
34
|
+
exact: true,
|
|
35
|
+
key: path,
|
|
36
|
+
path: path,
|
|
37
|
+
render: () => createElement(WrappedComponent, {
|
|
38
|
+
key: location.pathname
|
|
39
|
+
})
|
|
40
|
+
})
|
|
41
|
+
}))))
|
|
42
|
+
}))
|
|
42
43
|
, document.getElementById('app'))
|
|
43
44
|
|
|
44
45
|
}
|
package/src/entry/context.tsx
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { useReducer } from 'react'
|
|
1
|
+
import { useReducer, createElement } from 'react'
|
|
3
2
|
import { IProps, Action, IWindow, ReactRoutesType } from 'ssr-types-react'
|
|
4
|
-
|
|
5
|
-
// @ts-expect-error
|
|
6
|
-
import { STORE_CONTEXT } from '_build/create-context'
|
|
3
|
+
import { STORE_CONTEXT as Context } from '_build/create-context'
|
|
7
4
|
import { Routes } from './create-router'
|
|
8
5
|
|
|
9
6
|
const { reducer, state } = Routes as ReactRoutesType
|
|
@@ -34,9 +31,10 @@ function combineReducer (state: any, action: any) {
|
|
|
34
31
|
}
|
|
35
32
|
export function AppContext (props: IProps) {
|
|
36
33
|
const [state, dispatch] = useReducer(combineReducer, initialState)
|
|
37
|
-
return (
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
34
|
+
return createElement(Context.Provider, {
|
|
35
|
+
value: {
|
|
36
|
+
state,
|
|
37
|
+
dispatch
|
|
38
|
+
}
|
|
39
|
+
}, props.children)
|
|
42
40
|
}
|
|
@@ -1,17 +1,15 @@
|
|
|
1
|
-
//
|
|
2
|
-
// The file is provisional,don't depend on it
|
|
1
|
+
// The file is provisional,don't modify it
|
|
3
2
|
|
|
4
|
-
import
|
|
3
|
+
import { Context, createContext } from 'react'
|
|
5
4
|
import { IContext } from 'ssr-types-react'
|
|
6
|
-
|
|
7
5
|
let STORE_CONTEXT: Context<IContext>
|
|
8
6
|
if (__isBrowser__) {
|
|
9
|
-
STORE_CONTEXT = window.STORE_CONTEXT ||
|
|
7
|
+
STORE_CONTEXT = window.STORE_CONTEXT || createContext<IContext>({
|
|
10
8
|
state: {}
|
|
11
9
|
})
|
|
12
10
|
window.STORE_CONTEXT = STORE_CONTEXT
|
|
13
11
|
} else {
|
|
14
|
-
STORE_CONTEXT =
|
|
12
|
+
STORE_CONTEXT = createContext<IContext>({
|
|
15
13
|
state: {}
|
|
16
14
|
})
|
|
17
15
|
}
|
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
import * as React from 'react'
|
|
2
|
+
import { createElement } from 'react'
|
|
2
3
|
import { StaticRouter } from 'react-router-dom'
|
|
3
4
|
import { findRoute, getManifest, logGreen, normalizePath, getAsyncCssChunk, getAsyncJsChunk, reactRefreshFragment } from 'ssr-server-utils'
|
|
4
5
|
import { ISSRContext, IConfig, ReactRoutesType, ReactESMPreloadFeRouteItem, DynamicFC, StaticFC } from 'ssr-types-react'
|
|
6
|
+
import { setStoreContext } from 'ssr-common-utils'
|
|
5
7
|
import { serialize } from 'ssr-serialize-javascript'
|
|
6
|
-
// @ts-expect-error
|
|
7
8
|
import { STORE_CONTEXT as Context } from '_build/create-context'
|
|
8
9
|
import { Routes } from './create-router'
|
|
9
10
|
|
|
10
11
|
const { FeRoutes, layoutFetch, state, Layout } = Routes as ReactRoutesType
|
|
11
12
|
|
|
12
|
-
const serverRender = async (ctx: ISSRContext, config: IConfig)
|
|
13
|
+
const serverRender = async (ctx: ISSRContext, config: IConfig) => {
|
|
13
14
|
const { mode, parallelFetch, prefix, isVite, isDev, clientPrefix } = config
|
|
14
15
|
const path = normalizePath(ctx.request.path, prefix)
|
|
15
16
|
const routeItem = findRoute<ReactESMPreloadFeRouteItem>(FeRoutes, path)
|
|
17
|
+
setStoreContext(Context)
|
|
16
18
|
|
|
17
19
|
if (!routeItem) {
|
|
18
20
|
throw new Error(`
|
|
@@ -23,7 +25,7 @@ const serverRender = async (ctx: ISSRContext, config: IConfig): Promise<React.Re
|
|
|
23
25
|
|
|
24
26
|
const { fetch, webpackChunkName, component } = routeItem
|
|
25
27
|
const dynamicCssOrder = await getAsyncCssChunk(ctx, webpackChunkName, config)
|
|
26
|
-
const dynamicJsOrder = await getAsyncJsChunk(ctx, config)
|
|
28
|
+
const dynamicJsOrder = await getAsyncJsChunk(ctx, webpackChunkName, config)
|
|
27
29
|
const manifest = await getManifest(config)
|
|
28
30
|
|
|
29
31
|
const injectCss = ((isVite && isDev) ? [
|
|
@@ -58,7 +60,6 @@ const serverRender = async (ctx: ISSRContext, config: IConfig): Promise<React.Re
|
|
|
58
60
|
let fetchData = {}
|
|
59
61
|
if (!isCsr) {
|
|
60
62
|
const currentFetch = fetch ? (await fetch()).default : null
|
|
61
|
-
|
|
62
63
|
// csr 下不需要服务端获取数据
|
|
63
64
|
if (parallelFetch) {
|
|
64
65
|
[layoutFetchData, fetchData] = await Promise.all([
|
|
@@ -71,18 +72,23 @@ const serverRender = async (ctx: ISSRContext, config: IConfig): Promise<React.Re
|
|
|
71
72
|
}
|
|
72
73
|
}
|
|
73
74
|
const combineData = isCsr ? null : Object.assign(state ?? {}, layoutFetchData ?? {}, fetchData ?? {})
|
|
74
|
-
const injectState = isCsr ? <script dangerouslySetInnerHTML={{ __html: `window.prefix="${prefix}"` }} /> : <script dangerouslySetInnerHTML={{
|
|
75
|
+
const injectState = isCsr ? <script dangerouslySetInnerHTML={{ __html: `window.prefix="${prefix}";${clientPrefix ? `window.clientPrefix="${clientPrefix}";` : ''}` }} /> : <script dangerouslySetInnerHTML={{
|
|
75
76
|
__html: `window.__USE_SSR__=true; window.__INITIAL_DATA__ =${serialize(combineData)}; window.prefix="${prefix}";${clientPrefix ? `window.clientPrefix="${clientPrefix}";` : ''}`
|
|
76
77
|
}} />
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
78
|
+
// with jsx type error, use createElement here
|
|
79
|
+
return createElement(StaticRouter, {
|
|
80
|
+
location: ctx.request.url,
|
|
81
|
+
basename: prefix === '/' ? undefined : prefix
|
|
82
|
+
}, createElement(Context.Provider, {
|
|
83
|
+
value: {
|
|
84
|
+
state: combineData
|
|
85
|
+
}
|
|
86
|
+
}, createElement(Layout, {
|
|
87
|
+
ctx: ctx,
|
|
88
|
+
config: config,
|
|
89
|
+
staticList: staticList,
|
|
90
|
+
injectState: injectState
|
|
91
|
+
}, createElement(Component, null))))
|
|
86
92
|
}
|
|
87
93
|
|
|
88
94
|
export {
|