Skip to content
Merged
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
63c4b60
update deps, apply prettier, change to ESM, update tests
hrishikesh-k May 12, 2026
d723b43
update ci tests to use Node 22
hrishikesh-k May 13, 2026
a41c8f4
coderabbit 1
hrishikesh-k May 13, 2026
5228f75
remove exclude
hrishikesh-k May 13, 2026
a772d28
update lockfiles
hrishikesh-k May 13, 2026
aa3369d
lint fixes
hrishikesh-k May 13, 2026
a87042c
fix prettier config
hrishikesh-k May 13, 2026
f17a9b0
fix npm ci
hrishikesh-k May 13, 2026
2d4a587
coderabbit 2
hrishikesh-k May 13, 2026
0fed716
downgrade and pin angular
hrishikesh-k May 13, 2026
a4c1f7e
update engine version range to match Angular
hrishikesh-k May 13, 2026
49f5442
fix version and readme
hrishikesh-k May 13, 2026
8ee6796
update readme to suggest latest CLI
hrishikesh-k May 13, 2026
1967b06
add allowedHosts and trustedProxyHeaders
hrishikesh-k May 14, 2026
43517b0
Merge branch 'main' into hk/angular-22
hrishikesh-k May 14, 2026
e47da59
regenrate lockfiles
hrishikesh-k May 14, 2026
65c394d
fix type for getContext
hrishikesh-k May 15, 2026
766344a
update readme for deploy instructions
hrishikesh-k May 15, 2026
ed297f1
prevent missing env vars from failing build
hrishikesh-k May 15, 2026
306b642
fix for loop bug
hrishikesh-k May 15, 2026
a3cb3b7
update swapped file templates
hrishikesh-k May 15, 2026
fc84504
Merge branch 'main' into hk/angular-22
hrishikesh-k May 15, 2026
9877951
fix context potentially being undefined
hrishikesh-k May 15, 2026
74d27cc
consolidate app-engine exports in a single file
hrishikesh-k May 15, 2026
dc29e1a
conditionally update Angular config
hrishikesh-k May 15, 2026
846d7fc
revert pathname to request
hrishikesh-k May 15, 2026
35aa5cb
fix typo
hrishikesh-k May 15, 2026
065cff2
Merge branch 'main' into hk/angular-22
hrishikesh-k May 18, 2026
4a44694
fix merge conflicts
hrishikesh-k May 18, 2026
ca53395
Merge branch 'main' into hk/angular-22
hrishikesh-k May 18, 2026
285d979
match version of angular/ssr instead of core
hrishikesh-k May 18, 2026
5cafda5
Update README.md
hrishikesh-k May 18, 2026
d8af697
seperate version detection for angular/core and ssr
hrishikesh-k May 18, 2026
08952e1
update tests
hrishikesh-k May 18, 2026
095815b
update failBuild message and remove dead code
hrishikesh-k May 18, 2026
6058b19
Merge branch 'main' into hk/angular-22
hrishikesh-k May 19, 2026
3d96443
update readme
hrishikesh-k May 21, 2026
36a92f3
allow specifying own hosts
hrishikesh-k May 25, 2026
e66d9ea
Merge branch 'main' into hk/angular-22
hrishikesh-k May 25, 2026
3e6508d
don't inject defaults when allowedHosts includes `*`
hrishikesh-k May 25, 2026
27aa870
allow trusting all proxy headers
hrishikesh-k May 25, 2026
edc7fce
fix set logic
hrishikesh-k May 25, 2026
3d15f4c
fix type definition
hrishikesh-k May 25, 2026
0d7a751
prettify
hrishikesh-k May 25, 2026
13bb97a
remove unnecessary config options
hrishikesh-k May 26, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 120 additions & 32 deletions src/app-engine.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,142 @@
import { env } from 'node:process'

