Falling Back to Cache if 404
See original GitHub issueIs there a recognised pattern or method for using the cache if the live page 404s or there is no network connection?
For example, one way might be to try to get the page without the cache, and if that fails, then try to use the cache:
try:
with requests_cache.disabled():
r=requests.get('http://httpbin.org/ip')
if r.status_code!= requests.codes.ok:
raise ValueError()
#If this is good, add it to the cache somehow?
#...
except:
r=requests.get('http://httpbin.org/ip')
print(getattr(r, 'from_cache', False),r.text)
Issue Analytics
- State:
- Created 7 years ago
- Reactions:3
- Comments:6
Top Results From Across the Web
Setting "no-cache" for 404 files · Issue #245 · evansd/whitenoise
I'm thinking on how to tackle this and I see two possibilities: a custom middleware that sees if the 404 has STATIC_URL as...
Read more >Why 404.html causes uncaught error during service worker ...
This means the service worker will only install if all of the resources in cache.addAll have been cached. Firebase returns 404 Not Found...
Read more >ERROR 404 after a while, clear cache fixes it - Drupal
After a while I get suddenly a 404 error page instead of my node pages (no field added, type=page) in the default language...
Read more >Facing 404 error. It broke my website. - WordPress.org
The homepage is fetching deleted cached css file which gives 404 error. ... AO FAQ for more info. if it is active be...
Read more >Dynamic Routes Details - Next.js
If fallback is false , then any paths not returned by getStaticPaths will result in a 404 page. If fallback is true ,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Fyi, changes for the first option I mentioned (manually saving a response) are now in the master branch and in pre-release build
0.7.0.dev255. You can useCachedSession.cache.save_response(response)to manually save a response object.The second option (adding a
raise_for_statusoption) will be done in issue #210. I’ll close this issue for now, then, unless there’s a different way you would prefer to handle 404s.In case someone else has similar issue as mine, here is the solution I came up with: