Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding netlify.toml to add caching & hint headers #112

Merged
merged 16 commits into from
Aug 21, 2020
Merged

Adding netlify.toml to add caching & hint headers #112

merged 16 commits into from
Aug 21, 2020

Conversation

MikeRogers0
Copy link
Contributor

@MikeRogers0 MikeRogers0 commented Jul 23, 2020

This is a 🔦 documentation change.

Summary

#109 Pulled from here. Originally I thought it could be neat to add lots of Early Hints headers, but I reduced it down a bunch to keep things easy to review.

This adds:

  1. Caching to the files being served, so static assets will be cached for a long time
  2. Early hints on the homepage for CSS/JS.

🎥 Here is a video of the difference

Context

#103

If you liked this PR, please consider buying me a coffee :)

echo "CSS Path: ${CSS_PATH}"
echo "JS Path: ${JS_PATH}"

sed -i s,CSS_PATH,${CSS_PATH},g netlify.toml
Copy link
Contributor Author

@MikeRogers0 MikeRogers0 Jul 23, 2020

Choose a reason for hiding this comment

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

What this'll do is update our netflify.toml after build with the CSS/JS paths. So we can tell the browser in the HTTP header which files to expect.

I'm happy to pull this out if you're not a fan.

[build.processing.html]
pretty_urls = true
[build.processing.images]
compress = false
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could probably remove these build rules if you'd like, but I do kind of like being explicit as to what processing we're doing :)

X-Content-Type-Options = "nosniff"
Strict-Transport-Security = "max-age=15552000; includeSubDomains"
Referrer-Policy = "no-referrer-when-downgrade"
Cache-Control = "public, max-age=604800, s-max-age=604800"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The cache for most files is 7 days. Which I think is about right for the bridgetown update cycle.

"<CSS_PATH>; rel=preload; as=style",
"<JS_PATH>; rel=preload; as=script",
"<https://NETLIFY_IMAGES_CDN_DOMAIN>; rel=preconnect"
]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

When a user visits the homepage they'll get the HTTP hint to download the CSS/JS & preconnect to the CDN.

[[headers]]
for = "/*.(png|jpg|js|css|svg|woff|ttf|eot|ico|woff2)"
[headers.values]
Cache-Control = "public, max-age=31536000, s-max-age=31536000"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Things like fonts/images aren't likely to change, so 1 year is an acceptable expiry time.

@@ -9,13 +9,12 @@
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">

<link rel="preload" href="{% webpack_path css %}" as="style" />
<link rel="stylesheet" href="{% webpack_path css %}" />
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 moved these about a little :)

Requesting the preloading the stylesheet, then requesting it right after seemed to not make a big performance boost. I also moved the future page prefetches below the JS so that'll start downloading before we worry about the next pages.

@@ -0,0 +1,40 @@
[build]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Weirdly if you have a different base, you need two netlify.tml files -.- Took me so long to figure out why the headers weren't being picked up on.

@MikeRogers0 MikeRogers0 changed the title Adding netlify.toml to add caching headers Adding netlify.toml to add caching & hint headers Jul 23, 2020
@MikeRogers0 MikeRogers0 marked this pull request as ready for review July 23, 2020 20:01
@jaredcwhite
Copy link
Member

@MikeRogers0 I'm sorry I haven't gotten a chance to review this yet. Will do ASAP!

When experimenting with review apps on Netlify, I found I'd quite often needed these variables set to have reliable deploys.
@MikeRogers0
Copy link
Contributor Author

Deploy error is:

10:26:25 AM: error An unexpected error occurred: "https://registry.yarnpkg.com/@swup/plugin/-/plugin-1.0.1.tgz: Request failed "404 Not Found"".

I'll come back to this later :)

@MikeRogers0
Copy link
Contributor Author

@jaredcwhite Am I ok to close this?

@jaredcwhite
Copy link
Member

@MikeRogers0 Deploy preview feels very snappy! I still don't quite grok all the config stuff, but since it works, let's go for it.

@jaredcwhite jaredcwhite merged commit 8607f70 into bridgetownrb:main Aug 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants