diff --git a/src/presets/netlify/legacy/preset.ts b/src/presets/netlify/legacy/preset.ts index 9e5af03997..cdffee32f3 100644 --- a/src/presets/netlify/legacy/preset.ts +++ b/src/presets/netlify/legacy/preset.ts @@ -1,4 +1,4 @@ -import { existsSync, promises as fsp } from "node:fs"; +import { promises as fsp } from "node:fs"; import { defineNitroPreset } from "nitropack/kit"; import type { Nitro } from "nitropack/types"; import { dirname, join } from "pathe"; @@ -24,7 +24,7 @@ const netlify = defineNitroPreset( }, async compiled(nitro: Nitro) { await writeHeaders(nitro); - await writeRedirects(nitro); + await writeRedirects(nitro, "/.netlify/functions/server"); if (nitro.options.netlify) { const configPath = join( diff --git a/src/presets/netlify/legacy/utils.ts b/src/presets/netlify/legacy/utils.ts index 107f0c0d87..0c89a58adb 100644 --- a/src/presets/netlify/legacy/utils.ts +++ b/src/presets/netlify/legacy/utils.ts @@ -2,7 +2,25 @@ import { existsSync, promises as fsp } from "node:fs"; import type { Nitro } from "nitropack/types"; import { join } from "pathe"; -export async function writeRedirects(nitro: Nitro) { +export function generateCatchAllRedirects( + nitro: Nitro, + catchAllPath?: string +): string { + if (!catchAllPath) return ""; + + return [ + // e.g.: /_nuxt/* /_nuxt/:splat 200 + // Because of Netlify CDN shadowing + // (https://docs.netlify.com/routing/redirects/rewrites-proxies/#shadowing), + // this config avoids function invocations for all static paths, even 404s. + ...getStaticPaths(nitro).map( + (path) => `${path} ${path.replace("/*", "/:splat")} 200` + ), + `/* ${catchAllPath} 200`, + ].join("\n"); +} + +export async function writeRedirects(nitro: Nitro, catchAllPath?: string) { const redirectsPath = join(nitro.options.output.publicDir, "_redirects"); const staticFallback = existsSync( join(nitro.options.output.publicDir, "404.html") @@ -11,7 +29,7 @@ export async function writeRedirects(nitro: Nitro) { : ""; let contents = nitro.options.static ? staticFallback - : "/* /.netlify/functions/server 200"; + : generateCatchAllRedirects(nitro, catchAllPath); const rules = Object.entries(nitro.options.routeRules).sort( (a, b) => a[0].split(/\/(?!\*)/).length - b[0].split(/\/(?!\*)/).length @@ -103,6 +121,13 @@ export async function writeHeaders(nitro: Nitro) { await fsp.writeFile(headersPath, contents); } +export function getStaticPaths(nitro: Nitro): string[] { + const publicAssets = nitro.options.publicAssets.filter( + (dir) => dir.fallthrough !== true && dir.baseURL && dir.baseURL !== "/" + ); + return publicAssets.map((dir) => `${dir.baseURL}/*`); +} + export function deprecateSWR(nitro: Nitro) { if (nitro.options.future.nativeSWR) { return; diff --git a/test/presets/netlify-legacy.test.ts b/test/presets/netlify-legacy.test.ts index bbd68c48f2..24abb3fa2a 100644 --- a/test/presets/netlify-legacy.test.ts +++ b/test/presets/netlify-legacy.test.ts @@ -70,6 +70,10 @@ describe("nitro:preset:netlify-legacy", async () => { /rules/isr-ttl/* /.netlify/builders/server 200 /rules/isr/* /.netlify/builders/server 200 /rules/dynamic /.netlify/functions/server 200 + /.netlify/* /.netlify/:splat 200 + /build/* /build/:splat 200 + /with-default-fallthrough/* /with-default-fallthrough/:splat 200 + /nested/no-fallthrough/* /nested/no-fallthrough/:splat 200 /* /.netlify/functions/server 200" `); });