Skip to content

Commit aa1d300

Browse files
authored
feat(templates): website template performance improvements (#9466)
- Uses `pagination: false` where we don't need `totalDocs`. - in `preview/route.ts` uses `depth: 0`, select of only ID to improve performance - in `search` uses `select` to select only needed properties - adds type safety best practices to collection configs with `defaultPopulate` - uses `payload.count` to resolve SSG `pageNumber`s
1 parent 150c55d commit aa1d300

File tree

20 files changed

+83
-31
lines changed

20 files changed

+83
-31
lines changed

templates/website/src/app/(frontend)/[slug]/page.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ const queryPageBySlug = cache(async ({ slug }: { slug: string }) => {
9494
collection: 'pages',
9595
draft,
9696
limit: 1,
97+
pagination: false,
9798
overrideAccess: draft,
9899
where: {
99100
slug: {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,13 @@ export async function GET(
6767
// Verify the given slug exists
6868
try {
6969
const docs = await payload.find({
70-
collection: collection,
70+
collection,
7171
draft: true,
72+
limit: 1,
73+
// pagination: false reduces overhead if you don't need totalDocs
74+
pagination: false,
75+
depth: 0,
76+
select: {},
7277
where: {
7378
slug: {
7479
equals: slug,

templates/website/src/app/(frontend)/posts/[slug]/page.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ const queryPostBySlug = cache(async ({ slug }: { slug: string }) => {
8787
draft,
8888
limit: 1,
8989
overrideAccess: draft,
90+
pagination: false,
9091
where: {
9192
slug: {
9293
equals: slug,

templates/website/src/app/(frontend)/posts/page.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ export default async function Page() {
1919
depth: 1,
2020
limit: 12,
2121
overrideAccess: false,
22+
select: {
23+
title: true,
24+
slug: true,
25+
categories: true,
26+
meta: true,
27+
},
2228
})
2329

2430
return (

templates/website/src/app/(frontend)/posts/page/[pageNumber]/page.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,17 @@ export async function generateMetadata({ params: paramsPromise }: Args): Promise
7171

7272
export async function generateStaticParams() {
7373
const payload = await getPayload({ config: configPromise })
74-
const posts = await payload.find({
74+
const { totalDocs } = await payload.count({
7575
collection: 'posts',
76-
depth: 0,
77-
limit: 10,
78-
draft: false,
7976
overrideAccess: false,
8077
})
8178

79+
const totalPages = Math.ceil(totalDocs / 10)
80+
8281
const pages: { pageNumber: string }[] = []
8382

84-
if (posts.totalPages) {
85-
for (let i = 1; i <= posts.totalPages; i++) {
86-
pages.push({ pageNumber: String(i) })
87-
}
83+
for (let i = 1; i <= totalPages; i++) {
84+
pages.push({ pageNumber: String(i) })
8885
}
8986

9087
return pages

templates/website/src/app/(frontend)/search/page.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import React from 'react'
77
import { Post } from '@/payload-types'
88
import { Search } from '@/search/Component'
99
import PageClient from './page.client'
10+
import { CardPostData } from '@/components/Card'
1011

1112
type Args = {
1213
searchParams: Promise<{
@@ -21,6 +22,14 @@ export default async function Page({ searchParams: searchParamsPromise }: Args)
2122
collection: 'search',
2223
depth: 1,
2324
limit: 12,
25+
select: {
26+
title: true,
27+
slug: true,
28+
categories: true,
29+
meta: true,
30+
},
31+
// pagination: false reduces overhead if you don't need totalDocs
32+
pagination: false,
2433
...(query
2534
? {
2635
where: {
@@ -62,7 +71,7 @@ export default async function Page({ searchParams: searchParamsPromise }: Args)
6271
</div>
6372

6473
{posts.totalDocs > 0 ? (
65-
<CollectionArchive posts={posts.docs as unknown as Post[]} />
74+
<CollectionArchive posts={posts.docs as CardPostData[]} />
6675
) : (
6776
<div className="container">No results found.</div>
6877
)}

templates/website/src/collections/Pages/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
} from '@payloadcms/plugin-seo/fields'
2323
import { getServerSideURL } from '@/utilities/getURL'
2424

25-
export const Pages: CollectionConfig = {
25+
export const Pages: CollectionConfig<'pages'> = {
2626
slug: 'pages',
2727
access: {
2828
create: authenticated,
@@ -32,6 +32,7 @@ export const Pages: CollectionConfig = {
3232
},
3333
// This config controls what's populated by default when a page is referenced
3434
// https://payloadcms.com/docs/queries/select#defaultpopulate-collection-config-property
35+
// Type safe if the collection slug generic is passed to `CollectionConfig` - `CollectionConfig<'pagess'>
3536
defaultPopulate: {
3637
title: true,
3738
slug: true,

templates/website/src/collections/Posts/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
import { slugField } from '@/fields/slug'
2929
import { getServerSideURL } from '@/utilities/getURL'
3030

31-
export const Posts: CollectionConfig = {
31+
export const Posts: CollectionConfig<'posts'> = {
3232
slug: 'posts',
3333
access: {
3434
create: authenticated,
@@ -38,6 +38,7 @@ export const Posts: CollectionConfig = {
3838
},
3939
// This config controls what's populated by default when a post is referenced
4040
// https://payloadcms.com/docs/queries/select#defaultpopulate-collection-config-property
41+
// Type safe if the collection slug generic is passed to `CollectionConfig` - `CollectionConfig<'posts'>
4142
defaultPopulate: {
4243
title: true,
4344
slug: true,

templates/website/src/components/Card/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import type { Post } from '@/payload-types'
88

99
import { Media } from '@/components/Media'
1010

11+
export type CardPostData = Pick<Post, 'slug' | 'categories' | 'meta' | 'title'>
12+
1113
export const Card: React.FC<{
1214
alignItems?: 'center'
1315
className?: string
14-
doc?: Post
16+
doc?: CardPostData
1517
relationTo?: 'posts'
1618
showCategories?: boolean
1719
title?: string

templates/website/src/components/CollectionArchive/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import React from 'react'
33

44
import type { Post } from '@/payload-types'
55

6-
import { Card } from '@/components/Card'
6+
import { Card, CardPostData } from '@/components/Card'
77

88
export type Props = {
9-
posts: Post[]
9+
posts: CardPostData[]
1010
}
1111

1212
export const CollectionArchive: React.FC<Props> = (props) => {

0 commit comments

Comments
 (0)