Skip to content

Commit

Permalink
perf(next): completely skip query to find out if published document e…
Browse files Browse the repository at this point in the history
…xists, if already-queried document is published
  • Loading branch information
AlessioGr committed Nov 23, 2024
1 parent 7f88161 commit 81763b6
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 46 deletions.
1 change: 1 addition & 0 deletions packages/next/src/views/Account/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export const Account: React.FC<AdminViewProps> = async ({
await getVersions({
id: user.id,
collectionConfig,
doc: data,
docPermissions,
locale: locale?.code,
payload,
Expand Down
96 changes: 57 additions & 39 deletions packages/next/src/views/Document/getVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ import { sanitizeID } from '@payloadcms/ui/shared'

type Args = {
collectionConfig?: SanitizedCollectionConfig
/**
* Optional - performance optimization.
* If a document has been fetched before fetching versions, pass it here.
* If this document is set to published, we can skip the query to find out if a published document exists,
* as the passed in document is proof of its existence.
*/
doc?: Record<string, any>
docPermissions: SanitizedDocumentPermissions
globalConfig?: SanitizedGlobalConfig
id?: number | string
Expand All @@ -30,6 +37,7 @@ type Result = Promise<{
export const getVersions = async ({
id: idArg,
collectionConfig,
doc,
docPermissions,
globalConfig,
locale,
Expand Down Expand Up @@ -70,40 +78,45 @@ export const getVersions = async ({
}

if (versionsConfig?.drafts) {
publishedQuery = await payload.find({
collection: collectionConfig.slug,
depth: 0,
limit: 1,
locale: locale || undefined,
pagination: false,
select: {
updatedAt: true,
},
user,
where: {
and: [
{
or: [
{
_status: {
equals: 'published',
// Find out if a published document exists
if (doc?._status === 'published') {
publishedQuery = doc
} else {
publishedQuery = await payload.find({
collection: collectionConfig.slug,
depth: 0,
limit: 1,
locale: locale || undefined,
pagination: false,
select: {
updatedAt: true,
},
user,
where: {
and: [
{
or: [
{
_status: {
equals: 'published',
},
},
},
{
_status: {
exists: false,
{
_status: {
exists: false,
},
},
],
},
{
id: {
equals: id,
},
],
},
{
id: {
equals: id,
},
},
],
},
})
],
},
})
}

if (publishedQuery.docs?.[0]) {
hasPublishedDoc = true
Expand Down Expand Up @@ -182,16 +195,21 @@ export const getVersions = async ({
}

if (globalConfig) {
// Find out if a published document exists
if (versionsConfig?.drafts) {
publishedQuery = await payload.findGlobal({
slug: globalConfig.slug,
depth: 0,
locale,
select: {
updatedAt: true,
},
user,
})
if (doc?._status === 'published') {
publishedQuery = doc
} else {
publishedQuery = await payload.findGlobal({
slug: globalConfig.slug,
depth: 0,
locale,
select: {
updatedAt: true,
},
user,
})
}

if (publishedQuery?._status === 'published') {
hasPublishedDoc = true
Expand Down
9 changes: 2 additions & 7 deletions packages/next/src/views/Document/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import type {
AdminViewProps,
Data,
PayloadComponent,
ServerProps,
ServerSideEditViewProps,
} from 'payload'
import type { AdminViewProps, Data, PayloadComponent, ServerSideEditViewProps } from 'payload'

import { DocumentInfoProvider, EditDepthProvider, HydrateAuthProvider } from '@payloadcms/ui'
import { RenderServerComponent } from '@payloadcms/ui/elements/RenderServerComponent'
Expand Down Expand Up @@ -137,6 +131,7 @@ export const renderDocument = async ({
getVersions({
id: idFromArgs,
collectionConfig,
doc,
docPermissions,
globalConfig,
locale: locale?.code,
Expand Down

0 comments on commit 81763b6

Please sign in to comment.