Skip to content

Commit 7786a7c

Browse files
authored
fix: improve URL handling in getRequestURL function
1 parent 7c658fe commit 7786a7c

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

src/utils.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,18 @@ function getRequestURL(req) {
312312
if (typeof req.getURL === "function") {
313313
return req.getURL();
314314
}
315-
// Fastify decodes URI by default, Our logic is based on encoded URI
316-
else if (typeof req.originalUrl !== "undefined") {
317-
return req.originalUrl;
315+
316+
// Fastify decodes URI by default, our logic is based on encoded URI.
317+
// req.originalUrl preserves the original encoded URL; req.url may be
318+
// modified by middleware (e.g. connect-history-api-fallback), in which
319+
// case we re-encode it and use it instead.
320+
if (typeof req.originalUrl !== "undefined") {
321+
const encodedUrl =
322+
typeof req.url !== "undefined" ? encodeURI(req.url) : undefined;
323+
324+
return encodedUrl !== req.originalUrl && encodedUrl !== undefined
325+
? encodedUrl
326+
: req.originalUrl;
318327
}
319328

320329
return req.url;

0 commit comments

Comments
 (0)