I am deploying a project which was generated using CRA(Create-React-App), and all the babel and webpack configurations remain unmodified.
Also, I am deploying my React application using Static Bucket hosting feature of AWS S3.
The problem is, since there are no servers included, every time a new file is deployed, new chunk with random hashes are created.
When a client was in the website with old hash, and new hash was deployed, client gets Chunk Load Error.
To demonstrate this, build project with yarn build
and go to /build
folder to see chunks generated by webpack. Then using serve
, I can demonstrate how my deployment will behave in my local machine.
After yarn serve
was executed, go to appropriate localhost where build files are being served.
Then, at /build/static/js
folder, rename the hashes of each chunk file as you wish.
Leave the browser alone, and restart yarn serve
, which will serve renamed chunk files.
Then get back to the browser, and you will most likely get two errors:
Uncaught SyntaxError: Unexpected token '<'
Loading Chunk n failed.(missing: ~~)
The way I think can solve this problem is to HARD reload the webpage when ChunkLoadError occured. Sadly, window.reload(boolean)
function has been deprecated, and doing only window.location.reload()
doesn't solve this issue.
+) PS. I implemented Code-Splitting by using React.Lazy()
in some codes.