From bece7912d39a5e88aa9955c57fed1be10e0374dc Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Wed, 11 Jan 2023 12:23:47 +0000 Subject: [PATCH] fix(netlify, vercel): explicit server rendering with disabled cache/swr (#829) --- src/presets/netlify.ts | 14 ++++++++++---- src/presets/vercel.ts | 29 ++++++++++++++++++++++------- test/presets/netlify.test.ts | 1 + test/presets/vercel.test.ts | 4 ++++ test/tests.ts | 1 + 5 files changed, 38 insertions(+), 11 deletions(-) diff --git a/src/presets/netlify.ts b/src/presets/netlify.ts index 875e0439a2..8cc5730235 100644 --- a/src/presets/netlify.ts +++ b/src/presets/netlify.ts @@ -89,12 +89,18 @@ async function writeRedirects(nitro: Nitro) { ); // Rewrite static cached paths to builder functions - for (const [key] of rules.filter( - ([_, routeRules]) => - routeRules.cache && (routeRules.cache?.static || routeRules.cache?.swr) + for (const [key, value] of rules.filter( + ([_, value]) => + value.cache === false || + (value.cache && value.cache.swr === false) || + (value.cache && (value.cache?.static || value.cache?.swr)) )) { contents = - `${key.replace("/**", "/*")}\t/.netlify/builders/server 200\n` + contents; + value.cache === false || value.cache.swr === false + ? `${key.replace("/**", "/*")}\t/.netlify/functions/server 200\n` + + contents + : `${key.replace("/**", "/*")}\t/.netlify/builders/server 200\n` + + contents; } for (const [key, routeRules] of rules.filter( diff --git a/src/presets/vercel.ts b/src/presets/vercel.ts index da3e5a5ff9..d2be2fea22 100644 --- a/src/presets/vercel.ts +++ b/src/presets/vercel.ts @@ -167,14 +167,29 @@ function generateBuildConfig(nitro: Nitro) { ...rules .filter( ([key, value]) => - value.cache && - (value.cache.swr || value.cache.static) && - key.includes("/**") + value.cache === false || + (value.cache && value.cache.swr === false) || + (value.cache && + (value.cache.swr || value.cache.static) && + key.includes("/**")) ) - .map(([key]) => ({ - src: key.replace(/^(.*)\/\*\*/, "(?$1/.*)"), - dest: generateEndpoint(key) + "?url=$url", - })), + .map(([key, value]) => { + const src = key.replace(/^(.*)\/\*\*/, "(?$1/.*)"); + if ( + value.cache === false || + (value.cache && value.cache.swr === false) + ) { + // we need to write a rule to avoid route being shadowed by another cache rule elsewhere + return { + src, + dest: "/__nitro", + }; + } + return { + src, + dest: generateEndpoint(key) + "?url=$url", + }; + }), // If we are using a prerender function as a fallback, then we do not need to output // the below fallback route as well ...(!nitro.options.routeRules["/**"]?.cache || diff --git a/test/presets/netlify.test.ts b/test/presets/netlify.test.ts index c8844042a5..eb101da2d2 100644 --- a/test/presets/netlify.test.ts +++ b/test/presets/netlify.test.ts @@ -47,6 +47,7 @@ describe("nitro:preset:netlify", async () => { /rules/swr-ttl/* /.netlify/builders/server 200 /rules/swr/* /.netlify/builders/server 200 /rules/static /.netlify/builders/server 200 + /rules/dynamic /.netlify/functions/server 200 /* /.netlify/functions/server 200" `); /* eslint-enable no-tabs */ diff --git a/test/presets/vercel.test.ts b/test/presets/vercel.test.ts index 84a287a598..51079876ea 100644 --- a/test/presets/vercel.test.ts +++ b/test/presets/vercel.test.ts @@ -97,6 +97,10 @@ describe("nitro:preset:vercel", async () => { { "handle": "filesystem", }, + { + "dest": "/__nitro", + "src": "/rules/dynamic", + }, { "dest": "/__nitro--rules-swr?url=$url", "src": "(?/rules/swr/.*)", diff --git a/test/tests.ts b/test/tests.ts index 6cbc11cc63..eb0fcc09ed 100644 --- a/test/tests.ts +++ b/test/tests.ts @@ -46,6 +46,7 @@ export async function setupTest(preset: string) { cors: true, headers: { "access-control-allowed-methods": "GET" }, }, + "/rules/dynamic": { cache: false }, "/rules/redirect": { redirect: "/base" }, "/rules/static": { static: true }, "/rules/swr/**": { swr: true },