Jan.2022
@AtilaFassina
They were talking about cash
We are talking about cache
@AtilaFassina
This is not a deck about money.
...or...
@AtilaFassina
@AtilaFassina
Lead Web Developer
@AtilaFassina
WHAT
WHERE
HOW
@AtilaFassina
http://website.com?i-hate-cache
@AtilaFassina
@AtilaFassina
Edge Functions
Serverless Functions
Your browser
...
@AtilaFassina
@AtilaFassina
@AtilaFassina
expiration time
when client received the Response
😬
seconds since generated
@AtilaFassina
Last-Modified
Date
last time resource changed
time response was originated
@AtilaFassina
it’s actually about revision, but that’s less cool
@AtilaFassina
for resources which change without a pattern of frequency
@AtilaFassina
@AtilaFassina
Last-Modified
Etag
Entity tag representing content
Vary
list of headers which influence response
@AtilaFassina
ETag: W/"<etag_value>"
ETag: "<etag_value>"
Weak validator
identifies specific version of response’s content
EXAMPLES
@AtilaFassina
normalise your response
to avoid splitting caches too much
⚠️
identifies headers which affect content in response
Vary: *
factors other than the request headers influence response
❌
Vary: Accept-Encoding
@AtilaFassina
Accept-Encoding: gzip,deflate
Accept-Encoding: gzip
😓
because browsers can’t agree... 🥲
@AtilaFassina
if (acceptEncoding.includes('br')) {
res.setHeader('Accept-Encoding', 'br')
} else if (acceptEncoding.includes('gzip')) {
res.setHeader('Accept-Encoding', 'gzip')
} else {
res.removeHeader('Accept-Encoding')
}
@AtilaFassina
@AtilaFassina
one header to control it all
@AtilaFassina
don’t update while fresh
Cache-Control: public, max-age=604800, immutable
powerful combo when combined with revving/cache busting for static resources
💡
common use
@AtilaFassina
Cache-Control: public, max-age=604800, must-revalidate
if can’t connect to origin server
to revalidate, can’t use resource
⚠️
common use
once stale, must validate before reuse
@AtilaFassina
Cache-Control: max-age=604800, stale-while-revalidate=86400
eliminates latency effect of
revalidation to the end-user
✨
common use
use stale while revalidating
@AtilaFassina
@AtilaFassina
import type { NextFetchEvent, NextRequest } from 'next/server'
export function middleware(req: NextRequest, ev: NextFetchEvent) {
return new Response({
headers: {
'Cache-Control': 'private, max-age=43200, must-revalidate',
}
})
}
intercepts all requests going to sibling and child routes
💡
@AtilaFassina
import type { GetStaticProps } from 'next'
export const getStaticProps: GetStaticProps = () => {
const props = getMyProps()
return {
props,
revalidate: 10
}
}
re-renders the page, consequently refetching the resource
💡
@AtilaFassina
import type { GetServerSideProps } from 'next'
export const getServerSideProps: GetServerSideProps = async (context) => {
context.res.setHeader('Cache-Control', 'max-age=600')
return {
props: {},
}
}
this method behaves differently on client-side navigation and on server-side navigation
⚠️
@AtilaFassina
import type { LoaderFunction } from 'remix'
export function loader:LoaderFunction = () => {
const responseBody = someLogic()
return new Response(JSON.stringify(responseBody), {
headers: {
'Content-Type': 'application/json',
'Cache-Control': 'private, max-age=43200, must-revalidate',
}
})
}
`loader` headers are automatically passed down to page document request too
💡
@AtilaFassina
import type { HeadersFunction } from 'remix'
export function headers:HeadersFunction = () => {
return {
'Cache-Control': 'public, max-age=10, stale-while-revalidate=86400',
}
}
so we can “fake” Incremental Static Regeneration
🤯
similar to `revalidate: 10` in Next.js
@AtilaFassina
@AtilaFassina
Come back tomorrow
'cache-control': 'private, max-age=86400, immutable'
Always check no matter what!
'cache-control': 'no-cache'
Always update, but always serve from cache
'cache-control': 'public, max-age=1, stale-while-revalidate'
@AtilaFassina
Yours for a day, maybe 2
'cache-control': 'private, max-age=86400, stale-while-revalidate=129600'
Ours for a day, maybe 2
'cache-control': 'public, max-age=86400, stale-while-revalidate=129600'
Tell me yours!
👉
@AtilaFassina