Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

fix: [Issue-2437] Rewrites should support special symbols in dynamic routes #2440

Closed
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
4 changes: 2 additions & 2 deletions packages/libs/core/src/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function compileDestination(
) {
// Handle external URL redirects
const { origin, pathname, search } = new URL(destination);
const toPath = compile(pathname, { encode: encodeURIComponent });
const toPath = compile(pathname, { encode: encodeURI });
const compiledDestination = `${origin}${toPath(params)}${search}`;

// Remove trailing slash if original destination didn't have it
Expand All @@ -45,7 +45,7 @@ export function compileDestination(
// Handle all other paths. Escape all ? in case of query parameters
const escapedDestination = destination.replace(/\?/g, "\\?");
const toPath = compile(escapedDestination, {
encode: encodeURIComponent
encode: encodeURI
});
return toPath(params);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/libs/core/src/route/rewrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function getRewritePath(
}

// No-op rewrite support for pages: skip to next rewrite if path does not map to existing non-dynamic and dynamic routes
if (pageManifest && path === destination) {
if (pageManifest && path === decodeURI(destination)) {
const url = handlePageReq(
req,
destination,
Expand Down
35 changes: 35 additions & 0 deletions packages/libs/core/tests/match.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,40 @@ describe("Matcher Tests", () => {
const match = compileDestination("abc://123", {});
expect(match).toBeNull();
});

it("compiles not ASCII url for external rewrite", () => {
const externalMatch = compileDestination("https://пример.ру/:ru/:en", {
ru: "ру-ру",
en: "en-en"
});
expect(externalMatch).toEqual(
"https://xn--e1afmkfd.xn--p1ag/%D1%80%D1%83-%D1%80%D1%83/en-en"
);
});

it("compiles not ASCII url for internal rewrite", () => {
const internalMatch = compileDestination("/example/:ru/:en", {
ru: "ру-ру",
en: "en-en"
});
expect(internalMatch).toEqual("/example/%D1%80%D1%83-%D1%80%D1%83/en-en");
});

it("compiles url with allowed symbols for external rewrite", () => {
const externalMatch = compileDestination(
"https://example.com/users/:userid",
{
userid: "@testuser="
}
);
expect(externalMatch).toEqual("https://example.com/users/@testuser=");
});

it("compiles url with allowed symbols for internal rewrite", () => {
const externalMatch = compileDestination("/users/:userid", {
userid: "@testuser="
});
expect(externalMatch).toEqual("/users/@testuser=");
});
});
});
Loading