Skip to content

Commit

Permalink
templates: improve speed of seed script (#9748)
Browse files Browse the repository at this point in the history
Improves the speed of the seed script by moving operations to be async
and disabling revalidation on them
  • Loading branch information
paulpopus authored Dec 4, 2024
1 parent 84a5b40 commit 2321970
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 203 deletions.
8 changes: 5 additions & 3 deletions templates/website/src/Footer/hooks/revalidateFooter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import type { GlobalAfterChangeHook } from 'payload'

import { revalidateTag } from 'next/cache'

export const revalidateFooter: GlobalAfterChangeHook = ({ doc, req: { payload } }) => {
payload.logger.info(`Revalidating footer`)
export const revalidateFooter: GlobalAfterChangeHook = ({ doc, req: { payload, context } }) => {
if (!context.disableRevalidate) {
payload.logger.info(`Revalidating footer`)

revalidateTag('global_footer')
revalidateTag('global_footer')
}

return doc
}
8 changes: 5 additions & 3 deletions templates/website/src/Header/hooks/revalidateHeader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import type { GlobalAfterChangeHook } from 'payload'

import { revalidateTag } from 'next/cache'

export const revalidateHeader: GlobalAfterChangeHook = ({ doc, req: { payload } }) => {
payload.logger.info(`Revalidating header`)
export const revalidateHeader: GlobalAfterChangeHook = ({ doc, req: { payload, context } }) => {
if (!context.disableRevalidate) {
payload.logger.info(`Revalidating header`)

revalidateTag('global_header')
revalidateTag('global_header')
}

return doc
}
1 change: 0 additions & 1 deletion templates/website/src/app/(frontend)/next/seed/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { seed } from '@/endpoints/seed'
import config from '@payload-config'
import { headers } from 'next/headers'

const payloadToken = 'payload-token'
export const maxDuration = 60 // This function can run for a maximum of 60 seconds

export async function POST(
Expand Down
39 changes: 21 additions & 18 deletions templates/website/src/collections/Pages/hooks/revalidatePage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,37 @@ import type { Page } from '../../../payload-types'
export const revalidatePage: CollectionAfterChangeHook<Page> = ({
doc,
previousDoc,
req: { payload },
req: { payload, context },
}) => {
if (doc._status === 'published') {
const path = doc.slug === 'home' ? '/' : `/${doc.slug}`
if (!context.disableRevalidate) {
if (doc._status === 'published') {
const path = doc.slug === 'home' ? '/' : `/${doc.slug}`

payload.logger.info(`Revalidating page at path: ${path}`)
payload.logger.info(`Revalidating page at path: ${path}`)

revalidatePath(path)
revalidateTag('pages-sitemap')
}
revalidatePath(path)
revalidateTag('pages-sitemap')
}

// If the page was previously published, we need to revalidate the old path
if (previousDoc?._status === 'published' && doc._status !== 'published') {
const oldPath = previousDoc.slug === 'home' ? '/' : `/${previousDoc.slug}`
// If the page was previously published, we need to revalidate the old path
if (previousDoc?._status === 'published' && doc._status !== 'published') {
const oldPath = previousDoc.slug === 'home' ? '/' : `/${previousDoc.slug}`

payload.logger.info(`Revalidating old page at path: ${oldPath}`)
payload.logger.info(`Revalidating old page at path: ${oldPath}`)

revalidatePath(oldPath)
revalidateTag('pages-sitemap')
revalidatePath(oldPath)
revalidateTag('pages-sitemap')
}
}

return doc
}

export const revalidateDelete: CollectionAfterDeleteHook<Page> = ({ doc }) => {
const path = doc?.slug === 'home' ? '/' : `/${doc?.slug}`
revalidatePath(path)
revalidateTag('pages-sitemap')
export const revalidateDelete: CollectionAfterDeleteHook<Page> = ({ doc, req: { context } }) => {
if (!context.disableRevalidate) {
const path = doc?.slug === 'home' ? '/' : `/${doc?.slug}`
revalidatePath(path)
revalidateTag('pages-sitemap')
}

return doc
}
39 changes: 21 additions & 18 deletions templates/website/src/collections/Posts/hooks/revalidatePost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,38 @@ import type { Post } from '../../../payload-types'
export const revalidatePost: CollectionAfterChangeHook<Post> = ({
doc,
previousDoc,
req: { payload },
req: { payload, context },
}) => {
if (doc._status === 'published') {
const path = `/posts/${doc.slug}`
if (!context.disableRevalidate) {
if (doc._status === 'published') {
const path = `/posts/${doc.slug}`

payload.logger.info(`Revalidating post at path: ${path}`)
payload.logger.info(`Revalidating post at path: ${path}`)

revalidatePath(path)
revalidateTag('posts-sitemap')
}
revalidatePath(path)
revalidateTag('posts-sitemap')
}

// If the post was previously published, we need to revalidate the old path
if (previousDoc._status === 'published' && doc._status !== 'published') {
const oldPath = `/posts/${previousDoc.slug}`
// If the post was previously published, we need to revalidate the old path
if (previousDoc._status === 'published' && doc._status !== 'published') {
const oldPath = `/posts/${previousDoc.slug}`

payload.logger.info(`Revalidating old post at path: ${oldPath}`)
payload.logger.info(`Revalidating old post at path: ${oldPath}`)

revalidatePath(oldPath)
revalidateTag('posts-sitemap')
revalidatePath(oldPath)
revalidateTag('posts-sitemap')
}
}

return doc
}

export const revalidateDelete: CollectionAfterDeleteHook<Post> = ({ doc }) => {
const path = `/posts/${doc?.slug}`
export const revalidateDelete: CollectionAfterDeleteHook<Post> = ({ doc, req: { context } }) => {
if (!context.disableRevalidate) {
const path = `/posts/${doc?.slug}`

revalidatePath(path)
revalidateTag('posts-sitemap')
revalidatePath(path)
revalidateTag('posts-sitemap')
}

return doc
}
Loading

0 comments on commit 2321970

Please sign in to comment.