Skip to content

Commit

Permalink
feat: support dva ssr (#199)
Browse files Browse the repository at this point in the history
* feat: support dva ssr

* feat: dva ssr

* chore: umi beta 6

* chore: yarn.lock

* fix: chore

* chore: yarn.lock

* chore: yarn.lock
  • Loading branch information
信鑫-King authored May 19, 2020
1 parent 70ffc80 commit 9930e41
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 100 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-test-renderer": "^16.9.0",
"umi": "^3.2.0-beta.6",
"umi": "^3.2.0",
"yorkie": "^2.0.0"
},
"gitHooks": {
Expand Down
10 changes: 7 additions & 3 deletions packages/plugin-dva/src/dva.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { plugin, history } from '../core/umiExports';

let app:any = null;

function _onCreate() {
export function _onCreate(options = {}) {
const runtimeDva = plugin.applyPlugins({
key: 'dva',
type: ApplyPluginsType.modify,
Expand All @@ -18,7 +18,8 @@ function _onCreate() {
{{{ ExtendDvaConfig }}}
...(runtimeDva.config || {}),
// @ts-ignore
...(window.g_useSSR ? { initialState: window.g_initialData } : {}),
...(typeof window !== 'undefined' && window.g_useSSR ? { initialState: window.g_initialProps } : {}),
...(options || {}),
});
{{{ EnhanceApp }}}
app.use(createLoading());
Expand All @@ -37,7 +38,10 @@ export function getApp() {
export class _DvaContainer extends Component {
constructor(props: any) {
super(props);
_onCreate();
// run only in client, avoid override server _onCreate()
if (typeof window !== 'undefined') {
_onCreate();
}
}

componentWillUnmount() {
Expand Down
4 changes: 3 additions & 1 deletion packages/plugin-dva/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ app.model({ namespace: '${basename(path, extname(path))}', ...(require('${path}'
const runtimeTpl = readFileSync(join(__dirname, 'runtime.tpl'), 'utf-8');
api.writeTmpFile({
path: 'plugin-dva/runtime.tsx',
content: Mustache.render(runtimeTpl, {}),
content: Mustache.render(runtimeTpl, {
SSR: !!api.config?.ssr,
}),
});

// exports.ts
Expand Down
21 changes: 20 additions & 1 deletion packages/plugin-dva/src/runtime.tpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
import React from 'react';
import { _DvaContainer } from './dva';
import { _DvaContainer, getApp, _onCreate } from './dva';

export function rootContainer(container) {
return React.createElement(_DvaContainer, null, container);
}

{{#SSR}}
export const ssr = {
modifyGetInitialPropsCtx: async (ctx) => {
// 服务端执行早于 constructor 中的 onCreate
if (process.env.__IS_SERVER && ctx.history) {
const tmpApp = _onCreate({
// server
history: ctx.history,
})
tmpApp.router(() => {})
tmpApp.start();
}
// 一定有 app
const { _store } = getApp();
ctx.store = _store;
},
}
{{/SSR}}
Loading

0 comments on commit 9930e41

Please sign in to comment.