Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(next): reduce getNavPrefs calls from 3 to 1 per page load #11318

Merged
merged 1 commit into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions packages/next/src/elements/Nav/getNavPrefs.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import type { NavPreferences, Payload, User } from 'payload'
import type { DefaultDocumentIDType, NavPreferences, Payload, User } from 'payload'

import { cache } from 'react'

export const getNavPrefs = cache(
async ({ payload, user }: { payload: Payload; user: User }): Promise<NavPreferences> =>
user
async (
payload: Payload,
userID: DefaultDocumentIDType,
userSlug: string,
): Promise<NavPreferences> => {
return userSlug
? await payload
.find({
collection: 'payload-preferences',
depth: 0,
limit: 1,
user,
pagination: false,
where: {
and: [
{
Expand All @@ -20,17 +24,18 @@ export const getNavPrefs = cache(
},
{
'user.relationTo': {
equals: user.collection,
equals: userSlug,
},
},
{
'user.value': {
equals: user.id,
equals: userID,
},
},
],
},
})
?.then((res) => res?.docs?.[0]?.value)
: null,
: null
},
)
2 changes: 1 addition & 1 deletion packages/next/src/elements/Nav/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const DefaultNav: React.FC<NavProps> = async (props) => {
i18n,
)

const navPreferences = await getNavPrefs({ payload, user })
const navPreferences = await getNavPrefs(payload, user?.id, user?.collection)

const LogoutComponent = RenderServerComponent({
clientProps: {
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/layouts/Root/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const RootLayout = async ({
})
}

const navPrefs = await getNavPrefs({ payload, user: req.user })
const navPrefs = await getNavPrefs(req.payload, req.user?.id, req.user?.collection)

const clientConfig = getClientConfig({
config,
Expand Down