Skip to content

Commit e2ba2e8

Browse files
encode "+" character in query string
1 parent 8624ae9 commit e2ba2e8

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

packages/open-next/src/core/routing/matcher.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,16 @@ export function handleRewrites<T extends RewriteDefinition>(
200200
);
201201
// We need to use a localized path if the rewrite is not locale specific
202202
const pathToUse = rewrite.locale === false ? rawPath : localizedRawPath;
203+
// We need to encode the "+" character with its UTF-8 equivalent "%20" to avoid 2 issues:
204+
// 1. The compile function from path-to-regexp will throw an error if it finds a "+" character.
205+
// https://github.com/pillarjs/path-to-regexp?tab=readme-ov-file#unexpected--or-
206+
// 2. The convertToQueryString function will replace the "+" character with %2B instead of %20.
207+
// %2B does not get interpreted as a space in the URL thus breaking the query string.
208+
const encodePlusQueryString = queryString.replaceAll("+", "%20")
203209
debug("urlParts", { pathname, protocol, hostname, queryString });
204210
const toDestinationPath = compile(escapeRegex(pathname ?? "") ?? "");
205211
const toDestinationHost = compile(escapeRegex(hostname ?? "") ?? "");
206-
const toDestinationQuery = compile(escapeRegex(queryString ?? "") ?? "");
212+
const toDestinationQuery = compile(escapeRegex(encodePlusQueryString ?? "") ?? "");
207213
let params = {
208214
// params for the source
209215
...getParamsFromSource(match(escapeRegex(rewrite?.source) ?? ""))(
@@ -219,7 +225,7 @@ export function handleRewrites<T extends RewriteDefinition>(
219225
}, {}),
220226
};
221227
const isUsingParams = Object.keys(params).length > 0;
222-
let rewrittenQuery = queryString;
228+
let rewrittenQuery = encodePlusQueryString;
223229
let rewrittenHost = hostname;
224230
let rewrittenPath = pathname;
225231
if (isUsingParams) {

0 commit comments

Comments
 (0)