Skip to content

Commit a7a0501

Browse files
authored
feat(next): add redirect from ${adminRoute}/collections to ${adminRoute} (#13061)
Occasionally, I find myself on a URL like `https://domain.com/admin/collections/myCollection/docId` and I modify the URL with the intention of going to the admin panel, but I shorten it in the wrong place: `https://domain.com/admin/collections`. The confusion arises because the admin panel basically displays the collections. I think this redirect is a subtle but nice touch, since `/collections` is a URL that doesn't exist. EDIT: now I'm doing also the same thing for `/globals`
1 parent 1d6ffcb commit a7a0501

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

packages/next/src/views/Root/index.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import React from 'react'
1717
import { DefaultTemplate } from '../../templates/Default/index.js'
1818
import { MinimalTemplate } from '../../templates/Minimal/index.js'
1919
import { initPage } from '../../utilities/initPage/index.js'
20+
import { getCustomViewByRoute } from './getCustomViewByRoute.js'
2021
import { getRouteData } from './getRouteData.js'
2122

2223
export type GenerateViewMetadata = (args: {
@@ -62,6 +63,32 @@ export const RootPage = async ({
6263

6364
const searchParams = await searchParamsPromise
6465

66+
// Redirect `${adminRoute}/collections` to `${adminRoute}`
67+
if (segments.length === 1 && segments[0] === 'collections') {
68+
const { viewKey } = getCustomViewByRoute({
69+
config,
70+
currentRoute: '/collections',
71+
})
72+
73+
// Only redirect if there's NO custom view configured for /collections
74+
if (!viewKey) {
75+
redirect(adminRoute)
76+
}
77+
}
78+
79+
// Redirect `${adminRoute}/globals` to `${adminRoute}`
80+
if (segments.length === 1 && segments[0] === 'globals') {
81+
const { viewKey } = getCustomViewByRoute({
82+
config,
83+
currentRoute: '/globals',
84+
})
85+
86+
// Only redirect if there's NO custom view configured for /globals
87+
if (!viewKey) {
88+
redirect(adminRoute)
89+
}
90+
}
91+
6592
const {
6693
browseByFolderSlugs,
6794
DefaultView,

test/admin-root/e2e.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ test.describe('Admin Panel (Root)', () => {
6464
// })
6565
// })
6666

67+
test('should redirect `${adminRoute}/collections` to `${adminRoute}', async () => {
68+
const collectionsURL = `${url.admin}/collections`
69+
await page.goto(collectionsURL)
70+
// Should redirect to dashboard
71+
await expect.poll(() => page.url()).toBe(`${url.admin}`)
72+
})
73+
6774
test('renders admin panel at root', async () => {
6875
await page.goto(url.admin)
6976
const pageURL = page.url()

test/admin/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ export default buildConfigWithDefaults({
8383
views: {
8484
// Dashboard: CustomDashboardView,
8585
// Account: CustomAccountView,
86+
collections: {
87+
Component: '/components/views/CustomView/index.js#CustomView',
88+
path: '/collections',
89+
},
8690
CustomDefaultView: {
8791
Component: '/components/views/CustomDefault/index.js#CustomDefaultView',
8892
path: '/custom-default-view',

test/admin/e2e/general/e2e.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,19 @@ describe('General', () => {
358358
const response = await page.goto(customLogoutRouteURL)
359359
expect(response.status() !== 404).toBeTruthy()
360360
})
361+
362+
test('should not redirect `${adminRoute}/collections` to `${adminRoute} if there is a custom view', async () => {
363+
const collectionsURL = `${serverURL}/admin/collections`
364+
await page.goto(collectionsURL)
365+
await expect(page.getByText('Custom View').first()).toBeVisible()
366+
})
367+
368+
test('should redirect `${adminRoute}/globals` to `${adminRoute}', async () => {
369+
const globalsURL = `${serverURL}/admin/globals`
370+
await page.goto(globalsURL)
371+
// Should redirect to dashboard
372+
await expect.poll(() => page.url()).toBe(`${serverURL}/admin`)
373+
})
361374
})
362375

363376
describe('navigation', () => {

0 commit comments

Comments
 (0)