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

Add missing max-age cache directive #114

Merged
merged 5 commits into from
Nov 2, 2023
Merged

Conversation

ulrikandersen
Copy link
Contributor

@ulrikandersen ulrikandersen commented Nov 1, 2023

Adds the max-age directive to the cache control header which is going to tell the browser for how long it should cache the image for.

Max age is set to 1 hour to allow us to change images without waiting for 30 days for users' browser caches to expire.

@simonbs
Copy link
Contributor

simonbs commented Nov 1, 2023

@ulrikandersen Do you know how this will work? Will the image still be cached beyond one hour in which case the browser shows a stale image while revalidating or does it only apply the SWR logic within that hour?

I'm thinking we could cache icons basically infinitely as long as we refresh the image while showing a stale image.

@ulrikandersen
Copy link
Contributor Author

ulrikandersen commented Nov 1, 2023

@ulrikandersen Do you know how this will work? Will the image still be cached beyond one hour in which case the browser shows a stale image while revalidating or does it only apply the SWR logic within that hour?

I'm thinking we could cache icons basically infinitely as long as we refresh the image while showing a stale image.

I am actually not sure how widely supported the stale-while-revalidate header is 🤔 Can I Use?
Perhaps it is mainly something supported by Next's SWR and not for normal browser requests. If this is tru we should only use max-age and choose it according to how long we can accept an old image to be shown. Alternatively we could look into making the image name change when the actual image changes in the repo.

@simonbs
Copy link
Contributor

simonbs commented Nov 1, 2023

Alternatively we could look into making the image name change when the actual image changes in the repo.

I think we can quite easily do this, especially if query parameters in a URL are taken into consideration when browsers cache images, but I suppose they are.

I'm pretty sure we can get the ID of the commit in which a file was updated through the one request we do to GraphQL. If that's the case then we can just add that as a query parameter to the URL we use for loading the image. That way the URL will change when a new commit is made.

@simonbs
Copy link
Contributor

simonbs commented Nov 2, 2023

With the changes in #115 the image URLs will change for every commit to a branch.

@ulrikandersen #115 targets this PR so we can test it out if you think this will allow us to cache images more reliably.

@ulrikandersen
Copy link
Contributor Author

With image URLs changing when files change we are safe to instruct browser to cache for a long time (30 days).

I have removed stale-while-revalidate as it will have no effect (and most browsers don't support it).

@ulrikandersen ulrikandersen enabled auto-merge (squash) November 2, 2023 07:04
@ulrikandersen ulrikandersen requested review from simonbs and removed request for simonbs and nicolas-vancea-shape November 2, 2023 07:05
@ulrikandersen ulrikandersen merged commit 183e520 into develop Nov 2, 2023
3 checks passed
@ulrikandersen ulrikandersen deleted the fix/image-cache-header branch November 2, 2023 08:43
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.

3 participants