Skip to content

Commit 01e9450

Browse files
authored
fix: honor skipMiddlewareUrlNormalize (#287)
* fix: honor skipMiddlewareUrlNormalize * fix: preserve no trailing slash for localized index routes * chore: whitespaces
1 parent 694a2be commit 01e9450

File tree

4 files changed

+19
-12
lines changed

4 files changed

+19
-12
lines changed

edge-runtime/lib/next-request.ts

+14-9
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export interface RequestData {
2424
basePath?: string
2525
i18n?: I18NConfig | null
2626
trailingSlash?: boolean
27+
skipMiddlewareUrlNormalize?: boolean
2728
}
2829
page?: {
2930
name?: string
@@ -50,19 +51,23 @@ const normalizeRequestURL = (
5051
url.pathname,
5152
nextConfig?.i18n?.locales,
5253
)
53-
url.pathname = pathname
54+
if (!nextConfig?.skipMiddlewareUrlNormalize) {
55+
url.pathname = pathname || '/'
56+
}
5457
detectedLocale = detected
5558
}
5659

57-
// We want to run middleware for data requests and expose the URL of the
58-
// corresponding pages, so we have to normalize the URLs before running
59-
// the handler.
60-
url.pathname = normalizeDataUrl(url.pathname)
60+
if (!nextConfig?.skipMiddlewareUrlNormalize) {
61+
// We want to run middleware for data requests and expose the URL of the
62+
// corresponding pages, so we have to normalize the URLs before running
63+
// the handler.
64+
url.pathname = normalizeDataUrl(url.pathname)
6165

62-
// Normalizing the trailing slash based on the `trailingSlash` configuration
63-
// property from the Next.js config.
64-
if (nextConfig?.trailingSlash && url.pathname !== '/' && !url.pathname.endsWith('/')) {
65-
url.pathname = `${url.pathname}/`
66+
// Normalizing the trailing slash based on the `trailingSlash` configuration
67+
// property from the Next.js config.
68+
if (nextConfig?.trailingSlash && url.pathname !== '/' && !url.pathname.endsWith('/')) {
69+
url.pathname = `${url.pathname}/`
70+
}
6671
}
6772

6873
if (didRemoveBasePath) {

edge-runtime/lib/response.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,10 @@ function normalizeLocalizedTarget({
247247
!normalizedTarget.pathname.startsWith(`/api/`) &&
248248
!normalizedTarget.pathname.startsWith(`/_next/static/`)
249249
) {
250-
targetUrl.pathname = addBasePath(`/${locale}${normalizedTarget.pathname}`, nextConfig?.basePath)
250+
targetUrl.pathname =
251+
addBasePath(`/${locale}${normalizedTarget.pathname}`, nextConfig?.basePath) || `/`
251252
} else {
252-
targetUrl.pathname = addBasePath(normalizedTarget.pathname, nextConfig?.basePath)
253+
targetUrl.pathname = addBasePath(normalizedTarget.pathname, nextConfig?.basePath) || `/`
253254
}
254255
return targetUrl.toString()
255256
}

edge-runtime/lib/util.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export function normalizeLocalePath(pathname: string, locales?: string[]): PathL
5656
if (pathnameParts[1] && pathnameParts[1].toLowerCase() === locale.toLowerCase()) {
5757
detectedLocale = locale
5858
pathnameParts.splice(1, 1)
59-
pathname = pathnameParts.join('/') || '/'
59+
pathname = pathnameParts.join('/')
6060
return true
6161
}
6262
return false

src/build/functions/edge.ts

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ const writeHandlerFile = async (ctx: PluginContext, { matchers, name }: NextDefi
8989
basePath: nextConfig.basePath,
9090
i18n: nextConfig.i18n,
9191
trailingSlash: nextConfig.trailingSlash,
92+
skipMiddlewareUrlNormalize: nextConfig.skipMiddlewareUrlNormalize,
9293
}
9394

9495
await writeFile(

0 commit comments

Comments
 (0)