Skip to content

Commit cb061ca

Browse files
committed
chore: fix duplicate rules
1 parent 2711061 commit cb061ca

File tree

3 files changed

+22
-103
lines changed

3 files changed

+22
-103
lines changed

src/helpers/redirects.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,20 @@ export const generateRedirects = async ({
122122
// This generates a rewrite for every middleware in every locale, both with and without a splat
123123
netlifyConfig.redirects.push(
124124
...middleware
125-
.map((route) => [
126-
handlerRewrite(`${route}`),
127-
handlerRewrite(`${route}/*`),
128-
handlerRewrite(routeToDataRoute(`${route}/*`, buildId)),
129-
...(i18n?.locales?.map((locale) => [
130-
handlerRewrite(`/${locale}${route}`),
131-
handlerRewrite(`/${locale}${route}/*`),
132-
handlerRewrite(routeToDataRoute(`${route}/*`, buildId, locale)),
133-
]) ?? []),
134-
])
125+
.map((route) => {
126+
const unlocalized = [handlerRewrite(`${route}`), handlerRewrite(`${route}/*`)]
127+
if (i18n?.locales?.length > 0) {
128+
const localized = i18n?.locales?.map((locale) => [
129+
handlerRewrite(`/${locale}${route}`),
130+
handlerRewrite(`/${locale}${route}/*`),
131+
handlerRewrite(`/_next/data/${buildId}/${locale}${route}/*`),
132+
])
133+
// With i18n, all data routes are prefixed with the locale, but the HTML also has the unprefixed default
134+
return [...unlocalized, ...localized]
135+
}
136+
return [...unlocalized, handlerRewrite(`/_next/data/${buildId}${route}/*`)]
137+
})
138+
// Flatten the array of arrays. Can't use flatMap as it might be 2 levels deep
135139
.flat(2),
136140
)
137141

@@ -166,10 +170,10 @@ export const generateRedirects = async ({
166170
force: true,
167171
}),
168172
)
173+
} else if (matchesMiddleware(middleware, route)) {
174+
// Routes that match middleware can't use the ODB
175+
routesThatMatchMiddleware.add(route)
169176
} else {
170-
if (matchesMiddleware(middleware, route)) {
171-
routesThatMatchMiddleware.add(route)
172-
}
173177
// ISR routes use the ODB handler
174178
netlifyConfig.redirects.push(
175179
// No i18n, because the route is already localized
@@ -195,10 +199,11 @@ export const generateRedirects = async ({
195199
if (route.page in prerenderedDynamicRoutes) {
196200
if (matchesMiddleware(middleware, route.page)) {
197201
routesThatMatchMiddleware.add(route.page)
202+
} else {
203+
netlifyConfig.redirects.push(
204+
...redirectsForNextRoute({ buildId, route: route.page, basePath, to: ODB_FUNCTION_PATH, status: 200, i18n }),
205+
)
198206
}
199-
netlifyConfig.redirects.push(
200-
...redirectsForNextRoute({ buildId, route: route.page, basePath, to: ODB_FUNCTION_PATH, status: 200, i18n }),
201-
)
202207
} else {
203208
// If the route isn't prerendered, it's SSR
204209
netlifyConfig.redirects.push(

src/helpers/utils.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,8 @@ export const netlifyRoutesForNextRouteWithData = ({ route, dataRoute }: { route:
4545
...toNetlifyRoute(route),
4646
]
4747

48-
export const endsWithDynamicSegment = (route: string) => /\/(\*|(:[a-z]+))$/.test(route)
49-
5048
export const routeToDataRoute = (route: string, buildId: string, locale?: string) =>
51-
`/_next/data/${buildId}${locale ? `/${locale}` : ''}${route === '/' ? '/index' : route}${
52-
endsWithDynamicSegment(route) ? '' : '.json'
53-
}`
49+
`/_next/data/${buildId}${locale ? `/${locale}` : ''}${route === '/' ? '/index' : route}.json`
5450

5551
const netlifyRoutesForNextRoute = (route: string, buildId: string, i18n?: I18n): Array<string> => {
5652
if (!i18n?.locales?.length) {

test/__snapshots__/index.js.snap

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -463,11 +463,6 @@ Array [
463463
"status": 200,
464464
"to": "/.netlify/functions/___netlify-handler",
465465
},
466-
Object {
467-
"from": "/_next/data/build-id/getStaticProps/withFallbackAndMiddleware/*",
468-
"status": 200,
469-
"to": "/.netlify/functions/___netlify-handler",
470-
},
471466
Object {
472467
"from": "/en/getStaticProps/withFallbackAndMiddleware",
473468
"status": 200,
@@ -523,11 +518,6 @@ Array [
523518
"status": 200,
524519
"to": "/.netlify/functions/___netlify-handler",
525520
},
526-
Object {
527-
"from": "/_next/data/build-id/middle/*",
528-
"status": 200,
529-
"to": "/.netlify/functions/___netlify-handler",
530-
},
531521
Object {
532522
"from": "/en/middle",
533523
"status": 200,
@@ -1305,78 +1295,6 @@ Array [
13051295
"status": 200,
13061296
"to": "/.netlify/builders/___netlify-odb-handler",
13071297
},
1308-
Object {
1309-
"force": false,
1310-
"from": "/_next/data/build-id/en/getStaticProps/withFallbackAndMiddleware/:id.json",
1311-
"status": 200,
1312-
"to": "/.netlify/builders/___netlify-odb-handler",
1313-
},
1314-
Object {
1315-
"force": false,
1316-
"from": "/getStaticProps/withFallbackAndMiddleware/:id",
1317-
"status": 200,
1318-
"to": "/.netlify/builders/___netlify-odb-handler",
1319-
},
1320-
Object {
1321-
"force": false,
1322-
"from": "/_next/data/build-id/es/getStaticProps/withFallbackAndMiddleware/:id.json",
1323-
"status": 200,
1324-
"to": "/.netlify/builders/___netlify-odb-handler",
1325-
},
1326-
Object {
1327-
"force": false,
1328-
"from": "/es/getStaticProps/withFallbackAndMiddleware/:id",
1329-
"status": 200,
1330-
"to": "/.netlify/builders/___netlify-odb-handler",
1331-
},
1332-
Object {
1333-
"force": false,
1334-
"from": "/_next/data/build-id/fr/getStaticProps/withFallbackAndMiddleware/:id.json",
1335-
"status": 200,
1336-
"to": "/.netlify/builders/___netlify-odb-handler",
1337-
},
1338-
Object {
1339-
"force": false,
1340-
"from": "/fr/getStaticProps/withFallbackAndMiddleware/:id",
1341-
"status": 200,
1342-
"to": "/.netlify/builders/___netlify-odb-handler",
1343-
},
1344-
Object {
1345-
"force": false,
1346-
"from": "/_next/data/build-id/en/getStaticProps/withFallbackAndMiddleware/:slug/*",
1347-
"status": 200,
1348-
"to": "/.netlify/builders/___netlify-odb-handler",
1349-
},
1350-
Object {
1351-
"force": false,
1352-
"from": "/getStaticProps/withFallbackAndMiddleware/:slug/*",
1353-
"status": 200,
1354-
"to": "/.netlify/builders/___netlify-odb-handler",
1355-
},
1356-
Object {
1357-
"force": false,
1358-
"from": "/_next/data/build-id/es/getStaticProps/withFallbackAndMiddleware/:slug/*",
1359-
"status": 200,
1360-
"to": "/.netlify/builders/___netlify-odb-handler",
1361-
},
1362-
Object {
1363-
"force": false,
1364-
"from": "/es/getStaticProps/withFallbackAndMiddleware/:slug/*",
1365-
"status": 200,
1366-
"to": "/.netlify/builders/___netlify-odb-handler",
1367-
},
1368-
Object {
1369-
"force": false,
1370-
"from": "/_next/data/build-id/fr/getStaticProps/withFallbackAndMiddleware/:slug/*",
1371-
"status": 200,
1372-
"to": "/.netlify/builders/___netlify-odb-handler",
1373-
},
1374-
Object {
1375-
"force": false,
1376-
"from": "/fr/getStaticProps/withFallbackAndMiddleware/:slug/*",
1377-
"status": 200,
1378-
"to": "/.netlify/builders/___netlify-odb-handler",
1379-
},
13801298
Object {
13811299
"force": false,
13821300
"from": "/_next/data/build-id/en/getStaticProps/withFallbackBlocking/:id.json",

0 commit comments

Comments
 (0)