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

feature: 新增用于CDN边缘计算云函数的SSR请求handle #11688

Merged
merged 6 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion packages/preset-umi/templates/server.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createHistory as createClientHistory } from './core/history';
import { getPlugins as getClientPlugins } from './core/plugin';
import { ServerInsertedHTMLContext } from './core/serverInsertedHTMLContext';
import { PluginManager } from '{{{ umiPluginPath }}}';
import createRequestHandler, { createMarkupGenerator } from '{{{ umiServerPath }}}';
import createRequestHandler, { createMarkupGenerator, createUmiHandler, createUmiServerLoader } from '{{{ umiServerPath }}}';

let helmetContext;

Expand Down Expand Up @@ -54,6 +54,8 @@ const createOpts = {
ServerInsertedHTMLContext,
};
const requestHandler = createRequestHandler(createOpts);
export const renderRoot = createUmiHandler(createOpts);
export const serverLoader = createUmiServerLoader(createOpts);

export const _markupGenerator = createMarkupGenerator(createOpts);

Expand Down
54 changes: 53 additions & 1 deletion packages/server/src/ssr.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { ReactElement } from 'react';
import * as ReactDomServer from 'react-dom/server';
import { matchRoutes } from 'react-router-dom';
import { Writable } from 'stream';
import { Writable, Readable } from 'stream';
import type { IRoutesById } from './types';

interface RouteLoaders {
Expand Down Expand Up @@ -235,6 +235,58 @@ export default function createRequestHandler(
};
}

export function createUmiHandler(
opts: CreateRequestHandlerOptions,
) {
const jsxGeneratorDeferrer = createJSXGenerator(opts);

return function (req: Request) {
return new Promise(async (resolve, reject) => {
const jsx = await jsxGeneratorDeferrer(new URL(req.url).pathname);

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

let buf = Buffer.alloc(0)
const writable = new Writable();

writable._write = (chunk, _encoding, next) => {
buf = Buffer.concat([buf, chunk]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

concat 会引发较大的性能问题,换成解构会不会更好

next();
};

writable.on('finish', async () => {
resolve(Readable.from(buf))
});

const stream = await ReactDomServer.renderToPipeableStream(jsx.element, {
bootstrapScripts: [jsx.manifest.assets['umi.js'] || '/umi.js'],
onShellReady() {
stream.pipe(writable);
},
onError(err: any) {
reject(err);
},
});
})
};
}

export function createUmiServerLoader(
opts: CreateRequestHandlerOptions,
) {
return async function (req: Request) {
const query = Object.fromEntries(new URL(req.url).searchParams)
fz6m marked this conversation as resolved.
Show resolved Hide resolved
// 切换路由场景下,会通过此 API 执行 server loader
return await executeLoader(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在 async 函数里 return await 是多此一举,会多一次性能损耗。

query.route,
opts.routesWithServerLoader,
);
};
}

function matchRoutesForSSR(reqUrl: string, routesById: IRoutesById) {
return (
matchRoutes(createClientRoutes({ routesById }), reqUrl)?.map(
Expand Down
Loading