Skip to content

Conversation

@Fireentity
Copy link
Contributor

Description
I've disabled Netlify's default caching (mergeCachingHeaders: false) to implement a more granular policy. This change sets a long-term (1-year, immutable) cache for all hashed static assets (JS, CSS, images, fonts). Conversely, HTML and Gatsby's page-data files are now set to must-revalidate to ensure users always receive fresh content.

This approach aligns with caching best practices. As part of this, I also moved the global security headers (CSP, X-Frame-Options) to the correct allPageHeaders setting.

References:

https://developer.chrome.com/docs/lighthouse/performance/uses-long-cache-ttl
https://jakearchibald.com/2016/caching-best-practices/
https://simonhearne.com/2022/caching-header-best-practices/

This PR fixes #6930

Notes for Reviewers
I'm addressing other open issues in separate PRs. Since this one affects caching for all pages, I felt it was best to keep it focused.

Signed commits

  • Yes, I signed my commits.

Disables Netlify's default caching headers (mergeCachingHeaders: false) to implement a more granular and performant caching strategy. This change sets long-term immutable caching (1 year) for static assets like JS, CSS, fonts, and images.

At the same time, it ensues HTML and page-data files are not cached (must-revalidate) to prevent stale content. Finally, the security headers (X-Frame-Options, CSP) are moved to the 'allPageHeaders' option for proper global application.

Signed-off-by: Lorenzo Croce <lorenzo.croce@arenastudios.it>
@l5io
Copy link
Contributor

l5io commented Nov 10, 2025

🚀 Preview for commit 40d969d at: https://6911d9ea85ef234e69c41743--layer5.netlify.app

Copy link
Member

@leecalcote leecalcote left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for evaluating the site's performance in this way, @Fireentity. 👍

"/*.js": [
"Cache-Control: public, max-age=31536000, immutable",
],
"/*.css": [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surely, we change our CSS many times within the span of a year and will find ourselves in trouble if we don't age this content much more aggressively.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've looked into this further, and while Gatsby's content hashing technically prevents broken styles even with a 1-year cache, a shorter duration is indeed better to avoid "cache bloating" in users' browsers given our frequent updates. I propose lowering the max-age to 30 days. This seems to be the ideal sweet spot because it is the minimum duration required to pass Google PageSpeed Insight's "Serve static assets with an efficient cache policy" audit without warnings, while still keeping the cache size manageable compared to the 1-year default.

Here are the references supporting this approach, including the NitroPack guide and Google's documentation:
nitro
screamingfrog
developer.chrome

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 05901ad

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifically, I have configured
HTML and page-data to always revalidate (max-age=0) so users immediately see new deployments
CSS and JavaScript files are now set to 30 days (max-age=2592000) to prevent cache bloat.
Fonts and images remain at 1 year (max-age=31536000) since these assets rarely change

Copy link
Member

@leecalcote leecalcote left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More thought needs to be given to the extended caching duration proposed for some types of content.

The font file section is good to go. The rest is an open question that needs to be justified for each type of content.

@Rajesh-Nagarajan-11
Copy link
Member

Rajesh-Nagarajan-11 commented Nov 16, 2025

Thank you for your contribution!
Let's discuss this during the website call tomorrow ( 17/11/2025 ) at 6:30 PM IST | 8 AM CT

Add it as an agenda item to the meeting minutes, if you would 🙂

@l5io
Copy link
Contributor

l5io commented Nov 18, 2025

🚀 Preview for commit 057b1ac at: https://691bd56d25e7d35a91a0c64c--layer5.netlify.app

@Fireentity
Copy link
Contributor Author

@Rajesh-Nagarajan-11 I'm freeing up some time to participate in the next session

Update gatsby-config.js to lower the Cache-Control max-age from 1 year to 30 days (2592000 seconds) for .js and .css assets.

Signed-off-by: Lorenzo Croce <lorenzo.croce@arenastudios.it>
@l5io
Copy link
Contributor

l5io commented Nov 21, 2025

🚀 Preview for commit 05901ad at: https://6920b5074693ba2542c24e7b--layer5.netlify.app

Update Cache-Control headers for the /static/ folder to must-revalidate. Since files in this directory do not use content hashing, long-term caching prevented users from seeing immediate updates.

Signed-off-by: Lorenzo Croce <lorenzo.croce@arenastudios.it>
@l5io
Copy link
Contributor

l5io commented Nov 23, 2025

🚀 Preview for commit 4a1b5ef at: https://69238de481038ad905b0727a--layer5.netlify.app

@l5io
Copy link
Contributor

l5io commented Dec 1, 2025

🚀 Preview for commit ad28e0b at: https://692d443c290bfdacd26849fb--layer5.netlify.app

@l5io
Copy link
Contributor

l5io commented Dec 1, 2025

🚀 Preview for commit bc65c22 at: https://692d521cd8a8d81349e20366--layer5.netlify.app

@Fireentity
Copy link
Contributor Author

I believe the PR is ready now. Regarding the cache settings, static files are not post-processed and do not carry content hashes in their filenames, so they require a max-age=0 directive to ensure the browser does not serve stale content unless the filename is manually changed. For JS and CSS files, I have set the duration to 30 days because they are updated frequently; this prevents cache bloat while still meeting the minimum caching requirement. Finally, since assets like SVGs and fonts change very rarely, a one-year cache duration is appropriate for them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ Performance ] Improve Blog page Performance

4 participants