Skip to content

Commit

Permalink
fix(core): double encoding during match of routes (#13303)
Browse files Browse the repository at this point in the history
Co-authored-by: florian-lefebvre <69633530+florian-lefebvre@users.noreply.github.com>
  • Loading branch information
ematipico and florian-lefebvre authored Feb 26, 2025
1 parent 2154ada commit 5f72a58
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/stale-oranges-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes an issue where the dev server was applying second decoding of the URL of the incoming request, causing issues for certain URLs.
2 changes: 1 addition & 1 deletion packages/astro/src/core/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export class App {
if (!pathname) {
pathname = prependForwardSlash(this.removeBase(url.pathname));
}
let routeData = matchRoute(pathname, this.#manifestData);
let routeData = matchRoute(decodeURI(pathname), this.#manifestData);

// missing routes fall-through, pre rendered are handled by static layer
if (!routeData || routeData.prerender) return undefined;
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ async function getPathsForRoute(
// NOTE: The same URL may match multiple routes in the manifest.
// Routing priority needs to be verified here for any duplicate
// paths to ensure routing priority rules are enforced in the final build.
const matchedRoute = matchRoute(staticPath, options.routesList);
const matchedRoute = matchRoute(decodeURI(staticPath), options.routesList);
return matchedRoute === route;
});

Expand Down
7 changes: 3 additions & 4 deletions packages/astro/src/core/routing/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ import { SERVER_ISLAND_BASE_PREFIX, SERVER_ISLAND_COMPONENT } from '../server-is

/** Find matching route from pathname */
export function matchRoute(pathname: string, manifest: RoutesList): RouteData | undefined {
const decodedPathname = decodeURI(pathname);
return manifest.routes.find((route) => {
return (
route.pattern.test(decodedPathname) ||
route.fallbackRoutes.some((fallbackRoute) => fallbackRoute.pattern.test(decodedPathname))
route.pattern.test(pathname) ||
route.fallbackRoutes.some((fallbackRoute) => fallbackRoute.pattern.test(pathname))
);
});
}

/** Finds all matching routes from pathname */
export function matchAllRoutes(pathname: string, manifest: RoutesList): RouteData[] {
return manifest.routes.filter((route) => route.pattern.test(decodeURI(pathname)));
return manifest.routes.filter((route) => route.pattern.test(pathname));
}

const ROUTE404_RE = /^\/404\/?$/;
Expand Down
1 change: 1 addition & 0 deletions packages/astro/test/params.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ describe('Astro.params in dev mode', () => {
const $ = cheerio.load(html);
assert.equal($('.category').text(), '你好');
});

});

describe('Astro.params in static mode', () => {
Expand Down

0 comments on commit 5f72a58

Please sign in to comment.