Skip to content

Commit 4358a1c

Browse files
committed
Merge remote-tracking branch 'origin/main' into michalpiechowiak/frp-765-migrate-next-runtime-to-use-frameworks-api
2 parents 6c7874c + b352918 commit 4358a1c

37 files changed

+429
-359
lines changed

Diff for: .release-please-manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
".": "5.5.1"
2+
".": "5.6.0"
33
}

Diff for: CHANGELOG.md

+13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
# Changelog
22

3+
## [5.6.0](https://github.com/netlify/next-runtime/compare/v5.5.1...v5.6.0) (2024-07-29)
4+
5+
6+
### Features
7+
8+
* fail build when netlify forms detected without workaround ([#2539](https://github.com/netlify/next-runtime/issues/2539)) ([56fef5f](https://github.com/netlify/next-runtime/commit/56fef5fac626bf0e7fc44014b085f759be00dd40))
9+
10+
11+
### Bug Fixes
12+
13+
* apply type: module only to runtime modules ([#2549](https://github.com/netlify/next-runtime/issues/2549)) ([325968d](https://github.com/netlify/next-runtime/commit/325968d9fceb925d4972c242aea446f5a1cb2f8d))
14+
* edge-middleware i18n matching ([#2555](https://github.com/netlify/next-runtime/issues/2555)) ([f02ef88](https://github.com/netlify/next-runtime/commit/f02ef880f27bdc28f9e699757e8df9d2a1203438))
15+
316
## [5.5.1](https://github.com/netlify/next-runtime/compare/v5.5.0...v5.5.1) (2024-07-08)
417

518

Diff for: e2e-report/package-lock.json

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: e2e-report/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"lint": "next lint"
1010
},
1111
"dependencies": {
12-
"@netlify/plugin-nextjs": "^5.5.1",
12+
"@netlify/plugin-nextjs": "^5.6.0",
1313
"next": "^14.2.3",
1414
"react": "^18.3.1",
1515
"react-dom": "^18.3.1"

Diff for: edge-runtime/lib/next-request.ts

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

33
import {
44
addBasePath,
5+
addLocale,
56
addTrailingSlash,
67
normalizeDataUrl,
78
normalizeLocalePath,
@@ -73,6 +74,33 @@ const normalizeRequestURL = (
7374
}
7475
}
7576

77+
export const localizeRequest = (
78+
url: URL,
79+
nextConfig?: {
80+
basePath?: string
81+
i18n?: I18NConfig | null
82+
},
83+
): { localizedUrl: URL; locale?: string } => {
84+
const localizedUrl = new URL(url)
85+
localizedUrl.pathname = removeBasePath(localizedUrl.pathname, nextConfig?.basePath)
86+
87+
// Detect the locale from the URL
88+
const { detectedLocale } = normalizeLocalePath(localizedUrl.pathname, nextConfig?.i18n?.locales)
89+
90+
// Add the locale to the URL if not already present
91+
localizedUrl.pathname = addLocale(
92+
localizedUrl.pathname,
93+
detectedLocale ?? nextConfig?.i18n?.defaultLocale,
94+
)
95+
96+
localizedUrl.pathname = addBasePath(localizedUrl.pathname, nextConfig?.basePath)
97+
98+
return {
99+
localizedUrl,
100+
locale: detectedLocale,
101+
}
102+
}
103+
76104
export const buildNextRequest = (
77105
request: Request,
78106
context: Context,

Diff for: edge-runtime/lib/util.ts

+14
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,20 @@ export const addBasePath = (path: string, basePath?: string) => {
2929
return path
3030
}
3131

32+
// add locale prefix if not present, allowing for locale fallbacks
33+
export const addLocale = (path: string, locale?: string) => {
34+
if (
35+
locale &&
36+
path.toLowerCase() !== `/${locale.toLowerCase()}` &&
37+
!path.toLowerCase().startsWith(`/${locale.toLowerCase()}/`) &&
38+
!path.startsWith(`/api/`) &&
39+
!path.startsWith(`/_next/`)
40+
) {
41+
return `/${locale}${path}`
42+
}
43+
return path
44+
}
45+
3246
// https://github.com/vercel/next.js/blob/canary/packages/next/src/shared/lib/i18n/normalize-locale-path.ts
3347

3448
export interface PathLocale {

Diff for: edge-runtime/middleware.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import nextConfig from './next.config.json' with { type: 'json' }
55

66
import { InternalHeaders } from './lib/headers.ts'
77
import { logger, LogLevel } from './lib/logging.ts'
8-
import { buildNextRequest, RequestData } from './lib/next-request.ts'
8+
import { buildNextRequest, localizeRequest, RequestData } from './lib/next-request.ts'
99
import { buildResponse, FetchEventResult } from './lib/response.ts'
1010
import {
1111
getMiddlewareRouteMatcher,
@@ -31,25 +31,29 @@ export async function handleMiddleware(
3131
context: Context,
3232
nextHandler: NextHandler,
3333
) {
34-
const nextRequest = buildNextRequest(request, context, nextConfig)
3534
const url = new URL(request.url)
35+
3636
const reqLogger = logger
3737
.withLogLevel(
3838
request.headers.has(InternalHeaders.NFDebugLogging) ? LogLevel.Debug : LogLevel.Log,
3939
)
4040
.withFields({ url_path: url.pathname })
4141
.withRequestID(request.headers.get(InternalHeaders.NFRequestID))
4242

43+
const { localizedUrl } = localizeRequest(url, nextConfig)
4344
// While we have already checked the path when mapping to the edge function,
4445
// Next.js supports extra rules that we need to check here too, because we
4546
// might be running an edge function for a path we should not. If we find
4647
// that's the case, short-circuit the execution.
47-
if (!matchesMiddleware(url.pathname, request, searchParamsToUrlQuery(url.searchParams))) {
48+
if (
49+
!matchesMiddleware(localizedUrl.pathname, request, searchParamsToUrlQuery(url.searchParams))
50+
) {
4851
reqLogger.debug('Aborting middleware due to runtime rules')
4952

5053
return
5154
}
5255

56+
const nextRequest = buildNextRequest(request, context, nextConfig)
5357
try {
5458
const result = await nextHandler({ request: nextRequest })
5559
const response = await buildResponse({

0 commit comments

Comments
 (0)