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): order route rules from most specific + avoid double-rendering root #768

Merged
merged 7 commits into from
Dec 21, 2022
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
22 changes: 15 additions & 7 deletions src/presets/netlify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,22 @@ async function writeRedirects(nitro: Nitro) {
const redirectsPath = join(nitro.options.output.publicDir, "_redirects");
let contents = "/* /.netlify/functions/server 200";

const rules = Object.entries(nitro.options.routeRules).sort(
(a, b) => a[0].split(/\/(?!\*)/).length - b[0].split(/\/(?!\*)/).length
);

// Rewrite static cached paths to builder functions
for (const [key] of Object.entries(nitro.options.routeRules).filter(
for (const [key] of rules.filter(
([_, routeRules]) =>
routeRules.cache && (routeRules.cache?.static || routeRules.cache?.swr)
)) {
contents =
`${key.replace("/**", "/*")}\t/.netlify/builders/server 200\n` + contents;
}

for (const [key, routeRules] of Object.entries(
nitro.options.routeRules
).filter(([_, routeRules]) => routeRules.redirect)) {
for (const [key, routeRules] of rules.filter(
([_, routeRules]) => routeRules.redirect
)) {
// TODO: Remove map when netlify support 307/308
let code = routeRules.redirect.statusCode;
code = { 307: 302, 308: 301 }[code] || code;
Expand Down Expand Up @@ -125,9 +129,13 @@ async function writeHeaders(nitro: Nitro) {
const headersPath = join(nitro.options.output.publicDir, "_headers");
let contents = "";

for (const [path, routeRules] of Object.entries(
nitro.options.routeRules
).filter(([_, routeRules]) => routeRules.headers)) {
const rules = Object.entries(nitro.options.routeRules).sort(
(a, b) => b[0].split(/\/(?!\*)/).length - a[0].split(/\/(?!\*)/).length
);

for (const [path, routeRules] of rules.filter(
([_, routeRules]) => routeRules.headers
)) {
const headers = [
path.replace("/**", "/*"),
...Object.entries({ ...routeRules.headers }).map(
Expand Down
38 changes: 25 additions & 13 deletions src/presets/vercel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ export const vercel = defineNitroPreset({
);

// Write prerender functions
for (const [key, value] of Object.entries(
nitro.options.routeRules
).filter(
const rules = Object.entries(nitro.options.routeRules).filter(
([_, value]) => value.cache && (value.cache.swr || value.cache.static)
)) {
);

for (const [key, value] of rules) {
if (!value.cache) {
continue;
} // for type support
Expand Down Expand Up @@ -121,6 +121,10 @@ export const vercelEdge = defineNitroPreset({
});

function generateBuildConfig(nitro: Nitro) {
const rules = Object.entries(nitro.options.routeRules).sort(
(a, b) => b[0].split(/\/(?!\*)/).length - a[0].split(/\/(?!\*)/).length
);

return defu(nitro.options.vercel?.config, <VercelBuildConfigV3>{
version: 3,
overrides: Object.fromEntries(
Expand All @@ -132,7 +136,7 @@ function generateBuildConfig(nitro: Nitro) {
])
),
routes: [
...Object.entries(nitro.options.routeRules)
...rules
.filter(([_, routeRules]) => routeRules.redirect || routeRules.headers)
.map(([path, routeRules]) => {
let route = {
Expand All @@ -159,10 +163,8 @@ function generateBuildConfig(nitro: Nitro) {
},
continue: true,
})),
{
handle: "filesystem",
},
...Object.entries(nitro.options.routeRules)
{ handle: "filesystem" },
...rules
.filter(
([key, value]) =>
value.cache &&
Expand All @@ -173,10 +175,20 @@ function generateBuildConfig(nitro: Nitro) {
src: key.replace(/^(.*)\/\*\*/, "(?<url>$1/.*)"),
dest: generateEndpoint(key) + "?url=$url",
})),
{
src: "/(.*)",
dest: "/__nitro",
},
// 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 ||
!(
nitro.options.routeRules["/**"].cache.swr ||
nitro.options.routeRules["/**"]?.cache.static
)
? [
{
src: "/(.*)",
dest: "/__nitro",
},
]
: []),
],
});
}
Expand Down
2 changes: 1 addition & 1 deletion test/presets/netlify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ describe("nitro:preset:netlify", async () => {
/* eslint-disable no-tabs */
expect(redirects).toMatchInlineSnapshot(`
"/rules/nested/override /other 302
/rules/nested/* /base 302
/rules/redirect/obj https://nitro.unjs.io/ 301
/rules/nested/* /base 302
/rules/redirect /base 302
/rules/swr-ttl/* /.netlify/builders/server 200
/rules/swr/* /.netlify/builders/server 200
Expand Down
28 changes: 14 additions & 14 deletions test/presets/vercel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ describe("nitro:preset:vercel", async () => {
},
},
"routes": [
{
"headers": {
"Location": "https://nitro.unjs.io/",
},
"src": "/rules/redirect/obj",
"status": 308,
},
{
"headers": {
"Location": "/other",
},
"src": "/rules/nested/override",
"status": 307,
},
{
"headers": {
"cache-control": "s-maxage=60",
Expand All @@ -65,13 +79,6 @@ describe("nitro:preset:vercel", async () => {
"src": "/rules/redirect",
"status": 307,
},
{
"headers": {
"Location": "https://nitro.unjs.io/",
},
"src": "/rules/redirect/obj",
"status": 308,
},
{
"headers": {
"Location": "/base",
Expand All @@ -80,13 +87,6 @@ describe("nitro:preset:vercel", async () => {
"src": "/rules/nested/.*",
"status": 307,
},
{
"headers": {
"Location": "/other",
},
"src": "/rules/nested/override",
"status": 307,
},
{
"continue": true,
"headers": {
Expand Down