Skip to content

Commit

Permalink
Fix: NextJS Warning log "Large page data"
Browse files Browse the repository at this point in the history
  • Loading branch information
nkokla committed Oct 2, 2023
1 parent 905b9e5 commit 56d7ee1
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 18 deletions.
6 changes: 4 additions & 2 deletions components/blog-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function BlogCard({post, onClick}) {
<p>Publié par {post.authors[0].name}</p>
<p>Le {new Date(post.published_at).toLocaleDateString('fr-FR')}</p>
</div>
<p className='preview'>{post.excerpt}</p>
<p className='preview'>{post.custom_excerpt}</p>
{onClick && post.tags && (
<div className='blog-tags-container'>
{post.tags.map(tag => (
Expand Down Expand Up @@ -111,15 +111,17 @@ BlogCard.defaultProps = {
}

BlogCard.propTypes = {
/* eslint-disable camelcase */
post: PropTypes.shape({
title: PropTypes.string.isRequired,
feature_image: PropTypes.string,
authors: PropTypes.array.isRequired,
published_at: PropTypes.string.isRequired,
excerpt: PropTypes.string.isRequired,
custom_excerpt: PropTypes.string.isRequired,
tags: PropTypes.array.isRequired,
slug: PropTypes.string.isRequired
}).isRequired,
/* eslint-enable camelcase */
onClick: PropTypes.func
}

Expand Down
27 changes: 20 additions & 7 deletions lib/blog.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@ const URL = process.env.NEXT_PUBLIC_GHOST_URL
const KEY = process.env.GHOST_KEY
const LIMIT = 9
const INCLUDE = 'authors,tags'
const FULL_URL = `${URL}/ghost/api/v3/content/posts?key=${KEY}&limit=${LIMIT}&include=${INCLUDE}`

const LIMITED_FIELDS = [
'id',
'title',
'feature_image',
'published_at',
'custom_excerpt',
'slug',
]

const FULL_URL = `${URL}/ghost/api/v3/content/posts?key=${KEY}&include=${INCLUDE}`

const options = {
method: 'GET',
Expand All @@ -18,14 +28,17 @@ function buildTagFilter(tags) {
return ''
}

function buildQuery(props) {
const tagsFilter = props?.tags ? buildTagFilter(props.tags) : ''
function buildQuery(props = {}) {
const {tags, limit = LIMIT, limitFields, fields, page} = props
const customFields = limitFields ? LIMITED_FIELDS : fields

if (props?.page && props.page.length > 0) {
return `${FULL_URL}&page=${props.page}${tagsFilter}`
}
const computedUrl = `${FULL_URL}` +
`&limit=${limit}` +
`${customFields ? `&fields=${customFields.join(',')}` : ''}` +
`${page ? `&page=${page}` : ''}` +
`${buildTagFilter(tags)}`

return `${FULL_URL}${tagsFilter}`
return computedUrl
}

export async function getPosts(props) {
Expand Down
6 changes: 4 additions & 2 deletions pages/bases-locales/temoignages/[slug].js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Post from '@/components/post'

function SlugPage({post}) {
return (
<Page title={post.title} description={post.excerpt} image={post.feature_image}>
<Page title={post.title} description={post.custom_excerpt} image={post.feature_image}>
<Head title='Témoignages sur les Bases Adresses Locales' icon={<BookOpen size={56} alt='' aria-hidden='true' />} />
<Post {...post} backLink='/bases-locales/temoignages' />
</Page>
Expand All @@ -22,11 +22,13 @@ SlugPage.defaultProps = {
}

SlugPage.propTypes = {
/* eslint-disable camelcase */
post: PropTypes.shape({
title: PropTypes.string.isRequired,
excerpt: PropTypes.string.isRequired,
custom_excerpt: PropTypes.string.isRequired,
feature_image: PropTypes.string
})
/* eslint-enable camelcase */
}

export async function getServerSideProps({query}) {
Expand Down
2 changes: 1 addition & 1 deletion pages/bases-locales/temoignages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Temoignages.propTypes = {
}

export async function getServerSideProps({query}) {
const data = await getPosts({...query, tags: 'temoignage'})
const data = await getPosts({...query, limitFields: true, tags: 'temoignage'})
return {
props: {
posts: data.posts,
Expand Down
8 changes: 5 additions & 3 deletions pages/blog/[slug].js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Post from '@/components/post'

function SlugPage({post}) {
return (
<Page title={post.title} description={post.excerpt} image={post.feature_image}>
<Page title={post.title} description={post.custom_excerpt} image={post.feature_image}>
<Head title='Le Blog de L’Adresse' icon={<BookOpen size={56} alt='' aria-hidden='true' />} />
<Post {...post} backLink='/blog' />
</Page>
Expand All @@ -22,11 +22,13 @@ SlugPage.defaultProps = {
}

SlugPage.propTypes = {
/* eslint-disable camelcase */
post: PropTypes.shape({
title: PropTypes.string.isRequired,
excerpt: PropTypes.string.isRequired,
feature_image: PropTypes.string /* eslint-disable-line camelcase */
custom_excerpt: PropTypes.string.isRequired,
feature_image: PropTypes.string
})
/* eslint-enable camelcase */
}

export async function getServerSideProps({query}) {
Expand Down
2 changes: 1 addition & 1 deletion pages/blog/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ BlogIndex.propTypes = {
}

export async function getServerSideProps({query}) {
const data = await getPosts(query)
const data = await getPosts({...query, limitFields: true})
const tags = query?.tags || null
const tagsList = await getTags()

Expand Down
4 changes: 2 additions & 2 deletions pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,12 @@ function Home({stats, posts}) {

export async function getServerSideProps() {
const stats = await getStats()
const data = await getPosts({tags: 'temoignage'})
const {posts = null} = await getPosts({tags: 'temoignage', limitFields: true, limit: 3},)

return {
props: {
stats,
posts: data?.posts || null
posts,
}
}
}
Expand Down

0 comments on commit 56d7ee1

Please sign in to comment.