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

fix(sw): return 200-299,304 responses immediately #383

Merged
merged 3 commits into from
Sep 23, 2023

Conversation

SgtPooki
Copy link
Contributor

@SgtPooki SgtPooki commented Sep 23, 2023

fixes #380

This PR updates the caching strategy to prevent the duplicate call to the backend
if we have a cached call, and returns it immediately.

One thing to note, is that vercel-cached responses return HTTP 304 responses,
and response.ok didn't check for that. This PR also addresses this.

So, a call to the backend vercel function, that calls github does NOT happen IFF:

  1. There is a cached response
  2. That cached response has a status code of 200-299
  3. That cached response has a status code of 304;

A backend call to the backend vercel function, that calls github, WILL happen IFF:

  1. There is no cached response
  2. The cached response is a failed request (github error, vercel error, etc)
  3. The cached response was a modified result.
  4. The cached response's max-age is "too old"

@SgtPooki SgtPooki linked an issue Sep 23, 2023 that may be closed by this pull request
@vercel
Copy link

vercel bot commented Sep 23, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
starmaps ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 23, 2023 5:50am

@SgtPooki
Copy link
Contributor Author

Verified that subsequent load is almost instant:

  1. Load page with no cache (application data cleared)
  2. data pulls fully from vercel backend cache (cached from previous attempts, so faster than fully from github, but still takes longer than when it's cached in the sw for HUGE roadmaps)
  3. reload page
  4. page loads almost instantaneously

Verified that cache is invalidated for updated github items (description updates):

  1. load page
  2. edit github issue
  3. reload page (not clearing cache.. ) === github issue is not updated in starmap, but a popup appears showing new data exists
  4. reload page === github issue that was stale is updated in starmap, but the rest loads very quickly
  5. reload page === latest github issue data (which is cached in SW) loads instantly for everything

@whizzzkid @momack2 please review. Preview urls for the urls Molly gave in slack are:

@SgtPooki SgtPooki marked this pull request as ready for review September 23, 2023 06:07
Copy link
Collaborator

@whizzzkid whizzzkid left a comment

Choose a reason for hiding this comment

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

Ok can confirm it's a bit faster, thanks @SgtPooki

if (!cachedResponse) {
return false
}
if (!cachedResponse.ok || cachedResponse.status === 304) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can merge the two if statements.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yea, i was just trying to keep them separate and explicit for clarity

@whizzzkid whizzzkid merged commit b4737f5 into main Sep 23, 2023
3 checks passed
@whizzzkid whizzzkid deleted the 380-feat-improve-caching-experience branch September 23, 2023 19:24
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.

feat: improve caching experience
2 participants