Keep redirect lookups from crashing SSR pages

Dynamic Astro routes were reading Netlify redirect config through a cwd-relative path, which is fragile inside a serverless runtime and was taking detail pages down with 500s before render.

Resolve netlify.toml by searching from the module directory and current working directory, and fail open in request-time redirect lookup so a config read problem does not block page rendering.

Constraint: Netlify serverless cwd is not guaranteed to be the repo root
Rejected: Inline redirects into route modules | would duplicate platform config and drift from source of truth
Rejected: Leave redirect lookup hard-failing | one config read failure should not take down unrelated pages
Confidence: medium
Scope-risk: narrow
Reversibility: clean
Directive: Keep redirect config lookup independent of process cwd anywhere server code reads deploy config files
Tested: vitest ./test/prebuild/config-node.test.js; pnpm run netlify-build
Not-tested: live Netlify production deploy before push
This commit is contained in:
ThatGuySam 2026-04-06 10:31:57 -05:00
parent d026a5420b
commit 820e495d2d
3 changed files with 85 additions and 3 deletions

View file

@ -1,5 +1,7 @@
import TOML from '@iarna/toml'
import fs from 'fs-extra'
import path from 'path'
import { fileURLToPath } from 'url'
import pkg from '~/package.json'
import { publicRuntimeConfig } from '~/helpers/public-runtime-config.mjs'
@ -8,6 +10,7 @@ import { getRouteType } from '~/helpers/app-derived.js'
export const siteUrl = getSiteUrl()
const currentModuleDirectory = path.dirname( fileURLToPath( import.meta.url ) )
export const nuxtHead = {
// this htmlAttrs you need
@ -113,8 +116,41 @@ export const nuxtHead = {
export async function getNetlifyConfigPath () {
const searchDirectories = new Set()
// Local dev usually runs from repo root, but deployed serverless
// functions may execute from a nested working directory.
for ( const baseDirectory of [
process.cwd(),
currentModuleDirectory,
] ) {
let directory = baseDirectory
while ( true ) {
searchDirectories.add( directory )
const parentDirectory = path.dirname( directory )
if ( parentDirectory === directory ) break
directory = parentDirectory
}
}
for ( const directory of searchDirectories ) {
const configPath = path.join( directory, 'netlify.toml' )
if ( await fs.pathExists( configPath ) ) {
return configPath
}
}
throw new Error( 'Could not find netlify.toml' )
}
export async function getNetlifyConfig () {
const configPath = './netlify.toml'
const configPath = await getNetlifyConfigPath()
const tomlContent = await fs.readFile(configPath, 'utf-8')
const netlifyConfig = TOML.parse(tomlContent)