Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(netlify, vercel): explicit server rendering with disabled cache/swr #829

Merged
merged 1 commit into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
Copy link
Member

@pi0 pi0 Jan 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be simplified for swr check only: !value.cache || !value.cache.swr


Asking ChatGPT: πŸ€“

image

image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that simplification is right:

  1. it drops a check for static
  2. we do care whether cache is explicitly false, in which case we want to write the rule, but if it's undefined we shouldn't force server rendering, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. The main problem is that we are not deeply normalizing the route rules at the moment and with undifined conditions, such simplification is wrong. Cache should be either false or an object. And I'm thinking the static flag could be moved outside. Lets improve later.

)) {
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