Skip to content

Commit 7b7f774

Browse files
committed
fix: parsedUrl check for i18n
1 parent bfbb32f commit 7b7f774

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

packages/runtime/src/templates/handlerUtils.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,29 @@ export const localizeDataRoute = (dataRoute: string, localizedRoute: string): st
271271
.replace(new RegExp(`/_next/data/(.+?)/(${locale}/)?`), `/_next/data/$1/${locale}/`)
272272
.replace(/\/index\.json$/, '.json')
273273
}
274+
275+
interface Routes {
276+
page?: string
277+
regex?: string
278+
routeKeys?: object
279+
namedRegex?: string
280+
dataRouteRegex?: string
281+
}
282+
283+
export const getMatchedRoute = (
284+
paths: string,
285+
routes: Array<Routes>,
286+
parsedUrl?: string,
287+
basePath?: string,
288+
): Routes => {
289+
const matchedRoute = routes?.find((route) => {
290+
// Some internationalized routes are automatically removing the locale prefix making the path not match the route
291+
// we can use the parsedURL, which has the locale included '/en' and will match the route
292+
if (!basePath && parsedUrl) {
293+
return new RegExp(route.regex).test(new URL(parsedUrl, 'http://n').pathname)
294+
}
295+
// Default to the original path
296+
return new RegExp(route.regex).test(new URL(paths, 'http://n').pathname)
297+
})
298+
return matchedRoute
299+
}

packages/runtime/src/templates/server.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
localizeRoute,
1313
localizeDataRoute,
1414
unlocalizeRoute,
15+
getMatchedRoute,
1516
} from './handlerUtils'
1617

1718
interface NetlifyConfig {
@@ -50,7 +51,7 @@ const getNetlifyNextServer = (NextServer: NextServerType) => {
5051
const { url, headers } = req
5152

5253
// conditionally use the prebundled React module
53-
this.netlifyPrebundleReact(url)
54+
this.netlifyPrebundleReact(url, parsedUrl)
5455

5556
// intercept on-demand revalidation requests and handle with the Netlify API
5657
if (headers['x-prerender-revalidate'] && this.netlifyConfig.revalidateToken) {
@@ -79,12 +80,12 @@ const getNetlifyNextServer = (NextServer: NextServerType) => {
7980
}
8081

8182
// doing what they do in https://github.com/vercel/vercel/blob/1663db7ca34d3dd99b57994f801fb30b72fbd2f3/packages/next/src/server-build.ts#L576-L580
82-
private netlifyPrebundleReact(path: string) {
83+
private netlifyPrebundleReact(path: string, parsedUrl?) {
8384
const routesManifest = this.getRoutesManifest?.()
8485
const appPathsRoutes = this.getAppPathRoutes?.()
8586

8687
const routes = routesManifest && [...routesManifest.staticRoutes, ...routesManifest.dynamicRoutes]
87-
const matchedRoute = routes?.find((route) => new RegExp(route.regex).test(new URL(path, 'http://n').pathname))
88+
const matchedRoute = getMatchedRoute(path, routes, parsedUrl, this.nextConfig.basePath)
8889
const isAppRoute = appPathsRoutes && matchedRoute ? appPathsRoutes[matchedRoute.page] : false
8990

9091
if (isAppRoute) {

0 commit comments

Comments
 (0)