@@ -6,49 +6,61 @@ const client = contentful.createClient({
66} )
77
88export const monthNames = [
9- 'Jan' ,
10- 'Feb' ,
11- 'Mar' ,
12- 'Apr' ,
13- 'May' ,
14- 'Jun' ,
15- 'Jul' ,
16- 'Aug' ,
17- 'Sep' ,
18- 'Oct' ,
19- 'Nov' ,
20- 'Dec'
9+ 'Jan' , 'Feb' , 'Mar' , 'Apr' , 'May' , 'Jun' , 'Jul' , 'Aug' , 'Sep' , 'Oct' , 'Nov' , 'Dec'
2110]
2211
12+ // --------- one cached fetch for *all* entries ---------
13+
14+ let allEntriesPromise = null
15+
16+ export const getEntries = async ( ) => {
17+ if ( allEntriesPromise ) return allEntriesPromise
18+
19+ allEntriesPromise = ( async ( ) => {
20+ const pageSize = 1000
21+ let skip = 0
22+ const all = [ ]
23+
24+ while ( true ) {
25+ const res = await client . getEntries ( { limit : pageSize , skip } )
26+ all . push ( ...res . items )
27+ skip += res . items . length
28+ if ( all . length >= res . total || res . items . length === 0 ) break
29+ }
30+
31+ return all
32+ } ) ( ) . catch ( ( err ) => {
33+ allEntriesPromise = null
34+ throw err
35+ } )
36+
37+ return allEntriesPromise
38+ }
39+
40+ const byType = ( items , contentTypeId ) =>
41+ items . filter ( ( e ) => e . sys ?. contentType ?. sys ?. id === contentTypeId )
42+
2343export const getNews = async ( ) => {
24- const items = await client
25- . getEntries ( {
26- content_type : 'latestNews'
27- } )
28- . then ( ( { items } ) => items . map ( ( { fields } ) => ( {
29- ...fields ,
30- image : fields . image ?. fields ,
31- imageDark : fields . imageDark ?. fields // optional, for darkmode, eg white text variant
32- } ) ) )
33- return items
44+ const items = byType ( await getEntries ( ) , 'latestNews' )
45+ return items . map ( ( { fields } ) => ( {
46+ ...fields ,
47+ image : fields . image ?. fields ,
48+ imageDark : fields . imageDark ?. fields
49+ } ) )
3450}
3551
3652export const getEvents = async ( ) => {
37- const items = await client
38- . getEntries ( {
39- content_type : 'event'
40- } )
41- . then ( ( { items } ) => items . map ( ( { fields } ) => ( {
42- ...fields ,
43- image : fields . image ?. fields
44- } ) ) )
45- return items
53+ const items = byType ( await getEntries ( ) , 'event' )
54+ return items . map ( ( { fields } ) => ( {
55+ ...fields ,
56+ image : fields . image ?. fields
57+ } ) )
4658}
4759
4860export const getSponsors = async ( ) => {
49- const { items } = await client
50- . getEntries ( {
51- content_type : 'sponsor'
52- } )
53- return items
61+ return byType ( await getEntries ( ) , 'sponsor' )
62+ }
63+
64+ export const clearEntriesCache = ( ) => {
65+ allEntriesPromise = null
5466}
0 commit comments