1+ /* eslint-disable max-lines */
12import { existsSync } from 'node:fs'
23import { readFile , writeFile , rename , rm } from 'node:fs/promises'
34import { parse , join } from 'node:path'
@@ -18,24 +19,16 @@ const commonEngine = new CommonEngine()
1819export async function netlifyCommonEngineHandler(request: Request, context: any): Promise<Response> {
1920 // Example API endpoints can be defined here.
2021 // Uncomment and define endpoints as necessary.
21- // if (context.url?.pathname === '/api/hello') {
22+ // const pathname = new URL(request.url).pathname
23+ // if (pathname === '/api/hello') {
2224 // return Response.json({ message: 'Hello from the API' });
2325 // }
2426
2527 return await render(commonEngine)
2628}
2729`
28-
2930// eslint-disable-next-line no-inline-comments
30- const NetlifyServerTsAppEngine = /* typescript */ `import { AngularAppEngine, createRequestHandler } from '@angular/ssr'
31- import { getAllowedHosts, getContext, getTrustProxyHeaders } from '@netlify/angular-runtime/app-engine.js'
32-
33- const angularAppEngine = new AngularAppEngine({
34- allowedHosts: getAllowedHosts(),
35- trustProxyHeaders: getTrustProxyHeaders(),
36- })
37-
38- export async function netlifyAppEngineHandler(request: Request): Promise<Response> {
31+ const NetlifyServerTsAppEngineCommonContent = /* typescript */ `export async function netlifyAppEngineHandler(request: Request): Promise<Response> {
3932 const context = getContext()
4033
4134 // Example API endpoints can be defined here.
@@ -54,6 +47,35 @@ export async function netlifyAppEngineHandler(request: Request): Promise<Respons
5447export const reqHandler = createRequestHandler(netlifyAppEngineHandler)
5548`
5649
50+ // eslint-disable-next-line no-inline-comments
51+ const NetlifyServerTsAppEngine21Dot2 = /* typescript */ `import { AngularAppEngine, createRequestHandler } from '@angular/ssr'
52+ import { getAllowedHosts, getContext, getTrustProxyHeaders } from '@netlify/angular-runtime/app-engine.js'
53+
54+ const angularAppEngine = new AngularAppEngine({
55+ allowedHosts: getAllowedHosts(),
56+ trustProxyHeaders: getTrustProxyHeaders(),
57+ })
58+
59+ ${ NetlifyServerTsAppEngineCommonContent } `
60+
61+ // eslint-disable-next-line no-inline-comments
62+ const NetlifyServerTsAppEngine21Dot1 = /* typescript */ `import { AngularAppEngine, createRequestHandler } from '@angular/ssr'
63+ import { getAllowedHosts, getContext } from '@netlify/angular-runtime/app-engine.js'
64+
65+ const angularAppEngine = new AngularAppEngine({
66+ allowedHosts: getAllowedHosts(),
67+ })
68+
69+ ${ NetlifyServerTsAppEngineCommonContent } `
70+
71+ // eslint-disable-next-line no-inline-comments
72+ const NetlifyServerTsAppEngine21Dot0 = /* typescript */ `import { AngularAppEngine, createRequestHandler } from '@angular/ssr'
73+ import { getContext } from '@netlify/angular-runtime/app-engine.js'
74+
75+ const angularAppEngine = new AngularAppEngine()
76+
77+ ${ NetlifyServerTsAppEngineCommonContent } `
78+
5779let needSwapping = false
5880let serverModuleLocation
5981let serverModuleBackupLocation
@@ -110,6 +132,17 @@ export async function fixServerTs({ angularVersion, siteRoot, failPlugin, failBu
110132
111133 const angularJson = getAngularJson ( { failPlugin, siteRoot, workspaceType, packagePath } )
112134
135+ // Angular v21.1 introduced `allowedHosts`: https://github.com/angular/angular-cli/blob/21.1.x/packages/angular/ssr/src/app-engine.ts
136+ // Angular v21.2 introduced `trustProxyHeaders` as well: https://github.com/angular/angular-cli/blob/21.2.x/packages/angular/ssr/src/app-engine.ts
137+ // we cannot add the config if the version doesn't support it
138+ // because TypeScript complains about invalid properties
139+ let NetlifyServerTsAppEngine = NetlifyServerTsAppEngine21Dot0
140+ if ( satisfies ( angularVersion , '>=21.2.0' , { includePrerelease : true } ) ) {
141+ NetlifyServerTsAppEngine = NetlifyServerTsAppEngine21Dot2
142+ } else if ( satisfies ( angularVersion , '>=21.1.0' , { includePrerelease : true } ) ) {
143+ NetlifyServerTsAppEngine = NetlifyServerTsAppEngine21Dot1
144+ }
145+
113146 const project = getProject ( angularJson , failBuild , workspaceType === 'nx' )
114147 const build = workspaceType === 'nx' ? project . targets . build : project . architect . build
115148
@@ -218,3 +251,4 @@ export async function revertServerTsFix() {
218251 needSwapping = false
219252 }
220253}
254+ /* eslint-enable max-lines */
0 commit comments