Skip to content

Commit

Permalink
fix: normalise basepath (#228)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
ascorbic and kodiakhq[bot] authored Feb 2, 2024
1 parent b664608 commit 15116a0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
7 changes: 6 additions & 1 deletion edge-runtime/lib/next-request.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Context } from '@netlify/edge-functions'

import { normalizeDataUrl, removeBasePath, normalizeLocalePath } from './util.ts'
import { normalizeDataUrl, removeBasePath, normalizeLocalePath, addBasePath } from './util.ts'

interface I18NConfig {
defaultLocale: string
Expand Down Expand Up @@ -41,6 +41,7 @@ const normalizeRequestURL = (
const url = new URL(originalURL)

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

let detectedLocale: string | undefined

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

if (didRemoveBasePath) {
url.pathname = addBasePath(url.pathname, nextConfig?.basePath)
}

return {
url: url.toString(),
detectedLocale,
Expand Down
14 changes: 10 additions & 4 deletions edge-runtime/lib/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import { HTMLRewriter } from '../vendor/deno.land/x/[email protected]/

import { updateModifiedHeaders } from './headers.ts'
import type { StructuredLogger } from './logging.ts'
import { normalizeDataUrl, normalizeLocalePath, relativizeURL, rewriteDataPath } from './util.ts'
import {
addBasePath,
normalizeDataUrl,
normalizeLocalePath,
relativizeURL,
rewriteDataPath,
} from './util.ts'
import { addMiddlewareHeaders, isMiddlewareRequest, isMiddlewareResponse } from './middleware.ts'
import { RequestData } from './next-request.ts'

Expand Down Expand Up @@ -237,9 +243,9 @@ function normalizeLocalizedTarget({
!normalizedTarget.pathname.startsWith(`/api/`) &&
!normalizedTarget.pathname.startsWith(`/_next/static/`)
) {
targetUrl.pathname = `/${locale}${normalizedTarget.pathname}`
return targetUrl.toString()
targetUrl.pathname = addBasePath(`/${locale}${normalizedTarget.pathname}`, nextConfig?.basePath)
} else {
targetUrl.pathname = addBasePath(normalizedTarget.pathname, nextConfig?.basePath)
}
targetUrl.pathname = normalizedTarget.pathname
return targetUrl.toString()
}
16 changes: 13 additions & 3 deletions edge-runtime/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ export const removeBasePath = (path: string, basePath?: string) => {
return path
}

export const addBasePath = (path: string, basePath?: string) => {
if (basePath && !path.startsWith(basePath)) {
return `${basePath}${path}`
}
return path
}

// https://github.com/vercel/next.js/blob/canary/packages/next/src/shared/lib/i18n/normalize-locale-path.ts

export interface PathLocale {
Expand Down Expand Up @@ -91,8 +98,11 @@ export function rewriteDataPath({
}) {
const normalizedDataUrl = normalizeDataUrl(removeBasePath(dataUrl, basePath))

return dataUrl.replace(
normalizeIndex(normalizedDataUrl),
stripTrailingSlash(normalizeIndex(newRoute)),
return addBasePath(
dataUrl.replace(
normalizeIndex(normalizedDataUrl),
stripTrailingSlash(normalizeIndex(newRoute)),
),
basePath,
)
}

0 comments on commit 15116a0

Please sign in to comment.