-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
42c1966
feature: 新增用于CDN边缘计算云函数的SSR请求handle
6b37a59
feature: 新增用于CDN边缘计算云函数的SSR请求handle
59aff84
feature: 新增用于CDN边缘计算云函数的SSR请求handle
d4c7dc3
feature: 新增用于CDN边缘计算云函数的SSR请求handle
5f8a544
feature: 新增用于CDN边缘计算云函数的SSR请求handle
de4aae8
feature: 新增用于CDN边缘计算云函数的SSR请求handle
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
@@ -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]); | ||
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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
concat
会引发较大的性能问题,换成解构会不会更好