Skip to content

Commit 773e4ad

Browse files
fix: adds routeParams to the req on views (#12711)
1 parent c6659db commit 773e4ad

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

packages/next/src/utilities/initPage/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const initPage = async ({
1616
config: configPromise,
1717
importMap,
1818
route,
19+
routeParams = {},
1920
searchParams,
2021
useLayoutReq,
2122
}: Args): Promise<InitPageResult> => {
@@ -38,6 +39,7 @@ export const initPage = async ({
3839
depth: 10,
3940
ignoreQueryPrefix: true,
4041
}),
42+
routeParams,
4143
},
4244
urlSuffix: `${route}${searchParams ? queryString : ''}`,
4345
},

packages/next/src/utilities/initPage/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ export type Args = {
1616
* The current route, i.e. `/admin/collections/posts`.
1717
*/
1818
route: string
19+
/**
20+
* The route parameters of the current route
21+
*
22+
* @example `{ collection: 'posts', id: "post-id" }`.
23+
*/
24+
routeParams?: { [key: string]: string }
1925
/**
2026
* The search parameters of the current route provided to all pages in Next.js.
2127
*/

packages/next/src/views/Root/getRouteData.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export const getRouteData = ({
103103
config,
104104
importMap,
105105
route: currentRoute,
106+
routeParams: {},
106107
searchParams,
107108
}
108109

@@ -212,6 +213,8 @@ export const getRouteData = ({
212213
`/${segmentOne}` === config.admin.routes.browseByFolder
213214
) {
214215
// --> /browse-by-folder/:folderID
216+
initPageOptions.routeParams.folderID = folderID
217+
215218
ViewToRender = {
216219
Component: oneSegmentViews.browseByFolder,
217220
}
@@ -221,6 +224,7 @@ export const getRouteData = ({
221224
folderID = segmentTwo
222225
} else if (isCollection && matchedCollection) {
223226
// --> /collections/:collectionSlug
227+
initPageOptions.routeParams.collection = matchedCollection.slug
224228

225229
ViewToRender = {
226230
Component: ListView,
@@ -234,6 +238,7 @@ export const getRouteData = ({
234238
)
235239
} else if (isGlobal && matchedGlobal) {
236240
// --> /globals/:globalSlug
241+
initPageOptions.routeParams.global = matchedGlobal.slug
237242

238243
ViewToRender = {
239244
Component: DocumentView,
@@ -256,6 +261,8 @@ export const getRouteData = ({
256261
default:
257262
if (segmentTwo === 'verify') {
258263
// --> /:collectionSlug/verify/:token
264+
initPageOptions.routeParams.collection = segmentOne
265+
259266
ViewToRender = {
260267
Component: Verify,
261268
}
@@ -264,10 +271,14 @@ export const getRouteData = ({
264271
templateType = 'minimal'
265272
viewType = 'verify'
266273
} else if (isCollection && matchedCollection) {
274+
initPageOptions.routeParams.collection = matchedCollection.slug
267275
if (config.folders && segmentThree === config.folders.slug && matchedCollection.folders) {
268276
// Collection Folder Views
269277
// --> /collections/:collectionSlug/:folderCollectionSlug
270278
// --> /collections/:collectionSlug/:folderCollectionSlug/:folderID
279+
initPageOptions.routeParams.folderCollection = segmentThree
280+
initPageOptions.routeParams.folderID = segmentFour
281+
271282
ViewToRender = {
272283
Component: CollectionFolderView,
273284
}
@@ -283,6 +294,9 @@ export const getRouteData = ({
283294
// --> /collections/:collectionSlug/:id/preview
284295
// --> /collections/:collectionSlug/:id/versions
285296
// --> /collections/:collectionSlug/:id/versions/:versionID
297+
initPageOptions.routeParams.id = segmentThree
298+
initPageOptions.routeParams.versionID = segmentFive
299+
286300
ViewToRender = {
287301
Component: DocumentView,
288302
}
@@ -306,6 +320,8 @@ export const getRouteData = ({
306320
// --> /globals/:globalSlug/preview
307321
// --> /globals/:globalSlug/versions/:versionID
308322
// --> /globals/:globalSlug/api
323+
initPageOptions.routeParams.global = matchedGlobal.slug
324+
initPageOptions.routeParams.versionID = segmentFour
309325

310326
ViewToRender = {
311327
Component: DocumentView,

0 commit comments

Comments
 (0)