netlify / gatsby-plugin-netlify

Gatsby plugin. Automatically generates a _headers file and a _redirects file at the root of the public folder to configure HTTP headers and redirects on Netlify.
MIT License
13 stars 15 forks source link

Include html, page-data, and app-data cache control headers #270

Open MarkBennett opened 10 months ago

MarkBennett commented 10 months ago

A configuration issue with Netlify last week caused our Gatsby application to fail rehyrdation as some of the data on our site was using old cached versions.

We eventually traced this back to Netlify serving Cache-Control: public, max-age=31536000, immutable headers for html, page-data, and app-data.json on our site. The sw.js was not affected as it's included in the headers defined by this plugin.

In order to avoid future configuration changes impacting Gatsby content caching, our team would like to propose including the html, page-data and app-data cache headers recommended by Gatsby in their Caching Static Site documentation be included in the headers generated by this plugin, so that future Netlify configuration changes don't affect Gatsby sites like ours was.

Happy to contribute a PR but wanted to reach out to understand the appetite and if there are issues we're not aware of.

We can confirm that even the latest version of this plugin does not generate the cache control headers recommended in the Gatsby docs. I've included a sample generated using gatsby-plugin-netlify v5.1.0:

_headers
## Created with gatsby-plugin-netlify

/*
  X-Frame-Options: DENY
  X-XSS-Protection: 1; mode=block
  X-Content-Type-Options: nosniff
  Referrer-Policy: same-origin
/ab6a98a71453a026750777ae12b866740d51c59e-71ee8720a319de7ac17e.js
  Cache-Control: public, max-age=31536000, immutable
/7eb4c100dee79c56167684d582e3ac144cd46a3e-f919e87eb7710a87d499.js
  Cache-Control: public, max-age=31536000, immutable
/89b6f169c4647dfa712d4e801bc106a9f8c506bf-b35c01750755e854fcf7.js
  Cache-Control: public, max-age=31536000, immutable
/e1b89a0988750174861d123c5fb950ea7b744e3e-f3a11288929d1cf21055.js
  Cache-Control: public, max-age=31536000, immutable
/82cd71fbb9a3ea58d6d59e7d7d86a1158b6e29e6-e342c848a1d51b56a8ba.js
  Cache-Control: public, max-age=31536000, immutable
/0726132db507547d55dbceb3c36e2a5053e1fd5b-839bb01a19b72e836532.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-templates-sale-tsx-2e2f1774b2b19d1c9230.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-templates-sale-reminder-tsx-3453931d804bf385d5e3.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-templates-parcel-tsx-d86f9ec0359328ee8377.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-templates-option-tsx-718ac2a07d9320334b49.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-templates-sales-showcase-tsx-4aa70c1a17a7cdae9af6.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-pages-404-tsx-d2342f3549ee55ca5c58.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-pages-contact-tsx-b8b33b84891791dae15d.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-pages-error-test-tsx-70e040286df4ab552131.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-pages-index-tsx-9920bec17e7705f541a8.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-pages-past-sales-tsx-d16f8b22f68488343c36.js
  Cache-Control: public, max-age=31536000, immutable
/2c796e83-30a77c82afdb4fcb8566.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-pages-sales-tsx-1c36cd45c830b04f0a59.js
  Cache-Control: public, max-age=31536000, immutable
/c736391e93f4881c54ad51fa3803623538d0d48a-8e7c5432a2b6b51059ed.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-pages-sell-with-clhbid-tsx-3f340d16ce1b4b0fad2b.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-pages-testimonial-video-tsx-863a06578ebc3f5af0dd.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-pages-about-clhbid-frequently-asked-questions-tsx-86270d000505f77ba29b.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-pages-about-clhbid-index-mdx-7250380cc03ed2d1ad27.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-pages-about-clhbid-clhbid-team-alle-carter-mdx-470389f7ec4f789ada4c.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-pages-about-clhbid-clhbid-team-amos-wiebe-mdx-9b39f472cc598647ba6a.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-pages-about-clhbid-clhbid-team-bridget-hennigar-mdx-da88eefe8f78751d16d4.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-pages-about-clhbid-clhbid-team-corry-stark-mdx-a0730869bb702307fa99.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-pages-about-clhbid-clhbid-team-index-tsx-67b768b8f1fb3dc36d9c.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-pages-about-clhbid-clhbid-team-michael-reeve-mdx-c0b564c13eb68a0b4ad6.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-pages-about-clhbid-clhbid-team-roy-carter-mdx-7da6da562680317a18c0.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-pages-about-clhbid-clhbid-team-tyler-ruttan-mdx-ea2adaca4d6f019ac6e2.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-pages-why-clhbid-clhbid-testimonials-tsx-9f18aa9b2c498de8d320.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-pages-why-clhbid-clhbid-vs-listing-with-professional-realtors-mdx-2e7038653c5fedde5f29.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-pages-why-clhbid-clhbid-vs-private-sale-mdx-83883ecc77f8ca1f69e7.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-pages-why-clhbid-clhbid-vs-private-tender-mdx-225a322086c383524e39.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-pages-why-clhbid-clhbid-vs-public-auction-mdx-7795dedb16b8bb75c79e.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-pages-why-clhbid-index-mdx-6130a0e68d9bdb2120d0.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-pages-how-clhbid-works-bidding-on-en-bloc-properties-on-clhbid-mdx-5c4ba888cc06464dcd29.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-pages-how-clhbid-works-bidding-on-properties-on-clhbid-mdx-cef6ba404741ed95369a.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-pages-how-clhbid-works-clhbid-bidding-terms-and-conditions-mdx-7d571f046b65d548cc73.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-pages-how-clhbid-works-clhbid-transaction-fee-mdx-8b74c1e330ded034f56d.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-pages-how-clhbid-works-closing-the-sale-post-auction-mdx-adfc36e66b55b2d7fbb3.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-pages-how-clhbid-works-index-mdx-dab69074f10f8500b17f.js
  Cache-Control: public, max-age=31536000, immutable
/component---src-pages-how-clhbid-works-starting-bids-on-the-clhbid-auction-platform-mdx-04984812350845ac1ba4.js
  Cache-Control: public, max-age=31536000, immutable
/webpack-runtime-9314ffc0b0c86e7b185a.js
  Cache-Control: public, max-age=31536000, immutable
/framework-e70a358444443443528f.js
  Cache-Control: public, max-age=31536000, immutable
/styles.4870b0a1a138f4d0f9c4.css
  Cache-Control: public, max-age=31536000, immutable
/252f366e-503fbe2ac35377d43d28.js
  Cache-Control: public, max-age=31536000, immutable
/d64684d8-09abb2d34b864fa0acd5.js
  Cache-Control: public, max-age=31536000, immutable
/1bfc9850-ad6b973ca72ec96ff978.js
  Cache-Control: public, max-age=31536000, immutable
/b3a13d7d-6d1b610e7357abdef50f.js
  Cache-Control: public, max-age=31536000, immutable
/app-705104588cbed2c984a1.js
  Cache-Control: public, max-age=31536000, immutable
/static/*
  Cache-Control: public, max-age=31536000, immutable
/sw.js
  Cache-Control: no-cache

MarkBennett commented 10 months ago

For anyone else seeing this, we found that adding the following to our gatsby-config.ts ensured that our html and page-data had the correct cache control, even when Netlify configuration changed.

    {
      resolve: `gatsby-plugin-netlify`,
      options: {
        // Apply recommended Gatsby caching headers to Netlify's CDN
        // See: https://www.gatsbyjs.com/docs/how-to/previews-deploys-hosting/caching/
        headers: {
          "/*.html": ["Cache-Control: public, max-age=0, must-revalidate"],
          "/page-data/*": ["Cache-Control: public, max-age=0, must-revalidate"],
        },
      },
    },