diff --git a/packages/angular/build/BUILD.bazel b/packages/angular/build/BUILD.bazel index 9ff12a1b770a..45a284740a07 100644 --- a/packages/angular/build/BUILD.bazel +++ b/packages/angular/build/BUILD.bazel @@ -56,6 +56,7 @@ ts_library( module_root = "src/index.d.ts", deps = [ "//packages/angular/ssr", + "//packages/angular/ssr/node", "//packages/angular_devkit/architect", "@npm//@ampproject/remapping", "@npm//@angular/common", diff --git a/packages/angular/build/src/tools/vite/middlewares/ssr-middleware.ts b/packages/angular/build/src/tools/vite/middlewares/ssr-middleware.ts index 98ce95f61a5a..a45d28103160 100644 --- a/packages/angular/build/src/tools/vite/middlewares/ssr-middleware.ts +++ b/packages/angular/build/src/tools/vite/middlewares/ssr-middleware.ts @@ -9,6 +9,7 @@ import type { ɵgetOrCreateAngularServerApp as getOrCreateAngularServerApp } from '@angular/ssr'; import type { ServerResponse } from 'node:http'; import type { Connect, ViteDevServer } from 'vite'; +import { loadEsmModule } from '../../../utils/load-esm'; export function createAngularSSRMiddleware( server: ViteDevServer, @@ -30,6 +31,9 @@ export function createAngularSSRMiddleware( const url = new URL(req.url, baseUrl); (async () => { + const { writeResponseToNodeResponse, createWebRequestFromNodeRequest } = + await loadEsmModule('@angular/ssr/node'); + const { ɵgetOrCreateAngularServerApp } = (await server.ssrLoadModule('/main.server.mjs')) as { ɵgetOrCreateAngularServerApp: typeof getOrCreateAngularServerApp; }; @@ -46,20 +50,15 @@ export function createAngularSSRMiddleware( cachedAngularServerApp = angularServerApp; } - const response = await angularServerApp.render( - new Request(url, { signal: AbortSignal.timeout(30_000) }), - undefined, - ); - - return response?.text(); - })() - .then((content) => { - if (typeof content !== 'string') { - return next(); - } + const webReq = new Request(createWebRequestFromNodeRequest(req), { + signal: AbortSignal.timeout(30_000), + }); + const webRes = await angularServerApp.render(webReq); + if (!webRes) { + return next(); + } - res.end(content); - }) - .catch((error) => next(error)); + return writeResponseToNodeResponse(webRes, res); + })().catch(next); }; }