Skip to content

Commit 0cdd5b6

Browse files
authored
perf: disable router cache refresh that occurs after every page transition or page load (#8318)
This speeds up all page loads and reduces the amount of requests ## Example ### Clientside transition from dashboard => ui-fields list view #### Router cache disabled GET /admin/collections/ui-fields 200 in 33ms POST /api/form-state 200 in 9ms POST /api/form-state 200 in 10ms GET /api/payload-preferences/ui-fields-list 200 in 11ms GET /admin/collections/ui-fields?limit=10&sort=id 200 in 42ms #### Router cache enabled GET /admin/collections/ui-fields 200 in 33ms POST /api/form-state 200 in 11ms POST /api/form-state 200 in 12ms GET /api/payload-preferences/ui-fields-list 200 in 15ms GET /admin/collections/ui-fields?limit=10&sort=id 200 in 42ms **GET /admin/collections/ui-fields?limit=10&sort=id 200 in 82ms** <== this is gone
1 parent 63b446c commit 0cdd5b6

File tree

11 files changed

+33
-23
lines changed

11 files changed

+33
-23
lines changed

packages/next/src/views/List/Default/index.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ import {
2828
useListInfo,
2929
useListQuery,
3030
useModal,
31-
useRouteCache,
3231
useStepNav,
3332
useTranslation,
3433
useWindowInfo,
3534
ViewDescription,
3635
} from '@payloadcms/ui'
3736
import LinkImport from 'next/link.js'
37+
import { useRouter } from 'next/navigation.js'
3838
import { formatFilesize, isNumber } from 'payload/shared'
3939
import React, { Fragment, useEffect } from 'react'
4040

@@ -55,9 +55,10 @@ export const DefaultListView: React.FC = () => {
5555
newDocumentURL,
5656
} = useListInfo()
5757

58+
const router = useRouter()
59+
5860
const { data, defaultLimit, handlePageChange, handlePerPageChange, params } = useListQuery()
5961
const { openModal } = useModal()
60-
const { clearRouteCache } = useRouteCache()
6162
const { setCollectionSlug, setOnSuccess } = useBulkUpload()
6263
const { drawerSlug } = useBulkUpload()
6364

@@ -109,8 +110,8 @@ export const DefaultListView: React.FC = () => {
109110
const openBulkUpload = React.useCallback(() => {
110111
setCollectionSlug(collectionSlug)
111112
openModal(drawerSlug)
112-
setOnSuccess(clearRouteCache)
113-
}, [clearRouteCache, collectionSlug, drawerSlug, openModal, setCollectionSlug, setOnSuccess])
113+
setOnSuccess(() => router.refresh())
114+
}, [router, collectionSlug, drawerSlug, openModal, setCollectionSlug, setOnSuccess])
114115

