I'm having a strange performance problem in my React + Vite application.
Everything works well and quickly during development mode (npm run dev).
Also, when building the project itself (npm run build) to production, it finishes the build process well and doesn't give any errors or warnings. It takes about a minute to finish, even so some files are over 500kb. The index.js generated in dist/assets has 1800 KB of size.
The problem is, when I run it to production, which is hosted on Google Cloud Storage + GCP's Web Server, or even test it with npm run preview, the app slows down by alot.
Pages take forever to load, and DevTools shows big waterfalls with huge delays in loading and rendering of JS chunks.
Here are some performance improvements I've already made to Vite to try and fix this:
- No barrel files;
- Used clear file extensions in imports;
- Minified with esbuild;
- Disabled sourcemaps;
- opped console logs with
terserOptions.
Even with those things already applied, the production performance is still very bad; I didn't see any improvements.
Has anyone else been in a situation like this?
Could this problem have something to do with chunking, dynamic imports, caching headers, or even how GCP is sending the files? I have no idea what is causing this.
PS: Details for achieving MRE and better understanding of my current architecture.
- My
vite.config.jsuses@vitejs/pluginc-reactandtarget: 'es2020'; - Minification is done using
esbuild; - Sourcemaps are disabled;
- Console Logs are also disabled;
- I implemented some manual chunks into
vendor(React and ReactDOM),mui(MUI Core and Icons),charts(ApexCharts, Charts.js and React Wrapper) andutils(Axios, dayjs, uuid); - My
packages.jsoncontains over 70 dependencies in total; - Design Libraries include:
@mui/material(Which I'll need to move other components to use this one only)react-bootstrap,reactstrap,evergreen-ui(Legacy clutter)
- My
tsconfig.jsonusesES2020andmoduleResolutionis already setted to "bundler". strictmode and common linting flags are enabled and it only compilessrc/folder.
Important: I'm aware that using multiple UI components libraries (MUI + Bootstrap + Evergreen) is not ideal and can negatively impact performance, however, this is a legacy code base I inherited and I'm already planning to standardize everything around Material UI.
Still, since the slow down issue could be related to other things, like chunking strategy, HTTP headers, CDN cache, etc, I'm seeking help here to better understand what could be causing this severe slowdown in production.