Skip to content

Commit

Permalink
fix: prevent shadowing with globs
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Dec 21, 2022
1 parent 846d7d4 commit a076f01
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/presets/netlify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ async function writeRedirects(nitro: Nitro) {
let contents = "/* /.netlify/functions/server 200";

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

// Rewrite static cached paths to builder functions
for (const [key] of rules.filter(
Expand Down Expand Up @@ -127,7 +127,7 @@ async function writeHeaders(nitro: Nitro) {
let contents = "";

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

for (const [path, routeRules] of rules.filter(([_, routeRules]) => routeRules.headers)) {
const headers = [
Expand Down
2 changes: 1 addition & 1 deletion src/presets/vercel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ 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)
.sort((a, b) => b[0].split(/\/(?!\*)/).length - a[0].split(/\/(?!\*)/).length)

return defu(nitro.options.vercel?.config, <VercelBuildConfigV3>{
version: 3,
Expand Down
8 changes: 4 additions & 4 deletions 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 All @@ -58,15 +58,15 @@ describe("nitro:preset:netlify", async () => {
);
/* eslint-disable no-tabs */
expect(headers).toMatchInlineSnapshot(`
"/rules/nested/*
x-test: test
/rules/headers
"/rules/headers
cache-control: s-maxage=60
/rules/cors
access-control-allow-origin: *
access-control-allowed-methods: GET
access-control-allow-headers: *
access-control-max-age: 0
/rules/nested/*
x-test: test
"
`);
/* eslint-enable no-tabs */
Expand Down
16 changes: 8 additions & 8 deletions test/presets/vercel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,6 @@ describe("nitro:preset:vercel", async () => {
"src": "/rules/redirect/obj",
"status": 308,
},
{
"headers": {
"Location": "/base",
"x-test": "test",
},
"src": "/rules/nested/.*",
"status": 307,
},
{
"headers": {
"Location": "/other",
Expand Down Expand Up @@ -87,6 +79,14 @@ describe("nitro:preset:vercel", async () => {
"src": "/rules/redirect",
"status": 307,
},
{
"headers": {
"Location": "/base",
"x-test": "test",
},
"src": "/rules/nested/.*",
"status": 307,
},
{
"continue": true,
"headers": {
Expand Down

0 comments on commit a076f01

Please sign in to comment.