From 1b4bff210cdc59c75c8c081d92aae9e4aa5d6446 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A5=87=E9=A3=8E?= Date: Thu, 19 Oct 2023 13:56:38 +0800 Subject: [PATCH] =?UTF-8?q?feature:=20=E6=94=AF=E6=8C=81=E5=9C=A8serverLoa?= =?UTF-8?q?der=E4=B8=AD=E8=AF=BB=E5=8F=96=E5=BD=93=E5=89=8Drequest?= =?UTF-8?q?=EF=BC=8C=E4=BB=A5=E5=AE=9E=E7=8E=B0=E5=9C=A8serverLoader?= =?UTF-8?q?=E4=B8=AD=E8=AF=BB=E5=8F=96url=E4=B8=AD=E7=9A=84=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E5=92=8Cheaders?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/server/src/ssr.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/server/src/ssr.ts b/packages/server/src/ssr.ts index 04a5e13ed51a..f74fa84f46d1 100644 --- a/packages/server/src/ssr.ts +++ b/packages/server/src/ssr.ts @@ -48,7 +48,7 @@ const createJSXProvider = ( }; function createJSXGenerator(opts: CreateRequestHandlerOptions) { - return async (url: string, headers?: Headers) => { + return async (request: Request) => { const { routesWithServerLoader, PluginManager, @@ -60,7 +60,7 @@ function createJSXGenerator(opts: CreateRequestHandlerOptions) { } = opts; // make import { history } from 'umi' work - createHistory({ type: 'memory', initialEntries: [url], initialIndex: 1 }); + createHistory({ type: 'memory', initialEntries: [request.url], initialIndex: 1 }); const pluginManager = PluginManager.create({ plugins: getPlugins(), @@ -78,16 +78,13 @@ function createJSXGenerator(opts: CreateRequestHandlerOptions) { }, }); - const { pathname } = new URL(url); + const { pathname } = new URL(request.url); const matches = matchRoutesForSSR(pathname, routes); if (matches.length === 0) { return; } const loaderData: { [key: string]: any } = {}; - const request = new Request(url, { - headers, - }) await Promise.all( matches .filter((id: string) => routes[id].hasServerLoader) @@ -141,7 +138,8 @@ export function createMarkupGenerator(opts: CreateRequestHandlerOptions) { const jsxGeneratorDeferrer = createJSXGenerator(opts); return async (url: string) => { - const jsx = await jsxGeneratorDeferrer(url); + const request = new Request(url); + const jsx = await jsxGeneratorDeferrer(request); if (jsx) { return new Promise(async (resolve, reject) => { const serverInsertedHTMLCallbacks: Set<() => React.ReactNode> = @@ -216,8 +214,10 @@ export default function createRequestHandler( return; } - const fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl; - const jsx = await jsxGeneratorDeferrer(fullUrl, req.headers); + const request = new Request(req.protocol + '://' + req.get('host') + req.originalUrl, { + headers: req.headers, + }); + const jsx = await jsxGeneratorDeferrer(request); if (!jsx) return next(); @@ -252,7 +252,7 @@ export function createUmiHandler(opts: CreateRequestHandlerOptions) { ...opts, ...params, }); - const jsx = await jsxGeneratorDeferrer(req.url, req.headers); + const jsx = await jsxGeneratorDeferrer(req); if (!jsx) { throw new Error('no page resource')