Skip to content

Commit 15116a0

Browse files
fix: normalise basepath (#228)
* fix: normalise redirect/rewrite target locales * chore: fix data urls * fix: normalise basepath --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent b664608 commit 15116a0

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

edge-runtime/lib/next-request.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Context } from '@netlify/edge-functions'
22

3-
import { normalizeDataUrl, removeBasePath, normalizeLocalePath } from './util.ts'
3+
import { normalizeDataUrl, removeBasePath, normalizeLocalePath, addBasePath } from './util.ts'
44

55
interface I18NConfig {
66
defaultLocale: string
@@ -41,6 +41,7 @@ const normalizeRequestURL = (
4141
const url = new URL(originalURL)
4242

4343
url.pathname = removeBasePath(url.pathname, nextConfig?.basePath)
44+
const didRemoveBasePath = url.toString() !== originalURL
4445

4546
let detectedLocale: string | undefined
4647

@@ -64,6 +65,10 @@ const normalizeRequestURL = (
6465
url.pathname = `${url.pathname}/`
6566
}
6667

68+
if (didRemoveBasePath) {
69+
url.pathname = addBasePath(url.pathname, nextConfig?.basePath)
70+
}
71+
6772
return {
6873
url: url.toString(),
6974
detectedLocale,

edge-runtime/lib/response.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import { HTMLRewriter } from '../vendor/deno.land/x/[email protected]/
33

44
import { updateModifiedHeaders } from './headers.ts'
55
import type { StructuredLogger } from './logging.ts'
6-
import { normalizeDataUrl, normalizeLocalePath, relativizeURL, rewriteDataPath } from './util.ts'
6+
import {
7+
addBasePath,
8+
normalizeDataUrl,
9+
normalizeLocalePath,
10+
relativizeURL,
11+
rewriteDataPath,
12+
} from './util.ts'
713
import { addMiddlewareHeaders, isMiddlewareRequest, isMiddlewareResponse } from './middleware.ts'
814
import { RequestData } from './next-request.ts'
915

@@ -237,9 +243,9 @@ function normalizeLocalizedTarget({
237243
!normalizedTarget.pathname.startsWith(`/api/`) &&
238244
!normalizedTarget.pathname.startsWith(`/_next/static/`)
239245
) {
240-
targetUrl.pathname = `/${locale}${normalizedTarget.pathname}`
241-
return targetUrl.toString()
246+
targetUrl.pathname = addBasePath(`/${locale}${normalizedTarget.pathname}`, nextConfig?.basePath)
247+
} else {
248+
targetUrl.pathname = addBasePath(normalizedTarget.pathname, nextConfig?.basePath)
242249
}
243-
targetUrl.pathname = normalizedTarget.pathname
244250
return targetUrl.toString()
245251
}

edge-runtime/lib/util.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ export const removeBasePath = (path: string, basePath?: string) => {
2424
return path
2525
}
2626

27+
export const addBasePath = (path: string, basePath?: string) => {
28+
if (basePath && !path.startsWith(basePath)) {
29+
return `${basePath}${path}`
30+
}
31+
return path
32+
}
33+
2734
// https://github.com/vercel/next.js/blob/canary/packages/next/src/shared/lib/i18n/normalize-locale-path.ts
2835

2936
export interface PathLocale {
@@ -91,8 +98,11 @@ export function rewriteDataPath({
9198
}) {
9299
const normalizedDataUrl = normalizeDataUrl(removeBasePath(dataUrl, basePath))
93100

94-
return dataUrl.replace(
95-
normalizeIndex(normalizedDataUrl),
96-
stripTrailingSlash(normalizeIndex(newRoute)),
101+
return addBasePath(
102+
dataUrl.replace(
103+
normalizeIndex(normalizedDataUrl),
104+
stripTrailingSlash(normalizeIndex(newRoute)),
105+
),
106+
basePath,
97107
)
98108
}

0 commit comments

Comments
 (0)