Skip to content

Commit

Permalink
chore: refactor to remove normalizeLocalizedTarget
Browse files Browse the repository at this point in the history
  • Loading branch information
orinokai committed Jul 24, 2024
1 parent a7620fd commit da8215f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 55 deletions.
56 changes: 2 additions & 54 deletions edge-runtime/lib/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@ import { updateModifiedHeaders } from './headers.ts'
import type { StructuredLogger } from './logging.ts'
import { addMiddlewareHeaders, isMiddlewareRequest, isMiddlewareResponse } from './middleware.ts'
import { NetlifyNextRequest } from './next-request.ts'
import {
addBasePath,
normalizeDataUrl,
normalizeLocalePath,
normalizeTrailingSlash,
relativizeURL,
} from './util.ts'
import { normalizeDataUrl, normalizeTrailingSlash, relativizeURL } from './util.ts'

export interface FetchEventResult {
response: Response
Expand Down Expand Up @@ -187,26 +181,12 @@ export const buildResponse = async ({
// respect trailing slash rules to prevent 308s
rewriteUrl.pathname = normalizeTrailingSlash(rewriteUrl.pathname, nextConfig?.trailingSlash)

const target = normalizeLocalizedTarget({ target: rewriteUrl.toString(), request, nextConfig })
if (target === request.url) {
logger.withFields({ rewrite_url: rewrite }).debug('Rewrite url is same as original url')
return
}
const target = rewriteUrl.toString()
res.headers.set('x-middleware-rewrite', relativeUrl)
request.headers.set('x-middleware-rewrite', target)
return addMiddlewareHeaders(context.rewrite(target), res)
}

// If we are redirecting a request that had a locale in the URL, we need to add it back in
if (redirect && locale) {
redirect = normalizeLocalizedTarget({ target: redirect, request, nextConfig })
if (redirect === request.url) {
logger.withFields({ rewrite_url: rewrite }).debug('Rewrite url is same as original url')
return
}
res.headers.set('location', redirect)
}

// Data requests shouldn't automatically redirect in the browser (they might be HTML pages): they're handled by the router
if (redirect && isDataReq) {
res.headers.delete('location')
Expand All @@ -226,35 +206,3 @@ export const buildResponse = async ({

return res
}

/**
* Normalizes the locale in a URL.
*/
function normalizeLocalizedTarget({
target,
request,
nextConfig,
locale,
}: {
target: string
request: Request
nextConfig?: NetlifyNextRequest['nextConfig']
locale?: string
}) {
const targetUrl = new URL(target, request.url)
const normalizedTarget = normalizeLocalePath(targetUrl.pathname, nextConfig?.i18n?.locales)
const targetPathname = normalizedTarget.pathname
const targetLocale = normalizedTarget.detectedLocale ?? locale

if (
targetLocale &&
!targetPathname.startsWith(`/api/`) &&
!targetPathname.startsWith(`/_next/static/`)
) {
targetUrl.pathname =
addBasePath(`/${targetLocale}${targetPathname}`, nextConfig?.basePath) || `/`
} else {
targetUrl.pathname = addBasePath(targetPathname, nextConfig?.basePath) || `/`
}
return targetUrl.toString()
}
3 changes: 2 additions & 1 deletion edge-runtime/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export const addLocale = (path: string, locale?: string) => {
locale &&
path.toLowerCase() !== `/${locale.toLowerCase()}` &&
!path.toLowerCase().startsWith(`/${locale.toLowerCase()}/`) &&
!path.toLowerCase().startsWith(`/${locale.toLowerCase()}-`)
!path.startsWith(`/api/`) &&
!path.startsWith(`/_next/static/`)
) {
return `/${locale}${path}`
}
Expand Down

0 comments on commit da8215f

Please sign in to comment.