Skip to content

Commit

Permalink
fix(netlify, vercel): explicit server rendering with disabled cache/s…
Browse files Browse the repository at this point in the history
…wr (#829)
  • Loading branch information
danielroe authored Jan 11, 2023
1 parent 5d5b62e commit bece791
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 11 deletions.
14 changes: 10 additions & 4 deletions src/presets/netlify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
29 changes: 22 additions & 7 deletions src/presets/vercel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(/^(.*)\/\*\*/, "(?<url>$1/.*)"),
dest: generateEndpoint(key) + "?url=$url",
})),
.map(([key, value]) => {
const src = key.replace(/^(.*)\/\*\*/, "(?<url>$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 ||
Expand Down
1 change: 1 addition & 0 deletions test/presets/netlify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
4 changes: 4 additions & 0 deletions test/presets/vercel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ describe("nitro:preset:vercel", async () => {
{
"handle": "filesystem",
},
{
"dest": "/__nitro",
"src": "/rules/dynamic",
},
{
"dest": "/__nitro--rules-swr?url=$url",
"src": "(?<url>/rules/swr/.*)",
Expand Down
1 change: 1 addition & 0 deletions test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down

0 comments on commit bece791

Please sign in to comment.