Skip to content

Commit

Permalink
fix(ssr): support ant design pro ssr (#11702)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenshuai2144 authored Nov 4, 2023
1 parent 553711a commit a830383
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 38 deletions.
25 changes: 6 additions & 19 deletions examples/ant-design-pro/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,28 +313,15 @@ export default defineConfig({
mfsu: {
// esbuild: true,
},
chainWebpack(memo: any) {
chainWebpack(memo) {
memo.plugin('monaco-editor').use(MonacoEditorWebpackPlugin, []);
return memo;
},
// openAPI: [
// {
// requestLibPath: "import { request } from 'umi'",
// // 或者使用在线的版本
// // schemaPath: "https://gw.alipayobjects.com/os/antfincdn/M%24jrzTTYJN/oneapi.json"
// schemaPath: join(__dirname, 'oneapi.json'),
// mock: false,
// },
// {
// requestLibPath: "import { request } from 'umi'",
// schemaPath: 'https://gw.alipayobjects.com/os/antfincdn/CA1dOm%2631B/openapi.json',
// projectName: 'swagger',
// },
// ],
// nodeModulesTransform: {
// type: 'none',
// },
// exportStatic: {},
ssr: {
builder: 'webpack',
platform: 'node',
},
exportStatic: {},
codeSplitting: {
jsStrategy: 'granularChunks',
},
Expand Down
11 changes: 8 additions & 3 deletions packages/bundler-webpack/src/config/ssrPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {
Compiler,
Compilation,
Compiler,
} from '@umijs/bundler-webpack/compiled/webpack';
import { sources } from '@umijs/bundler-webpack/compiled/webpack';
import { fsExtra } from '@umijs/utils';
Expand All @@ -23,9 +23,11 @@ const PLUGIN_NAME = 'SSR_PLUGIN';
class SSRPlugin {
opts: IOpts;
manifest: Map<string, string>;
isGenManifest: boolean;
constructor(opts: IOpts) {
this.opts = opts;
this.manifest = new Map();
this.isGenManifest = false;
}
apply(compiler: Compiler) {
// ref: https://github.com/webdeveric/webpack-assets-manifest
Expand Down Expand Up @@ -70,13 +72,16 @@ class SSRPlugin {
2,
);
if (
process.env.NODE_ENV === 'production' ||
this.opts.userConfig.writeToDisk
(process.env.NODE_ENV === 'production' ||
this.opts.userConfig.writeToDisk) &&
!this.isGenManifest
) {
// 如果已经生成了,就不管了。不然会报错重复添加
compilation.emitAsset(
'build-manifest.json',
new sources.RawSource(assetsSource, false),
);
this.isGenManifest = true;
} else {
const outputPath = compiler.options.output.path!;
fsExtra.mkdirpSync(outputPath);
Expand Down
5 changes: 3 additions & 2 deletions packages/preset-umi/src/features/exportStatic/exportStatic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ function getExportHtmlData(routes: Record<string, IRoute>): IExportHtmlItem[] {
if (
// skip layout
!route.isLayout &&
route?.path &&
// skip dynamic route for win, because `:` is not allowed in file name
(!IS_WIN || !route.path.includes('/:')) &&
(!IS_WIN || !route?.path?.includes('/:')) &&
// skip `*` route, because `*` is not working for most site serve services
(!route.path.includes('*') ||
(!route?.path?.includes('*') ||
// except `404.html`
is404)
) {
Expand Down
41 changes: 27 additions & 14 deletions packages/server/src/ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,20 @@ function createJSXGenerator(opts: CreateRequestHandlerOptions) {
.map(
(id: string) =>
new Promise<void>(async (resolve) => {
loaderData[id] = await executeLoader(id, routesWithServerLoader, serverLoaderArgs);
loaderData[id] = await executeLoader(
id,
routesWithServerLoader,
serverLoaderArgs,
);
resolve();
}),
),
);

const manifest =
typeof opts.manifest === 'function' ? opts.manifest(sourceDir) : opts.manifest;
typeof opts.manifest === 'function'
? opts.manifest(sourceDir)
: opts.manifest;
const context = {
routes,
routeComponents,
Expand Down Expand Up @@ -167,11 +173,11 @@ export function createMarkupGenerator(opts: CreateRequestHandlerOptions) {
html = html.replace(
/(<\/head>)/,
[
opts.helmetContext.helmet.title.toString(),
opts.helmetContext.helmet.priority.toString(),
opts.helmetContext.helmet.meta.toString(),
opts.helmetContext.helmet.link.toString(),
opts.helmetContext.helmet.script.toString(),
opts.helmetContext.helmet?.title?.toString(),
opts.helmetContext.helmet?.priority?.toString(),
opts.helmetContext.helmet?.meta?.toString(),
opts.helmetContext.helmet?.link?.toString(),
opts.helmetContext.helmet?.script?.toString(),
'$1',
]
.filter(Boolean)
Expand Down Expand Up @@ -217,9 +223,12 @@ export default function createRequestHandler(
return;
}

const request = new Request(req.protocol + '://' + req.get('host') + req.originalUrl, {
headers: req.headers,
});
const request = new Request(
req.protocol + '://' + req.get('host') + req.originalUrl,
{
headers: req.headers,
},
);
const jsx = await jsxGeneratorDeferrer(req.url, { request });

if (!jsx) return next();
Expand Down Expand Up @@ -255,10 +264,12 @@ export function createUmiHandler(opts: CreateRequestHandlerOptions) {
...opts,
...params,
});
const jsx = await jsxGeneratorDeferrer(new URL(req.url).pathname, { request: req });
const jsx = await jsxGeneratorDeferrer(new URL(req.url).pathname, {
request: req,
});

if (!jsx) {
throw new Error('no page resource')
throw new Error('no page resource');
}

return ReactDomServer.renderToNodeStream(jsx.element);
Expand All @@ -269,7 +280,9 @@ export function createUmiServerLoader(opts: CreateRequestHandlerOptions) {
return async function (req: Request) {
const query = Object.fromEntries(new URL(req.url).searchParams);
// 切换路由场景下,会通过此 API 执行 server loader
return await executeLoader(query.route, opts.routesWithServerLoader, { request: req });
return await executeLoader(query.route, opts.routesWithServerLoader, {
request: req,
});
};
}

Expand Down Expand Up @@ -311,7 +324,7 @@ function createClientRoute(route: any) {
async function executeLoader(
routeKey: string,
routesWithServerLoader: RouteLoaders,
serverLoaderArgs?: IServerLoaderArgs
serverLoaderArgs?: IServerLoaderArgs,
) {
const mod = await routesWithServerLoader[routeKey]();
if (!mod.serverLoader || typeof mod.serverLoader !== 'function') {
Expand Down

0 comments on commit a830383

Please sign in to comment.