Skip to content

Commit dc29e1a

Browse files
committed
conditionally update Angular config
1 parent 74d27cc commit dc29e1a

2 files changed

Lines changed: 48 additions & 14 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,14 @@ If you are using Angular 19 and did not opt into the App Engine Developer Previe
146146
```ts
147147
import { CommonEngine } from '@angular/ssr/node'
148148
import { render } from '@netlify/angular-runtime/common-engine.js'
149-
import type { Context } from '@netlify/edge-functions'
150149

151150
const commonEngine = new CommonEngine()
152151

153-
export async function netlifyCommonEngineHandler(request: Request, context: Context): Promise<Response> {
152+
export async function netlifyCommonEngineHandler(request: Request, context: any): Promise<Response> {
154153
// Example API endpoints can be defined here.
155154
// Uncomment and define endpoints as necessary.
156-
// if (context.url.pathname === '/api/hello') {
155+
// const pathname = new URL(request.url).pathname
156+
// if (pathname === '/api/hello') {
157157
// return Response.json({ message: 'Hello from the API' });
158158
// }
159159

src/helpers/serverModuleHelpers.js

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable max-lines */
12
import { existsSync } from 'node:fs'
23
import { readFile, writeFile, rename, rm } from 'node:fs/promises'
34
import { parse, join } from 'node:path'
@@ -18,24 +19,16 @@ const commonEngine = new CommonEngine()
1819
export 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
5447
export 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+
5779
let needSwapping = false
5880
let serverModuleLocation
5981
let 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

Comments
 (0)