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

feat: support staleMaxAge: -1 to always respond stale value #857

Merged
merged 8 commits into from
Jan 20, 2023

Conversation

yassilah
Copy link
Contributor

πŸ”— Linked issue

Resolves #856

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@codecov
Copy link

codecov bot commented Jan 20, 2023

Codecov Report

Merging #857 (511ad87) into main (686a560) will decrease coverage by 0.25%.
The diff coverage is n/a.

❗ Current head 511ad87 differs from pull request most recent head 258249e. Consider uploading reports for the commit 258249e to get more accurate results

@@            Coverage Diff             @@
##             main     #857      +/-   ##
==========================================
- Coverage   67.34%   67.09%   -0.25%     
==========================================
  Files          58       59       +1     
  Lines        5748     5772      +24     
  Branches      611      612       +1     
==========================================
+ Hits         3871     3873       +2     
- Misses       1869     1891      +22     
  Partials        8        8              
Impacted Files Coverage Ξ”
src/presets/index.ts 100.00% <0.00%> (ΓΈ)
src/presets/edgio.ts 7.40% <0.00%> (ΓΈ)
src/presets/layer0.ts 100.00% <0.00%> (+90.69%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

entry.integrity = undefined;
entry.mtime = undefined;
entry.expires = undefined;
if (entry.value && !opts.allowStale) {
Copy link
Member

Choose a reason for hiding this comment

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

Does it makes sense that we directly use !opts.swr here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You mean instead of entry.value && !opts.allowStale?

Copy link
Member

Choose a reason for hiding this comment

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

Yes. I suppose it should be expected behavior of SWR to be nonblocking after maxAge. (To be precise, also before staleMaxAge but it is not implemented yet in handler)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well tbh I thought so too, but i'm not knowledgeable enough on SWR expected behaviour to answer that πŸ˜…
However, the idea of allowStale is to be used in combination with swr because the browser will always send a request after maxAge is invalid but if allowStale is true then the server may respond with outdated data until the background task is finished; by default, the server should wait for the updated data to be fetched before sending a response. Does that make sense or am I completely wrong?

Copy link
Member

Choose a reason for hiding this comment

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

You are definitely not wrong. We use cache options for both Browser<>Server And Server<>Handler caching strategy. And these utils handle Server<>Handler SWR caching strategy similar to browsers (regardless of HTTP client which could be another API or browser)


SWR Cache Timeline: 0....maxAge (A) .... maxAge...staleMaxAge (B) ... after maxAge/staleMaxAge (C)

Stage A: Fresh Cache. Use value without revalidate
Stage B: Stale Cache. Respond and revalidate in background
Stage C: Expired Cache. Block and revalidate

Since we didn't implement staleMaxAge yet, it is either B or C. New allowStale flag is identical to staleMaxAge: Infinity.

How about supporting staleMaxAge: -1 as a way to support without new flag?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright, yes I do see what you mean and it would make sense to use the existing staleMaxAge flag to avoid multiplying the options; i'll update the PR

src/runtime/cache.ts Outdated Show resolved Hide resolved
Co-authored-by: pooya parsa <pyapar@gmail.com>
src/runtime/cache.ts Outdated Show resolved Hide resolved
@pi0 pi0 changed the title feat: add allowStale option default to false feat: support staleMaxAge: -1 to always respond stale value Jan 20, 2023
@pi0 pi0 merged commit 456f82a into nitrojs:main Jan 20, 2023
@pi0
Copy link
Member

pi0 commented Jan 20, 2023

Thanks!

@yassilah yassilah deleted the feat-no-stale-option branch January 20, 2023 14:31
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.

[feature]: add a no-stale option to allow sending stale value
2 participants