sdorra / content-collections

Transform your content into type-safe data collections
https://content-collections.dev
MIT License
424 stars 18 forks source link

Next.js bundle size doubles when used in `sitemap.ts` or `manifest.ts` #315

Open wottpal opened 1 day ago

wottpal commented 1 day ago

Disclaimer¹: Sorry for opening all the issues recently, I'm a big fan and just want content-collections to become as good as possible! Disclaimer²: This issue might be Next.js (or even only Next.js v15 related). I'm happy to open a ticket there after hearing your thoughts.


When importing allPages from content-collection in either manifest.ts or sitemap.ts (the latter is quite common to build a sitemap of your content), the analyzed bundle size* doubles in it's size because the generated .json is somehow included twice and not deduped correctly. In big projects, where this .json file can grow up to multiple Megabytes, this becomes quite noticeable and could potentially reduce SSR/SSG speeds.

*I'm speaking of the Node.js bundle size (the environment where the sitemap but also pages are generated). Next.js differentiates between Node.js|Edge|Client bundle sizes.

Reproduction

You can reproduce it with the same boilerplate project I shared in #309 as shown below. (Btw. if there are more potential contributors, I'm happy to set this to public or invite them as well).

# Install dependencies
bun install

# Create empty environment
cp .env.local.example .env.local

# Here you might wan to increase the size of `content/de/blog/0001-hello-world.mdx` a bit, that it becomes more noticable.

# Analyze build
bun run analyze

# Delete `src/app/sitemap.ts` & `src/app/manifest.ts` and analyze build again to see Node.js bundle having half of the size.

Sample screenshots

Bundle without src/app/sitemap.ts & src/app/manifest.ts: bundle-without-sitemap-manifest

Bundle with src/app/sitemap.ts & src/app/manifest.ts: bundle-with-sitemap-manifest

sdorra commented 15 hours ago

Disclaimer¹: Sorry for opening all the issues recently, I'm a big fan and just want content-collections to become as good as possible!

No problem; I am grateful when a problem is found and reported.

I'm not sure what i can do when next.js includes content collections twice, but i don't think that this is a problem in the most cases. Its hard to tell which entry point uses which bundle in a next.js application. I think it only becomes a problem if it is in the client bundle. In the case of sitemap and manifest is mostly no problem, because it is generated static at build time.