Skip to content

Commit

Permalink
fix(config): normalize route rules with leading slash (#2978)
Browse files Browse the repository at this point in the history
  • Loading branch information
gioboa authored Jan 8, 2025
1 parent 9371b7c commit e9ec259
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 27 deletions.
4 changes: 3 additions & 1 deletion src/core/config/resolvers/route-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
NitroRouteConfig,
NitroRouteRules,
} from "nitropack/types";
import { withLeadingSlash } from "ufo";

export async function resolveRouteRulesOptions(options: NitroOptions) {
// Backward compatibility for options.routes
Expand All @@ -17,8 +18,9 @@ export function normalizeRouteRules(
config: NitroConfig
): Record<string, NitroRouteRules> {
const normalizedRules: Record<string, NitroRouteRules> = {};
for (const path in config.routeRules) {
for (let path in config.routeRules) {
const routeConfig = config.routeRules[path] as NitroRouteConfig;
path = withLeadingSlash(path);
const routeRules: NitroRouteRules = {
...routeConfig,
redirect: undefined,
Expand Down
1 change: 1 addition & 0 deletions test/fixture/nitro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export default defineNitroConfig({
"/rules/_/cached/noncached": { cache: false, swr: false, isr: false },
"/rules/_/cached/**": { swr: true },
"/api/proxy/**": { proxy: "/api/echo" },
"**": { headers: { "x-test": "test" } },
},
prerender: {
crawlLinks: true,
Expand Down
28 changes: 15 additions & 13 deletions test/presets/netlify-legacy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,21 @@ describe("nitro:preset:netlify-legacy", async () => {
);

expect(headers).toMatchInlineSnapshot(`
"/rules/headers
cache-control: s-maxage=60
/rules/cors
access-control-allow-origin: *
access-control-allow-methods: GET
access-control-allow-headers: *
access-control-max-age: 0
/rules/nested/*
x-test: test
/build/*
cache-control: public, max-age=3600, immutable
"
`);
"/rules/headers
cache-control: s-maxage=60
/rules/cors
access-control-allow-origin: *
access-control-allow-methods: GET
access-control-allow-headers: *
access-control-max-age: 0
/rules/nested/*
x-test: test
/build/*
cache-control: public, max-age=3600, immutable
/*
x-test: test
"
`);
});
it("should write config.json", async () => {
const config = await fsp
Expand Down
28 changes: 15 additions & 13 deletions test/presets/netlify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,21 @@ describe("nitro:preset:netlify", async () => {
);

expect(headers).toMatchInlineSnapshot(`
"/rules/headers
cache-control: s-maxage=60
/rules/cors
access-control-allow-origin: *
access-control-allow-methods: GET
access-control-allow-headers: *
access-control-max-age: 0
/rules/nested/*
x-test: test
/build/*
cache-control: public, max-age=3600, immutable
"
`);
"/rules/headers
cache-control: s-maxage=60
/rules/cors
access-control-allow-origin: *
access-control-allow-methods: GET
access-control-allow-headers: *
access-control-max-age: 0
/rules/nested/*
x-test: test
/build/*
cache-control: public, max-age=3600, immutable
/*
x-test: test
"
`);
});

it("writes config.json", async () => {
Expand Down
6 changes: 6 additions & 0 deletions test/presets/vercel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ describe("nitro:preset:vercel", async () => {
},
"src": "/build/(.*)",
},
{
"headers": {
"x-test": "test",
},
"src": "/(.*)",
},
{
"continue": true,
"headers": {
Expand Down

0 comments on commit e9ec259

Please sign in to comment.