Skip to content

Commit da8215f

Browse files
committed
chore: refactor to remove normalizeLocalizedTarget
1 parent a7620fd commit da8215f

File tree

2 files changed

+4
-55
lines changed

2 files changed

+4
-55
lines changed

edge-runtime/lib/response.ts

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,7 @@ import { updateModifiedHeaders } from './headers.ts'
55
import type { StructuredLogger } from './logging.ts'
66
import { addMiddlewareHeaders, isMiddlewareRequest, isMiddlewareResponse } from './middleware.ts'
77
import { NetlifyNextRequest } from './next-request.ts'
8-
import {
9-
addBasePath,
10-
normalizeDataUrl,
11-
normalizeLocalePath,
12-
normalizeTrailingSlash,
13-
relativizeURL,
14-
} from './util.ts'
8+
import { normalizeDataUrl, normalizeTrailingSlash, relativizeURL } from './util.ts'
159

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

190-
const target = normalizeLocalizedTarget({ target: rewriteUrl.toString(), request, nextConfig })
191-
if (target === request.url) {
192-
logger.withFields({ rewrite_url: rewrite }).debug('Rewrite url is same as original url')
193-
return
194-
}
184+
const target = rewriteUrl.toString()
195185
res.headers.set('x-middleware-rewrite', relativeUrl)
196186
request.headers.set('x-middleware-rewrite', target)
197187
return addMiddlewareHeaders(context.rewrite(target), res)
198188
}
199189

200-
// If we are redirecting a request that had a locale in the URL, we need to add it back in
201-
if (redirect && locale) {
202-
redirect = normalizeLocalizedTarget({ target: redirect, request, nextConfig })
203-
if (redirect === request.url) {
204-
logger.withFields({ rewrite_url: rewrite }).debug('Rewrite url is same as original url')
205-
return
206-
}
207-
res.headers.set('location', redirect)
208-
}
209-
210190
// Data requests shouldn't automatically redirect in the browser (they might be HTML pages): they're handled by the router
211191
if (redirect && isDataReq) {
212192
res.headers.delete('location')
@@ -226,35 +206,3 @@ export const buildResponse = async ({
226206

227207
return res
228208
}
229-
230-
/**
231-
* Normalizes the locale in a URL.
232-
*/
233-
function normalizeLocalizedTarget({
234-
target,
235-
request,
236-
nextConfig,
237-
locale,
238-
}: {
239-
target: string
240-
request: Request
241-
nextConfig?: NetlifyNextRequest['nextConfig']
242-
locale?: string
243-
}) {
244-
const targetUrl = new URL(target, request.url)
245-
const normalizedTarget = normalizeLocalePath(targetUrl.pathname, nextConfig?.i18n?.locales)
246-
const targetPathname = normalizedTarget.pathname
247-
const targetLocale = normalizedTarget.detectedLocale ?? locale
248-
249-
if (
250-
targetLocale &&
251-
!targetPathname.startsWith(`/api/`) &&
252-
!targetPathname.startsWith(`/_next/static/`)
253-
) {
254-
targetUrl.pathname =
255-
addBasePath(`/${targetLocale}${targetPathname}`, nextConfig?.basePath) || `/`
256-
} else {
257-
targetUrl.pathname = addBasePath(targetPathname, nextConfig?.basePath) || `/`
258-
}
259-
return targetUrl.toString()
260-
}

edge-runtime/lib/util.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ export const addLocale = (path: string, locale?: string) => {
3535
locale &&
3636
path.toLowerCase() !== `/${locale.toLowerCase()}` &&
3737
!path.toLowerCase().startsWith(`/${locale.toLowerCase()}/`) &&
38-
!path.toLowerCase().startsWith(`/${locale.toLowerCase()}-`)
38+
!path.startsWith(`/api/`) &&
39+
!path.startsWith(`/_next/static/`)
3940
) {
4041
return `/${locale}${path}`
4142
}

0 commit comments

Comments
 (0)