115116
useEffect(() => {
116117
if (drawerDepth <= 1) {

packages/next/src/withPayload.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,21 @@
44
* @returns {import('next').NextConfig}
55
* */
66
export const withPayload = (nextConfig = {}) => {
7+
const env = nextConfig?.env || {}
8+
79
if (nextConfig.experimental?.staleTimes?.dynamic) {
810
console.warn(
9-
'Payload detected a non-zero value for the `staleTimes.dynamic` option in your Next.js config. This may cause stale data to load in the Admin Panel. To clear this warning, remove the `staleTimes.dynamic` option from your Next.js config or set it to 0. In the future, Next.js may support scoping this option to specific routes.',
11+
'Payload detected a non-zero value for the `staleTimes.dynamic` option in your Next.js config. This will slow down page transitions and may cause stale data to load within the Admin panel. To clear this warning, remove the `staleTimes.dynamic` option from your Next.js config or set it to 0. In the future, Next.js may support scoping this option to specific routes.',
1012
)
13+
env.NEXT_PUBLIC_ENABLE_ROUTER_CACHE_REFRESH = 'true'
1114
}
1215

1316
/**
1417
* @type {import('next').NextConfig}
1518
*/
1619
const toReturn = {
1720
...nextConfig,
18-
env: {
19-
...(nextConfig?.env || {}),
20-
},
21+
env,
2122
outputFileTracingExcludes: {
2223
...(nextConfig?.outputFileTracingExcludes || {}),
2324
'**/*': [

packages/payload/src/auth/operations/login.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ export const loginOperation = async <TSlug extends CollectionSlug>(
173173
req,
174174
where: whereConstraint,
175175
})
176+
user.collection = collectionConfig.slug
176177

177178
if (!user || (args.collection.config.auth.verify && user._verified === false)) {
178179
throw new AuthenticationError(req.t, Boolean(canLoginWithUsername && sanitizedUsername))

packages/payload/src/auth/operations/me.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,14 @@ export const meOperation = async (args: Arguments): Promise<MeOperationResult> =
3737
req,
3838
showHiddenFields: false,
3939
})) as User
40+
user.collection = collection.config.slug
4041

4142
if (req.user.collection !== collection.config.slug) {
4243
return {
4344
user: null,
4445
}
4546
}
4647

47-
delete user.collection
48-
4948
// /////////////////////////////////////
5049
// me hook - Collection
5150
// /////////////////////////////////////

packages/payload/src/auth/operations/refresh.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export const refreshOperation = async (incomingArgs: Arguments): Promise<Result>
7575
depth: isGraphQL ? 0 : args.collection.config.auth.depth,
7676
req: args.req,
7777
})
78+
user.collection = args.req.user.collection
7879

7980
let result: Result
8081

packages/payload/src/auth/types.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,24 @@ export type Permissions = {
6161
}
6262
}
6363

64-
export type User = {
65-
[key: string]: any // This NEEDS to be an any, otherwise it breaks the Omit for ClientUser below
64+
type BaseUser = {
6665
collection: string
6766
email?: string
6867
id: number | string
6968
username?: string
7069
}
7170

71+
export type User = {
72+
[key: string]: any
73+
} & BaseUser
74+
7275
/**
7376
* `collection` is not available one the client. It's only available on the server (req.user)
7477
* On the client, you can access the collection via config.admin.user. Config can be accessed using the useConfig() hook
7578
*/
76-
export type ClientUser = Omit<User, 'collection'>
79+
export type ClientUser = {
80+
[key: string]: any
81+
} & BaseUser
7782

7883
type GenerateVerifyEmailHTML<TUser = any> = (args: {
7984
req: PayloadRequest

packages/ui/src/elements/EditMany/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ export const EditMany: React.FC<EditManyProps> = (props) => {
173173
params: { page: selectAll === SelectAllStatus.AllAvailable ? '1' : undefined },
174174
}),
175175
)
176-
clearRouteCache()
176+
clearRouteCache() // Use clearRouteCache instead of router.refresh, as we only need to clear the cache if the user has route caching enabled - clearRouteCache checks for this
177177
closeModal(drawerSlug)
178178
}
179179

packages/ui/src/elements/PublishMany/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export const PublishMany: React.FC<PublishManyProps> = (props) => {
8181
}),
8282
)
8383

84-
clearRouteCache()
84+
clearRouteCache() // Use clearRouteCache instead of router.refresh, as we only need to clear the cache if the user has route caching enabled - clearRouteCache checks for this
8585
return null
8686
}
8787

packages/ui/src/elements/UnpublishMany/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export const UnpublishMany: React.FC<UnpublishManyProps> = (props) => {
7878
},
7979
}),
8080
)
81-
clearRouteCache()
81+
clearRouteCache() // Use clearRouteCache instead of router.refresh, as we only need to clear the cache if the user has route caching enabled - clearRouteCache checks for this
8282
return null
8383
}
8484

packages/ui/src/providers/Root/index.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,12 @@ export const RootProvider: React.FC<Props> = ({
5858
translations,
5959
user,
6060
}) => {
61+
const RouteCacheComponent =
62+
process.env.NEXT_PUBLIC_ENABLE_ROUTER_CACHE_REFRESH === 'true' ? RouteCache : Fragment
63+
6164
return (
6265
<Fragment>
63-
<RouteCache>
66+
<RouteCacheComponent>
6467
<ConfigProvider config={config}>
6568
<FieldComponentsProvider fieldComponents={fieldComponents}>
6669
<ClientFunctionProvider>
@@ -114,7 +117,7 @@ export const RootProvider: React.FC<Props> = ({
114117
</ClientFunctionProvider>
115118
</FieldComponentsProvider>
116119
</ConfigProvider>
117-
</RouteCache>
120+
</RouteCacheComponent>
118121
<ToastContainer />
119122
</Fragment>
120123
)

0 commit comments

Comments
 (0)