/**
* Generate `allowedHosts` config for `AngularAppEngine` from `@angular/ssr`
* @returns {string[]}
*/
export function getAllowedHosts() {
const allowedHosts = []
const defaultAllowedHosts = []

let deployId
let deployPrimeUrlHostname
let siteId
let siteName

const environmentVariables = ['DEPLOY_ID', 'DEPLOY_PRIME_URL', 'DEPLOY_URL', 'SITE_ID', 'SITE_NAME', 'URL']

for (const environmentVariable of environmentVariables) {
if (!env[environmentVariable] || env[environmentVariable] === 'undefined') {
console.warn(
`Missing Netlify-specific environment variable ${environmentVariable}. \`allowedHosts\` config might be incomplete.`,
)
return allowedHosts
switch (environmentVariable) {
case 'DEPLOY_ID':
// not setting this directly above so that validation
// and warnings remain centralized in the helper
deployId = getEnvironmentVariable(environmentVariable)
break
case 'DEPLOY_PRIME_URL':
// DEPLOY_PRIME_URL:
// <branch-name>--<subdomain>.netlify.app or
// <dp-#>--<subdomain>.netlify.app (supports ADS)
deployPrimeUrlHostname = getHostnameFromEnvironmentVariable(environmentVariable)
if (deployPrimeUrlHostname) {
defaultAllowedHosts.push(deployPrimeUrlHostname)
}
break
case 'DEPLOY_URL':
case 'URL': {
// DEPLOY_URL: <deploy-id>--<subdomain>.netlify.app
// URL: <subdomain>.netlify.app OR <custom-domain>
// www handling is not required as Netlify auto-redirects
const hostname = getHostnameFromEnvironmentVariable(environmentVariable)
if (hostname) {
defaultAllowedHosts.push(hostname)
}
break
}
case 'SITE_ID':
// SITE_ID: <site_id>
// this makes it <site_id>.netlify.app
// separate handling because we need it later
siteId = getEnvironmentVariable(environmentVariable)
if (siteId) {
defaultAllowedHosts.push(`${siteId}.netlify.app`)
}
break
case 'SITE_NAME':
// SITE_NAME: <subdomain>
// this makes it <subdomain>.netlify.app
// this may duplicate URL for sites without a custom domain
// but duplicates are removed later and this is still useful
// if a custom domain is removed after deployment
siteName = getEnvironmentVariable(environmentVariable)
if (siteName) {
defaultAllowedHosts.push(`${siteName}.netlify.app`)
}
break
default:
break
}
}

const deployPrimeUrlHostname = new URL(env.DEPLOY_PRIME_URL).hostname

// <subdomain>.netlify.app OR <custom-domain>
// www handling is not required as Netlify will auto-redirect
allowedHosts.push(new URL(env.URL).hostname)
// <deploy-id>--<subdomain>.netlify.app
allowedHosts.push(new URL(env.DEPLOY_URL).hostname)
// <branch-name>--<subdomain>.netlify.app or <dp-#>--<subdomain>.netlify.app (supports ADS)
allowedHosts.push(deployPrimeUrlHostname)
// <subdomain>.netlify.app
// this will be duplicated for sites without custom domain
// but it's important to have in case a site's custom domain is removed after a deploy
allowedHosts.push(`${env.SITE_NAME}.netlify.app`)
// <site-id>.netlify.app
allowedHosts.push(`${env.SITE_ID}.netlify.app`)
// <deploy-id>--<site-id>.netlify.app
allowedHosts.push(`${env.DEPLOY_ID}--${env.SITE_ID}.netlify.app`)

// we need to extract the branch name or the deploy preview number
// so we can add the subdomain as well as site-id specific URLs
// this would be required for sites using ADS so that
// we can add netlify.app URLs as well
if (deployPrimeUrlHostname.includes('--')) {
// extract the branch name or deploy preview number
// so we can generate ADS-compatible URLs
if (deployPrimeUrlHostname?.includes('--') && siteId && siteName) {
const [branchNameOrDpNumber] = deployPrimeUrlHostname.split('--')
allowedHosts.push(`${branchNameOrDpNumber}--${env.SITE_NAME}.netlify.app`)
allowedHosts.push(`${branchNameOrDpNumber}--${env.SITE_ID}.netlify.app`)
defaultAllowedHosts.push(`${branchNameOrDpNumber}--${siteName}.netlify.app`)
defaultAllowedHosts.push(`${branchNameOrDpNumber}--${siteId}.netlify.app`)
}

if (deployId && siteId) {
// <deploy-id>--<site-id>.netlify.app
defaultAllowedHosts.push(`${deployId}--${siteId}.netlify.app`)
}

return allowedHosts
return [...new Set(defaultAllowedHosts)]
}

/**
* Return Netlify-specific context
* @returns {import('@netlify/edge-functions').Context | undefined}
*/
export function getContext() {
// eslint-disable-next-line no-undef
return typeof Netlify !== 'undefined' ? Netlify?.context : undefined
}

/**
* Return the value of an environment variable
* @param {string} environmentVariable
*
* @returns {string | undefined}
*/
function getEnvironmentVariable(environmentVariable) {
const value = env[environmentVariable]

// we inject the literal string "undefined" for variables that don't have a value
if (value == null || value === '' || value === 'undefined') {
console.warn(
`Missing Netlify-specific environment variable ${environmentVariable}. ` +
'`allowedHosts` config might be incomplete.',
)

return
}

return value
}

/**
* Return hostname from the value of an environment variable
* @param {string} environmentVariable
*
* @returns {string | undefined}
*/
function getHostnameFromEnvironmentVariable(environmentVariable) {
const value = getEnvironmentVariable(environmentVariable)

if (value == null) {
return
}

try {
return new URL(value).hostname
} catch {
console.warn(`Netlify-specific environment variable ${environmentVariable} does not contain a valid URL`)
}
}

/**
* Generate `trustProxyHeaders` config for `AngularAppEngine` from `@angular/ssr`
* @returns {string[]}
*/
export function getTrustProxyHeaders() {
return ['x-forwarded-for']
Comment thread
hrishikesh-k marked this conversation as resolved.
}
Loading