Skip to content

Commit

Permalink
refactor(@angular/build): update SSR Vite middleware to utilize the e…
Browse files Browse the repository at this point in the history
…nhanced API

Updated the SSR Vite middleware to align with the latest API improvements
  • Loading branch information
alan-agius4 committed Sep 19, 2024
1 parent 13a3e43 commit fedeeb0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
1 change: 1 addition & 0 deletions packages/angular/build/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
27 changes: 13 additions & 14 deletions packages/angular/build/src/tools/vite/middlewares/ssr-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -30,6 +31,9 @@ export function createAngularSSRMiddleware(
const url = new URL(req.url, baseUrl);

(async () => {
const { writeResponseToNodeResponse, createWebRequestFromNodeRequest } =
await loadEsmModule<typeof import('@angular/ssr/node')>('@angular/ssr/node');

const { ɵgetOrCreateAngularServerApp } = (await server.ssrLoadModule('/main.server.mjs')) as {
ɵgetOrCreateAngularServerApp: typeof getOrCreateAngularServerApp;
};
Expand All @@ -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);
};
}

0 comments on commit fedeeb0

Please sign in to comment.