File tree Expand file tree Collapse file tree 20 files changed +83
-31
lines changed Expand file tree Collapse file tree 20 files changed +83
-31
lines changed Original file line number Diff line number Diff line change @@ -94,6 +94,7 @@ const queryPageBySlug = cache(async ({ slug }: { slug: string }) => {
94
94
collection : 'pages' ,
95
95
draft,
96
96
limit : 1 ,
97
+ pagination : false ,
97
98
overrideAccess : draft ,
98
99
where : {
99
100
slug : {
Original file line number Diff line number Diff line change @@ -67,8 +67,13 @@ export async function GET(
67
67
// Verify the given slug exists
68
68
try {
69
69
const docs = await payload . find ( {
70
- collection : collection ,
70
+ collection,
71
71
draft : true ,
72
+ limit : 1 ,
73
+ // pagination: false reduces overhead if you don't need totalDocs
74
+ pagination : false ,
75
+ depth : 0 ,
76
+ select : { } ,
72
77
where : {
73
78
slug : {
74
79
equals : slug ,
Original file line number Diff line number Diff line change @@ -87,6 +87,7 @@ const queryPostBySlug = cache(async ({ slug }: { slug: string }) => {
87
87
draft,
88
88
limit : 1 ,
89
89
overrideAccess : draft ,
90
+ pagination : false ,
90
91
where : {
91
92
slug : {
92
93
equals : slug ,
Original file line number Diff line number Diff line change @@ -19,6 +19,12 @@ export default async function Page() {
19
19
depth : 1 ,
20
20
limit : 12 ,
21
21
overrideAccess : false ,
22
+ select : {
23
+ title : true ,
24
+ slug : true ,
25
+ categories : true ,
26
+ meta : true ,
27
+ } ,
22
28
} )
23
29
24
30
return (
Original file line number Diff line number Diff line change @@ -71,20 +71,17 @@ export async function generateMetadata({ params: paramsPromise }: Args): Promise
71
71
72
72
export async function generateStaticParams ( ) {
73
73
const payload = await getPayload ( { config : configPromise } )
74
- const posts = await payload . find ( {
74
+ const { totalDocs } = await payload . count ( {
75
75
collection : 'posts' ,
76
- depth : 0 ,
77
- limit : 10 ,
78
- draft : false ,
79
76
overrideAccess : false ,
80
77
} )
81
78
79
+ const totalPages = Math . ceil ( totalDocs / 10 )
80
+
82
81
const pages : { pageNumber : string } [ ] = [ ]
83
82
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 ) } )
88
85
}
89
86
90
87
return pages
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ import React from 'react'
7
7
import { Post } from '@/payload-types'
8
8
import { Search } from '@/search/Component'
9
9
import PageClient from './page.client'
10
+ import { CardPostData } from '@/components/Card'
10
11
11
12
type Args = {
12
13
searchParams : Promise < {
@@ -21,6 +22,14 @@ export default async function Page({ searchParams: searchParamsPromise }: Args)
21
22
collection : 'search' ,
22
23
depth : 1 ,
23
24
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 ,
24
33
...( query
25
34
? {
26
35
where : {
@@ -62,7 +71,7 @@ export default async function Page({ searchParams: searchParamsPromise }: Args)
62
71
</ div >
63
72
64
73
{ posts . totalDocs > 0 ? (
65
- < CollectionArchive posts = { posts . docs as unknown as Post [ ] } />
74
+ < CollectionArchive posts = { posts . docs as CardPostData [ ] } />
66
75
) : (
67
76
< div className = "container" > No results found.</ div >
68
77
) }
Original file line number Diff line number Diff line change @@ -22,7 +22,7 @@ import {
22
22
} from '@payloadcms/plugin-seo/fields'
23
23
import { getServerSideURL } from '@/utilities/getURL'
24
24
25
- export const Pages : CollectionConfig = {
25
+ export const Pages : CollectionConfig < 'pages' > = {
26
26
slug : 'pages' ,
27
27
access : {
28
28
create : authenticated ,
@@ -32,6 +32,7 @@ export const Pages: CollectionConfig = {
32
32
} ,
33
33
// This config controls what's populated by default when a page is referenced
34
34
// https://payloadcms.com/docs/queries/select#defaultpopulate-collection-config-property
35
+ // Type safe if the collection slug generic is passed to `CollectionConfig` - `CollectionConfig<'pagess'>
35
36
defaultPopulate : {
36
37
title : true ,
37
38
slug : true ,
Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ import {
28
28
import { slugField } from '@/fields/slug'
29
29
import { getServerSideURL } from '@/utilities/getURL'
30
30
31
- export const Posts : CollectionConfig = {
31
+ export const Posts : CollectionConfig < 'posts' > = {
32
32
slug : 'posts' ,
33
33
access : {
34
34
create : authenticated ,
@@ -38,6 +38,7 @@ export const Posts: CollectionConfig = {
38
38
} ,
39
39
// This config controls what's populated by default when a post is referenced
40
40
// https://payloadcms.com/docs/queries/select#defaultpopulate-collection-config-property
41
+ // Type safe if the collection slug generic is passed to `CollectionConfig` - `CollectionConfig<'posts'>
41
42
defaultPopulate : {
42
43
title : true ,
43
44
slug : true ,
Original file line number Diff line number Diff line change @@ -8,10 +8,12 @@ import type { Post } from '@/payload-types'
8
8
9
9
import { Media } from '@/components/Media'
10
10
11
+ export type CardPostData = Pick < Post , 'slug' | 'categories' | 'meta' | 'title' >
12
+
11
13
export const Card : React . FC < {
12
14
alignItems ?: 'center'
13
15
className ?: string
14
- doc ?: Post
16
+ doc ?: CardPostData
15
17
relationTo ?: 'posts'
16
18
showCategories ?: boolean
17
19
title ?: string
Original file line number Diff line number Diff line change @@ -3,10 +3,10 @@ import React from 'react'
3
3
4
4
import type { Post } from '@/payload-types'
5
5
6
- import { Card } from '@/components/Card'
6
+ import { Card , CardPostData } from '@/components/Card'
7
7
8
8
export type Props = {
9
- posts : Post [ ]
9
+ posts : CardPostData [ ]
10
10
}
11
11
12
12
export const CollectionArchive : React . FC < Props > = ( props ) => {
You can’t perform that action at this time.
0 commit comments