question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

Conflict between service worker and CloudFront?

See original GitHub issue

Iā€™m deploying my React app to S3 behind a CloudFront distribution. With my particular process, I copy all the files to S3, and then copy index.html in the following manner:

aws s3 cp --cache-control 'public, no-cache, must-revalidate, proxy-revalidate, max-age=0' ./build/index.html $APP_BUCKET

The Cache-Control used to just be max-age=0, but I ran into problems with aggressive caching and added more options. I never want index.html to be cached, otherwise when I release updates, users would still get the old index.html pointing to the old JS files.

Now Iā€™m wondering if the service worker is somehow getting in the way. Should I remove it since Iā€™m using CloudFront? Or is there a better way than what Iā€™m doing to make sure that when I deploy a code update, that users will get it next time they reload or hit the site?

Thanks.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
ffxsamcommented, Sep 6, 2017

I set the CloudFront TTLs (default, min, max) all to 0. Thereā€™s still odd behavior going on. If I redeploy my updated app and then open it up, I see in the console a message telling me to refresh, that thereā€™s an update. I do so, and I see the code update, but the log says once again to refresh to get an update. That behavior seems wrong.

Any insight on how to properly deploy to S3 + CloudFront would be really helpful!

UPDATE:

I think I got it. Iā€™ve left CloudFront cache settings alone (using origin headers, etc), and changed my deployment to single out these two files now:

aws s3 cp --cache-control max-age=0 ./build/index.html s3://myapp.com/
aws s3 cp --cache-control max-age=0 ./build/service-worker.js s3://myapp.com/

The service worker itself was getting cached, which is problematic.

But someone let me know if thereā€™s a better way still. šŸ˜‰

0reactions
gaguirrecommented, Dec 7, 2018

@lalzada I was working in a test version actually, so I had no real users actually. It was a solution just for me to avoid waiting 1 day.

Thanks for your question, Iā€™ll edit my comment šŸ‘

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to configure Service Worker with React to prevent ...
You should always cache it on Cloudfront edge since you can run invalidation requests to purge cloudfront edge cache after a new deployment....
Read more >
Troubleshoot issues with CloudFront custom object caching
If your CloudFront distribution isn't caching based on the custom values that you set on cache behaviors, then check the origin. Verify whether...
Read more >
Install and Configure Amazon CloudFront for Sites Hosted at ...
Amazon CloudFront is a highly-configurable CDN. Learn how to install and configure it for best compatibility with Kinsta's CloudflareĀ ...
Read more >
Production deploy of a Single Page App using S3, CloudFront ...
Building and uploading content to AWS S3 with the correct HTTP Cache headers so that ... To support deep links, the main Distribution...
Read more >
Deploying React and Angular Single Page Applications to ...
Do not cache service-worker.js file: If you are using PWA, ... Let's now dive into the relationship between CloudFront and S3.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found