Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support dva ssr #199

Merged
merged 9 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.1.0",
"umi": "^3.2.0-beta.6",
"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 } : {}),
ycjcl868 marked this conversation as resolved.
Show resolved Hide resolved
...(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() || {};
ycjcl868 marked this conversation as resolved.
Show resolved Hide resolved
ctx.store = _store;
},
}
{{/SSR}}
Loading