Skip to content

Commit dc01b2c

Browse files
committed
feat: respond to PR review
1 parent b7412ee commit dc01b2c

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type {
1010
} from "types/next-types";
1111
import type { InternalEvent, InternalResult } from "types/open-next";
1212
import { emptyReadableStream, toReadableStream } from "utils/stream";
13-
import { normalizePath } from "utils/normalize-path"
13+
import { normalizeRepeatedSlashes } from "utils/normalize-path"
1414

1515
import { debug } from "../../adapters/logger";
1616
import { handleLocaleRedirect, localizePath } from "./i18n";
@@ -267,15 +267,12 @@ function handleRepeatedSlashRedirect(
267267
event: InternalEvent,
268268
): false | InternalResult {
269269
// Redirect `https://example.com//foo` to `https://example.com/foo`.
270-
const url = new URL(event.url);
271-
if (url.pathname.match(/(\\|\/\/)/)) {
270+
if (event.rawPath.match(/(\\|\/\/)/)) {
272271
return {
273272
type: event.type,
274273
statusCode: 308,
275274
headers: {
276-
Location: `${url.protocol}//${url.host}${normalizePath(url.pathname)}${
277-
url.search
278-
}`,
275+
Location: normalizeRepeatedSlashes(new URL(event.url)),
279276
},
280277
body: emptyReadableStream(),
281278
isBase64Encoded: false,

packages/open-next/src/utils/normalize-path.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import path from "node:path";
22

33
export function normalizePath(path: string) {
4-
return path.replace(/\\/g, "/").replace(/\/\/+/g, "/");
4+
return path.replace(/\\/g, "/");
5+
}
6+
7+
export function normalizeRepeatedSlashes(url: URL) {
8+
const urlNoQuery = url.host + url.pathname;
9+
return `${url.protocol}//${urlNoQuery
10+
.replace(/\\/g, "/")
11+
.replace(/\/\/+/g, "/")}${url.search}`;
512
}
613

714
export function getMonorepoRelativePath(relativePath = "../.."): string {

0 commit comments

Comments
 (0)