Skip to content

Commit

Permalink
fix(ssr): beforeRenderServer before routes require (#5033)
Browse files Browse the repository at this point in the history
* fix(ssr): renderServer before routes require

* chore: routes init once

* chore: format
  • Loading branch information
ycjcl868 committed Jul 14, 2020
1 parent 11c23fe commit 9ec8d41
Showing 1 changed file with 35 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,12 @@ import { IServerRender } from '@umijs/types';
import { ApplyPluginsType, createMemoryHistory{{ #DynamicImport }}, dynamic{{ /DynamicImport }} } from '{{{ RuntimePath }}}';
import { plugin } from './plugin';

// 主要为后面支持按需服务端渲染,单独用 routes 会全编译
const routes = {{{ Routes }}};

// allow user to extend routes
plugin.applyPlugins({
key: 'patchRoutes',
type: ApplyPluginsType.event,
args: { routes },
});

// origin require module
// https://github.com/webpack/webpack/issues/4175#issuecomment-342931035
const requireFunc = typeof __webpack_require__ === "function" ? __non_webpack_require__ : require;

let routes;

/**
* server render function
* @param params
Expand Down Expand Up @@ -53,6 +45,39 @@ const render: IServerRender = async (params) => {
const history = createMemoryHistory({
initialEntries: [format(location)],
});

/**
* beforeRenderServer hook, for polyfill global.*
*/
await plugin.applyPlugins({
key: 'ssr.beforeRenderServer',
type: ApplyPluginsType.event,
args: {
env,
path,
context,
history,
mode,
location,
},
async: true,
});

/**
* routes init and patch only once
* beforeRenderServer must before routes init avoding require error
*/
if (!routes) {
// 主要为后面支持按需服务端渲染,单独用 routes 会全编译
routes = {{{ Routes }}};
// allow user to extend routes
plugin.applyPlugins({
key: 'patchRoutes',
type: ApplyPluginsType.event,
args: { routes },
});
}

// for renderServer
const opts = {
path,
Expand All @@ -75,21 +100,6 @@ const render: IServerRender = async (params) => {
} catch (_) {}
}

// beforeRenderServer hook, for polyfill global.*
await plugin.applyPlugins({
key: 'ssr.beforeRenderServer',
type: ApplyPluginsType.event,
args: {
env,
path,
context,
history,
mode,
location,
},
async: true,
});

// renderServer get rootContainer
const { pageHTML, pageInitialProps, routesMatched } = await renderServer(opts);
rootContainer = pageHTML;
Expand Down

0 comments on commit 9ec8d41

Please sign in to comment.