Skip to content

Commit dccecf9

Browse files
committed
chore: refactor for clarity
1 parent 878f022 commit dccecf9

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

edge-runtime/lib/response.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export interface FetchEventResult {
2020

2121
interface BuildResponseOptions {
2222
context: Context
23-
config?: NetlifyNextRequest['nextConfig']
23+
nextConfig?: NetlifyNextRequest['nextConfig']
2424
locale?: string
2525
request: Request
2626
result: FetchEventResult
@@ -29,7 +29,7 @@ interface BuildResponseOptions {
2929

3030
export const buildResponse = async ({
3131
context,
32-
config,
32+
nextConfig,
3333
locale,
3434
request,
3535
result,
@@ -185,9 +185,9 @@ export const buildResponse = async ({
185185
}
186186

187187
// respect trailing slash rules to prevent 308s
188-
rewriteUrl.pathname = normalizeTrailingSlash(rewriteUrl.pathname, config?.trailingSlash)
188+
rewriteUrl.pathname = normalizeTrailingSlash(rewriteUrl.pathname, nextConfig?.trailingSlash)
189189

190-
const target = normalizeLocalizedTarget({ target: rewriteUrl.toString(), request, config })
190+
const target = normalizeLocalizedTarget({ target: rewriteUrl.toString(), request, nextConfig })
191191
if (target === request.url) {
192192
logger.withFields({ rewrite_url: rewrite }).debug('Rewrite url is same as original url')
193193
return
@@ -199,7 +199,7 @@ export const buildResponse = async ({
199199

200200
// If we are redirecting a request that had a locale in the URL, we need to add it back in
201201
if (redirect && locale) {
202-
redirect = normalizeLocalizedTarget({ target: redirect, request, config })
202+
redirect = normalizeLocalizedTarget({ target: redirect, request, nextConfig })
203203
if (redirect === request.url) {
204204
logger.withFields({ rewrite_url: rewrite }).debug('Rewrite url is same as original url')
205205
return
@@ -233,16 +233,16 @@ export const buildResponse = async ({
233233
function normalizeLocalizedTarget({
234234
target,
235235
request,
236-
config,
236+
nextConfig,
237237
locale,
238238
}: {
239239
target: string
240240
request: Request
241-
config?: NetlifyNextRequest['nextConfig']
241+
nextConfig?: NetlifyNextRequest['nextConfig']
242242
locale?: string
243243
}) {
244244
const targetUrl = new URL(target, request.url)
245-
const normalizedTarget = normalizeLocalePath(targetUrl.pathname, config?.i18n?.locales)
245+
const normalizedTarget = normalizeLocalePath(targetUrl.pathname, nextConfig?.i18n?.locales)
246246
const targetPathname = normalizedTarget.pathname
247247
const targetLocale = normalizedTarget.detectedLocale ?? locale
248248

@@ -251,9 +251,10 @@ function normalizeLocalizedTarget({
251251
!targetPathname.startsWith(`/api/`) &&
252252
!targetPathname.startsWith(`/_next/static/`)
253253
) {
254-
targetUrl.pathname = addBasePath(`/${targetLocale}${targetPathname}`, config?.basePath) || `/`
254+
targetUrl.pathname =
255+
addBasePath(`/${targetLocale}${targetPathname}`, nextConfig?.basePath) || `/`
255256
} else {
256-
targetUrl.pathname = addBasePath(targetPathname, config?.basePath) || `/`
257+
targetUrl.pathname = addBasePath(targetPathname, nextConfig?.basePath) || `/`
257258
}
258259
return targetUrl.toString()
259260
}

edge-runtime/middleware.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,19 @@ export async function handleMiddleware(
3232
nextHandler: NextHandler,
3333
) {
3434
const url = new URL(request.url)
35+
const query = searchParamsToUrlQuery(url.searchParams)
36+
const { localizedUrl, locale } = localizeRequest(url, nextConfig)
3537
const reqLogger = logger
3638
.withLogLevel(
3739
request.headers.has(InternalHeaders.NFDebugLogging) ? LogLevel.Debug : LogLevel.Log,
3840
)
3941
.withFields({ url_path: url.pathname })
4042
.withRequestID(request.headers.get(InternalHeaders.NFRequestID))
4143

44+
// Convert the incoming request to a Next.js request, which includes
45+
// normalizing the URL, adding geo and IP information and converting
46+
// the headers to a plain object, among other things.
4247
const nextRequest = buildNextRequest(request, context, nextConfig)
43-
const { localizedUrl, locale } = localizeRequest(url, nextConfig)
44-
const query = searchParamsToUrlQuery(url.searchParams)
4548

4649
// While we have already checked the path when mapping to the edge function,
4750
// Next.js supports extra rules that we need to check here too, because we
@@ -56,7 +59,7 @@ export async function handleMiddleware(
5659
const result = await nextHandler({ request: nextRequest })
5760
const response = await buildResponse({
5861
context,
59-
config: nextRequest.nextConfig,
62+
nextConfig: nextRequest.nextConfig,
6063
locale,
6164
request,
6265
result,

0 commit comments

Comments
 (0)