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

Return stale data and refresh in background instead of waiting #10

Merged
merged 2 commits into from
Oct 22, 2022

Conversation

geoffrich
Copy link
Owner

Closes #9

Instead of expiring data after 24 hours and refreshing, keep the stale data and refresh it in the background. This works by setting a parallel Redis key at year:[year]:[page]:c:fresh that does expire after 24 hours. When retrieving the cached data, we check if that key exists - if it doesn't, then we need to refresh the data, but still return the old data.

To refresh the data, we trigger a separate HTTP request against the app. This is because I don't trust the promise to resolve on its own if we already return from the function. We can't use the fetch passed to the handler since that will resolve against the app without an HTTP request, and we want to make a separate request.

Marvel data doesn't change that much, so the amount of stale data should be negligible. This will be a big perf improvement due to how slow the Marvel API can be.

@netlify
Copy link

netlify bot commented Oct 21, 2022

Deploy Preview for marvel-by-year ready!

Name Link
🔨 Latest commit 0090958
🔍 Latest deploy log https://app.netlify.com/sites/marvel-by-year/deploys/6353321ae7f55800080b680f
😎 Deploy Preview https://deploy-preview-10--marvel-by-year.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

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.

stale-while-revalidate for Marvel API responses
1 participant