Next.js Discord

Discord Forum

build large page

Answered
New Guinea Freshwater Crocodile posted this in #help-forum
Open in Discord
New Guinea Freshwater CrocodileOP
I can see that warning on my build

Warning: data for page "/****" is 81.4 MB which exceeds the threshold of 128 kB, this amount of data can reduce performance.


Of course 81MB is very large data but how can I get rid of it? If i get all data on client my SEO say goodbye 😅 so with large data coming from CMS do I have to filter all my data or is there some tips that i may don't know Thank you for your clarifications, insights, advices. I'm listening carefuly. 🙂
Answered by French Spaniel
I think the deep nesting may be what's causing such a big page size since you are doing wildcards to fetch everything, can you simplify the queries to grab exactly what you need
View full answer

97 Replies

European sprat
how on earth do you have a page that is 81MB?
French Spaniel
Gotta be images
To break this up you can paginate data, and don't bundle images into your build
New Guinea Freshwater CrocodileOP
Hmmm what do you mean by paginate? and how do i get rid of images? You mean i filter all images and get em on client?
BTW all the data comes from Directus wich is an headless CMS and yes many images ^^
I'm pretty noob to builds and rendering strategies, so feel free to explain as if I were a child.
😅
@New Guinea Freshwater Crocodile Hmmm what do you mean by paginate? and how do i get rid of images? You mean i filter all images and get em on client?
French Spaniel
Split the data into multiple pages, but this probably unnecessary since its probably all the images that are causing this massive page size

I use directus as well, how are you getting the images from directus?
New Guinea Freshwater CrocodileOP
I use the id on Image component and have a function getAsset that concat my baseUrl with /assets/${id)
but as for the get of images I use the SDK basicaly
French Spaniel
Could I see this getAsset function?
New Guinea Freshwater CrocodileOP
of course but I don't think this the problem ^^
import getConfig from "next/config";

const { publicRuntimeConfig } = getConfig();

const { url } = publicRuntimeConfig;

const getAsset = (id: string) => {
  return `${url}/assets/${id}`;
};

export default getAsset;
because the problem is probably as you said the fetching
I use reactQuery i thought it was a good idea but maybe that's the problem?
French Spaniel
No reactQuery is not the problem, it's like the actual image data is in your build
New Guinea Freshwater CrocodileOP
It's quite "bizzare"
French Spaniel
This page that is 81.4 mb, how many records are we talking about? Like is this a page showing 1000s and 1000s of things?
New Guinea Freshwater CrocodileOP
no we even don't have 1000 items ^^
but this is on a getStaticpath i think
gotta check it out
No it's a single page
seems to be this one
at the build time
because i use getStaticProps maybe?
on this project i'm not yet on the latest version
do you think server side is better?
French Spaniel
No you definitely want to pre build if you can like you are doign with getStaticPaths and getStaticProps that's how I do it in all my projects
It's much faster that way for the user since you are serving a static page
New Guinea Freshwater CrocodileOP
yes, that's what I thought.
French Spaniel
Is your directus instance served somewhere? How are you exposing these images? Like can I get https://co.webincompany.fr/assets/image1
New Guinea Freshwater CrocodileOP
French Spaniel
Oh let me see your getStaticPaths and getStaticProps
New Guinea Freshwater CrocodileOP
btw here is a getStaticprop

export const getStaticProps = async () => {
  const queryClient = new QueryClient();

  const menu = await findMenu("1");
  const infos = await findInfos();

  await queryClient.prefetchQuery({
    queryKey: ["page"],
    queryFn: () => findFacilities(),
  });

  await queryClient.prefetchQuery({
    queryKey: ["facilities"],
    queryFn: () => getAllFacilities(),
  });

  await queryClient.prefetchQuery({
    queryKey: ["facilityTypes"],
    queryFn: () => getAllFacilitiyTypes(),
  });

  return {
    props: { dehydratedState: dehydrate(queryClient), menu, infos },
    revalidate: 3,
  };
};
yes same time ^^
French Spaniel
Do one of these queryFn make a call to getAsset down the stack?
New Guinea Freshwater CrocodileOP
no getAsset is always on my client
in my next Image component src
like so :
<Image
  src={getAsset(slide.directus_files_id?.id)}
  alt={slide.directus_files_id?.description ?? ""}
  fill
  sizes="(min-width: 180ch) 50vw, 90vw"
  priority={index === 0 ? true : false}
  />
maybe I should get rid of reactquery especially on getStaticprops function ? no?
French Spaniel
You could give that a try, I've never use react query in that way. What is the goal with it there?
New Guinea Freshwater CrocodileOP
I wanted to cache it all without having to call it again on the client side but this is something either difficult or I'm not good at it ^^
French Spaniel
I have limited experience with react query, you might be able to do it this way and pass the initial data.
https://tanstack.com/query/v4/docs/react/guides/initial-query-data

