Skip to content

Commit 2321970

Browse files
authored
templates: improve speed of seed script (#9748)
Improves the speed of the seed script by moving operations to be async and disabling revalidation on them
1 parent 84a5b40 commit 2321970

File tree

7 files changed

+237
-203
lines changed

7 files changed

+237
-203
lines changed

templates/website/src/Footer/hooks/revalidateFooter.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import type { GlobalAfterChangeHook } from 'payload'
22

33
import { revalidateTag } from 'next/cache'
44

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

8-
revalidateTag('global_footer')
9+
revalidateTag('global_footer')
10+
}
911

1012
return doc
1113
}

templates/website/src/Header/hooks/revalidateHeader.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import type { GlobalAfterChangeHook } from 'payload'
22

33
import { revalidateTag } from 'next/cache'
44

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

8-
revalidateTag('global_header')
9+
revalidateTag('global_header')
10+
}
911

1012
return doc
1113
}

templates/website/src/app/(frontend)/next/seed/route.ts

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { seed } from '@/endpoints/seed'
33
import config from '@payload-config'
44
import { headers } from 'next/headers'
55

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

98
export async function POST(

templates/website/src/collections/Pages/hooks/revalidatePage.ts

+21-18
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,37 @@ import type { Page } from '../../../payload-types'
77
export const revalidatePage: CollectionAfterChangeHook<Page> = ({
88
doc,
99
previousDoc,
10-
req: { payload },
10+
req: { payload, context },
1111
}) => {
12-
if (doc._status === 'published') {
13-
const path = doc.slug === 'home' ? '/' : `/${doc.slug}`
12+
if (!context.disableRevalidate) {
13+
if (doc._status === 'published') {
14+
const path = doc.slug === 'home' ? '/' : `/${doc.slug}`
1415

15-
payload.logger.info(`Revalidating page at path: ${path}`)
16+
payload.logger.info(`Revalidating page at path: ${path}`)
1617

17-
revalidatePath(path)
18-
revalidateTag('pages-sitemap')
19-
}
18+
revalidatePath(path)
19+
revalidateTag('pages-sitemap')
20+
}
2021

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

25-
payload.logger.info(`Revalidating old page at path: ${oldPath}`)
26+
payload.logger.info(`Revalidating old page at path: ${oldPath}`)
2627

27-
revalidatePath(oldPath)
28-
revalidateTag('pages-sitemap')
28+
revalidatePath(oldPath)
29+
revalidateTag('pages-sitemap')
30+
}
2931
}
30-
3132
return doc
3233
}
3334

34-
export const revalidateDelete: CollectionAfterDeleteHook<Page> = ({ doc }) => {
35-
const path = doc?.slug === 'home' ? '/' : `/${doc?.slug}`
36-
revalidatePath(path)
37-
revalidateTag('pages-sitemap')
35+
export const revalidateDelete: CollectionAfterDeleteHook<Page> = ({ doc, req: { context } }) => {
36+
if (!context.disableRevalidate) {
37+
const path = doc?.slug === 'home' ? '/' : `/${doc?.slug}`
38+
revalidatePath(path)
39+
revalidateTag('pages-sitemap')
40+
}
3841

3942
return doc
4043
}

templates/website/src/collections/Posts/hooks/revalidatePost.ts

+21-18
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,38 @@ import type { Post } from '../../../payload-types'
77
export const revalidatePost: CollectionAfterChangeHook<Post> = ({
88
doc,
99
previousDoc,
10-
req: { payload },
10+
req: { payload, context },
1111
}) => {
12-
if (doc._status === 'published') {
13-
const path = `/posts/${doc.slug}`
12+
if (!context.disableRevalidate) {
13+
if (doc._status === 'published') {
14+
const path = `/posts/${doc.slug}`
1415

15-
payload.logger.info(`Revalidating post at path: ${path}`)
16+
payload.logger.info(`Revalidating post at path: ${path}`)
1617

17-
revalidatePath(path)
18-
revalidateTag('posts-sitemap')
19-
}
18+
revalidatePath(path)
19+
revalidateTag('posts-sitemap')
20+
}
2021

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

25-
payload.logger.info(`Revalidating old post at path: ${oldPath}`)
26+
payload.logger.info(`Revalidating old post at path: ${oldPath}`)
2627

27-
revalidatePath(oldPath)
28-
revalidateTag('posts-sitemap')
28+
revalidatePath(oldPath)
29+
revalidateTag('posts-sitemap')
30+
}
2931
}
30-
3132
return doc
3233
}
3334

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

37-
revalidatePath(path)
38-
revalidateTag('posts-sitemap')
39+
revalidatePath(path)
40+
revalidateTag('posts-sitemap')
41+
}
3942

4043
return doc
4144
}

0 commit comments

Comments
 (0)