doesitarm/helpers/astro/request.js
ThatGuySam 820e495d2d 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
2026-04-06 10:31:57 -05:00

75 lines
2 KiB
JavaScript

import { getNetlifyRedirect } from '~/helpers/config-node.js'
const ONE_SECOND = 1
const ONE_MINUTE = 60
const ONE_HOUR = 60 * 60
const TWELVE_HOURS = 12 * ONE_HOUR
function trimTrailingSlash ( url ) {
return url.replace( /\/$/, '' )
}
// We use a set of top level list pages
// so that our list searching time is as fast as possible
const topLevelListPages = new Set([
'', // Homepage
'/games',
'/devices',
'/benchmarks',
'/categories'
])
function getMaxAgeForUrl ( Astro ) {
const requestUrl = new URL( Astro.request.url )
const urlPath = trimTrailingSlash( requestUrl.pathname )
// Top level list pages
if ( topLevelListPages.has( urlPath ) ) {
return ONE_MINUTE
}
// Kind page
if ( requestUrl.pathname.startsWith( '/kind/' ) ) {
return ONE_MINUTE
}
return ONE_SECOND
}
export async function applyResponseDefaults ( Astro ) {
const maxAgeSeconds = getMaxAgeForUrl( Astro )
// 'stale-while-revalidate' tells Cloudflare to cache the response for X seconds
// but update the cache in the background
// so the user always gets a fast and cached response.
//
// This also means our Astro SSR entry function on Netlify
// only gets called from Cloudflare instead of the user's browser
// so that our actual number of requests to Netlify are minimized.
//
// Cache-Control: s-maxage=1, stale-while-revalidate
Astro.response.headers.set( 'Cache-Control', `s-maxage=${ maxAgeSeconds }, stale-while-revalidate` )
}
export async function catchRedirectResponse ( Astro ) {
const requestUrl = new URL( Astro.request.url )
let netlifyRedirectUrl = null
try {
netlifyRedirectUrl = await getNetlifyRedirect( requestUrl.pathname )
} catch ( error ) {
console.warn( `Skipping redirect lookup for ${ requestUrl.pathname }`, error )
}
// console.log('netlifyRedirectUrl', netlifyRedirectUrl)
if ( netlifyRedirectUrl !== null ) {
return Astro.redirect( netlifyRedirectUrl.to )
}
return null
}