A couple things I would try in isolation
- Run the build with the images set to an empty string so we can be sure it is the images causing it
- Try removing the react query from getStaticProps
New Guinea Freshwater CrocodileOP
yes the dehydrated state is here for that
Anyway, I'm going to refactor all this with version 13 and the new directus sdk in the near future.
I am very used to pages router
New Guinea Freshwater CrocodileOP
Yeah I was too, but i work on some testing projects and now I understand quite well I think. even though I still don't understand the new dynamic routing
In any case next 13 looks really cool and powerful
do you know the way to get static page on it?
because for now i only understant the cool server side component and client component
@New Guinea Freshwater Crocodile do you know the way to get static page on it?
French Spaniel
Nope, lot of other people in this discord that know app router quite well
New Guinea Freshwater CrocodileOP
Sure I may ask in the future
So no idea about why this 81mb ? 😅
New Guinea Freshwater CrocodileOP
Does a route weight on builds are for one page or for all the children?
because my 81Mb have one page and six folders of two page each
In the end of the build i have something like that much lighter
@New Guinea Freshwater Crocodile In the end of the build i have something like that much lighter
French Spaniel
These numbers look much better, where was that 81 mbs coming from?
New Guinea Freshwater CrocodileOP
it's during the build I have a warning told me there's a threshold
 which exceeds the threshold of 128 kB, this amount of data can reduce performance.
French Spaniel
Could you screenshot the full build output where it says that, I want to see the route its telling you
New Guinea Freshwater CrocodileOP
I just realize it's also in the log of dev
but it's quite a small page
and getting rid of react query has changed nothing you were right
maybe it's because of my deep nested fetch function
French Spaniel
Something on this page definitely needs to be fixed, 5.61s until full page load
New Guinea Freshwater CrocodileOP
Yes clearly
do you use sdk with directus?
French Spaniel
Can I see these queryFns?

await queryClient.prefetchQuery({
queryKey: ["page"],
queryFn: () => findFacilities(),
});

await queryClient.prefetchQuery({
queryKey: ["facilities"],
queryFn: () => getAllFacilities(),
});

await queryClient.prefetchQuery({
queryKey: ["facilityTypes"],
queryFn: () => getAllFacilitiyTypes(),
});
@New Guinea Freshwater Crocodile do you use sdk with directus?
French Spaniel
No, I serve my images through cloudfront on AWS
I just usually directus purely as a way to manage my content so I don't have to build a dashboard
New Guinea Freshwater CrocodileOP
export const findFacilities = async (): Promise<IPage> => {
  return await directus
    .items("pages")
    .readOne("9911ded7-f532-4f54-ad2b-17c53a2c7f95", {
      fields: ["*.*", "blocks.*", "blocks.item.*.*.*.*", "*.collection"],
    })
    .then((response) => response)
    .catch((error) => error);
};
export const getAllFacilities = async (): Promise<IFacility[]> => {
  return directus
    .items("facilities")
    .readByQuery({ limit: -1, fields: ["*.*.*.*.*.*"] })
    .then((response) => response.data)
    .catch((error) => error);
};
export const getAllFacilitiyTypes = async (): Promise<IFacilityType[]> => {
  return directus
    .items("facility_type")
    .readByQuery({ limit: -1, fields: ["*.*.*"] })
    .then((response) => response.data)
    .catch((error) => error);
};
if you're not familiar with the sdk all the * are a query depth level
I think this could be this one
French Spaniel
Your esentially grabbing everything
This is a little new to me, but I think next step would be somehow figuring out the size of the data returned by these queries
New Guinea Freshwater CrocodileOP
It seems that is 81Mb ^^
French Spaniel
Haha yes, but we need to figure out which one it is, and what fields have all this data in it
Or if a bunch of duplicate data is being grabbed for no reason
New Guinea Freshwater CrocodileOP
The problem is that I need certain data within a relationship, for example where there's an id in a database, Directus sends me the complete data instead if I give it a certain level of depth.
So if I need a piece of data within a piece of data within a piece of data, you know what I mean, that means a lot of data ^^ I think it's a complicated job to find out what data is needed and what isn't. It's a lot of work.
New Guinea Freshwater CrocodileOP
yes
It's the problem right?
French Spaniel
I think the deep nesting may be what's causing such a big page size since you are doing wildcards to fetch everything, can you simplify the queries to grab exactly what you need
Answer
New Guinea Freshwater CrocodileOP
Ok this is a problem I was almost pretending not to see but I understand now. I've got a long job ahead of me
Thank you for your time and your help ^^
French Spaniel
No problem!
New Guinea Freshwater CrocodileOP
I'll save you the search with me. I will try on my own 😅
Have a great day or evening wherever you are ^^