I have an CLS issue with this landing site. The landing site is statically generated in Next.js(SSG) as seen on this photo /
.
I must be missing something regarding Next.js SSG. When I open the generated index.html
file, not all HTML content is present. When page loads it looks like Next.js is injecting these missing sections on the page load... Shouldn't the entire HTML be statically generated and available in the index.html site? What am I missing or not understanding here?
Here is the data fetching for the page:
import Head from "next/head"
import {
Box,
Center,
Container,
Heading,
Tag,
} from "@chakra-ui/react";
import { request } from "/lib/datocms";
import {
MAIN_MENU_QUERY,
BLOG_POSTS_QUERY,
BLOG_CATEGORIES_QUERY,
HOME_PAGE_QUERY,
} from "/lib/queries";
import BlockRender from "/components/blocks/BlockRender";
import ChakraNextLink from "/components/atoms/ChakraNextLink";
import BlogGrid from "/components/blog/BlogGrid"
import { renderMetaTags } from "react-datocms";
...
export const getStaticProps = async ({ preview }) => {
const graphqlRequest = {
query: MAIN_MENU_QUERY,
preview
}
const data = await request(graphqlRequest);
const homePageGraphqlRequest = {
query: HOME_PAGE_QUERY,
preview
}
const homePage = await request(homePageGraphqlRequest);
const pageData = homePage?.homePage;
const blogPostsGraphqlRequest = {
query: BLOG_POSTS_QUERY,
preview,
variables: {
count: homePage.homePage.blogPostCount ?? 0
}
}
const blogPosts = await request(blogPostsGraphqlRequest);
const blogCategoriesGraphqlRequest = {
query: BLOG_CATEGORIES_QUERY,
preview
}
const blogCategories = await request(blogCategoriesGraphqlRequest);
return {
props: {
data,
pageData,
blogPosts,
blogCategories
},
};
};
Here is the code of my index page:
export default function Home({ data, pageData, blogPosts, blogCategories }) {
const metaTags = pageData.seo.concat(data.site.favicon);
return (
<>
<Head>
{renderMetaTags(metaTags)}
</Head>
# all this content is not in the generated Index.html file and causes CLS issue
{pageData.content &&
pageData.content.map((block, key) => <BlockRender key={key} block={block} />)
}
# this is in the generated index.html file
<Box w="full" py="12">
{pageData.showBlogSection && (
<BlogSection posts={blogPosts} categories={blogCategories}>
)}
</Box>
</>
)
}
The problem was in the component that dynamically loads components for data from my backend headless CMS. I changed this:
into this:
The CLS droped from
0.85
to0.1
.I hope this answers helps someone with similar issue.