I am attempting to serve a create-react-app site to AWS Cloudfront after deploying to S3.
The index.html is still trying to get old main.xxxxx.js after deploy to S3. A default service-worker.js is used to cache site in browser.
if you reload the browser, app is loading correctly.
The conflict may occur between service-worker.js cache and cloudfront cache. server-worker doesn't get the newly uploaded main.xxx.js but instead old one while cloudfront has newly main.xxxxx.js after we invalidate existing files
Re-produce:
npm run build.
aws s3 sync ./build s3://$S3_BUCKET/frontend --delete --acl 'public-read'
aws cloudfront create-invalidation --distribution-id $CLOUDFRONT_ID --paths '/*'
aws s3 cp ./build/service-worker.js s3://$S3_BUCKET/frontend/service-worker.js --cache-control max-age=0
The cloudfront is set to Use Origin Cache Headers (will use file Cache-Control headers)
Expected Behavior: After new deployment with the steps mentioned, when we try to access app on first attempt, this should get the new main.xxxx.js file and load updated contents