-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Scripts set in manualChunks are loaded directly in front page, instead to be lazy loaded when needed #5189
Comments
I can confirm that this issue occurs. Also, it seems that even dependencies I've never imported are passed to manualChunks, raising the chunks size. |
On the other hand, it is good to have the option to decide which chunks should be loaded immediately, because thus we will avoid the waterfall-effect on chunks that are always needed. The <link preload is not always working like expected and is more difficult to maintain, that is why this rule would be quite nice improvement of the loading times. I suggest to have either two rules manualChunks + preloadChunks or just to put this option in the manualChunks e.g. { chunkUrl, preload: true } |
I have some confusion about this issue, i use build.rollupOptions.output.manualChunks and import() in other project. Finally, i didn't find this issue. And, the reproducible repo isn't simple. |
Is something happening with this issue? @TrickyPi if you are using it without any issue in other projects, can you show us your example? Are you sure those manual chunks are really lazy-loaded? ... because this really does not work as expected. Once you start using manualChunks, lazy-routes are not lazy-loaded at all, they load together with index (first page) - utilizing the last version of vite and vue-router. |
I can confirm this too in our Vue app. We didn't have the issue before migrating from Webpack, using |
Yes the issue is still there and since it is build issue, I can not just make a code-sandbox example. The repository I mentioned in my first post shows clearly the problem. |
I created a minimal repo, and it works. step
|
I think this should be fixed now in Vite 2.9 (changelog). Would appreciate if y'all can verify this, and whether this can be closed. |
I think this "issue" is unrelated to the Vite's default chunking strategy. |
No change with Vite 2.9.1 unfortunately, all chunks are still loaded via |
I use your reproduction and add the plugin your repo vite.config.js:
|
Any fix in view (from Vite - no extra packages solution please!)? |
Same edge-case: |
I've been banging my head against the wall with this error. It appears that returning I also finally found a workaround that allows for custom chunk names similar to webpack's magic comments. I developed this because the plugin manualChunks: (id) => {
// This is how I do dynamic chunk naming.
// Example uses:
// import("some/module/index?chunkName=my-custom-chunk-name")
// import MyComponent from "some/component.vue?chunkName=restricted_path/my-custom-chunk-name"
if(id.includes("?chunkName=") || id.includes("&chunkName=")){
let name = id.match(/(?<=chunkName=)(.*)(?=&)|(?<=chunkName=)(.*)/gm)
if(name[0]) return name[0];
}
// Vendor Chunks
if(id.includes('node_modules')) return 'vendor';
// Here's the odd fix. You cannot return void, it must return a string
return "index";
}, I'm still cleaning this up and will post updates as it develops.
|
Ignore modulepreload
|
Hi everyone, I experienced the same problem and thanks to @pfdgithub , the following update solved the issue. Both build size and Lighthouse score ok now. {
build: {
modulePreload: {
resolveDependencies: (url, deps, context) => {
return [];
}
},
rollupOptions: {
output: {
sourcemap: false,
manualChunks: {
ethers: ['ethers'],
router: ['react-router-dom'],
rtk: ['@reduxjs/toolkit'],
redux: ['react-redux'],
chakra: ['@chakra-ui/react'],
},
},
},
}
} |
Hello @TrickyPi, I've cloned this repo and tried it. Yes, it works with Vite 2.9.1. But when I updated Vite to the latest 5.0.10, it was not working as expected anymore. Edit: The issue exists from Vite 3.0.0, everything is good until 2.9.16, so something broke this while transitioning from Vite 2 to 3. |
@huseyindeniz I tried following :
I'm getting a chunk / module BUT, all chunks are loaded immediately on page load. They are not loaded when required, which results in a bad lighthouse score. What am I doing wrong? Thank you for your help. |
hi @magicyoda did you add the following to your index.html file, if not, can you try
|
Why is this issue still opened? I've just checked it in vite@5 and the issue is still there. |
This appears to be unresolved in the latest version of Vite as far as I can tell, and none of the above solutions seem to address it. All manual chunks are eagerly loaded which has catastrophic effects on performance. |
BUMP!! Anything going on here?? Related issues breaks vue-router dynamic imports. Cannot find module with dynamic imports that are loaded on direct navigation to a URL. |
Still. |
Yes, that is the reason I don't use manualChunks since 2021, it kind a kill my website performance and it makes no sense to use them |
Any news on this one? It really should be fixed. |
Bump. |
2 years later: bump! Would be nice to have, at least, a feedback! |
This is still happening. Any news to resolve it? |
We really need to get this fixed. |
Is it not possible to load bundles async in Vite? I'm a bit confused. I need lazy loading and I was under the assumption Vite handled this through |
It is an actual bug, else manualChunks would be total useless concept since all the chunks are loaded at the beginning. |
@a-tonchev I was able to figure it out and I actually don't agree. You provide manual chunks with an The async loading using |
Okay then what would be your suggestion to solve the problem, can you give an example? |
async loading with an import works yes, but the question is that if you want to group multiple async components together to a certain chunks via manualChunks (just like you can do in Webpack with a magic comment "webpackChunkName"), it doesn't work as then the defined chunk is loaded always regardless of is the component used or not. |
Yes and that is the problem. Creating chunks makes only sense to load different parts of the JS on demand. If you load all the chunks at the beginning - code splitting makes no sense at all, you can just load the whole JS. That is why this bug makes the manualChunks useless |
Tested around and had the same issue. I wanted to split out the
import { defineConfig, splitVendorChunkPlugin } from "vite";
import react from "@vitejs/plugin-react";
import tsconfigPaths from "vite-tsconfig-paths";
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react(), tsconfigPaths(), splitVendorChunkPlugin()],
server: {
port: 3000,
},
build: {
rollupOptions: {
output: {
sourcemapExcludeSources: true,
manualChunks(id: string) {
if (id.includes("jsoneditor")) {
return "jsoneditor";
}
if (id.includes("node_modules") || id.includes("commonjsHelper")) {
return "vendor";
}
},
},
},
},
}); Example code of my async function setupJsonEditor() {
const JsonEditorLib = (await import("jsoneditor")).default;
await import("jsoneditor/dist/jsoneditor.min.css");
const editor = new JsonEditorLib(
..., Initial page load: I hope it helps someone! :) |
@Tschuck
For manual chunk, be sure to split them carefully to avoid circular dependencies. |
Thanks to @pfdgithub, I have made this work for our project. It appears that the issue was that while I was configuring a set of files for a specific As an example, I was trying to split a number of mock/test-related files out to a manual chunk which should not be loaded outside of the test environments. But rollup was adding The solution was to specify for all 3rd-party chunks where they should go, especially the ones mentioned by @pfdgithub: {
build: {
rollupOptions: {
output: {
manualChunks: (id) => {
// List of modules that rollup sometimes bundles with manual chunks, causing those chunks to be eager-loaded
const ROLLUP_COMMON_MODULES = [
'vite/preload-helper',
'vite/modulepreload-polyfill',
'vite/dynamic-import-helper',
'commonjsHelpers',
'commonjs-dynamic-modules',
'__vite-browser-external'
];
// Split out mock code into its own chunk
// (THIS IS AN EXAMPLE, REPLACE WITH WHATEVER YOU NEED)
if (id.includes('/node_modules/@mswjs/')) {
return 'mock';
}
// Bundle all other 3rd-party modules into a single vendor.js module
if (
id.includes('node_modules') ||
ROLLUP_COMMON_MODULES.some((commonModule) => id.includes(commonModule))
) {
return 'vendor';
}
// All other files are project source files and are allowed to be split whichever way rollup wants
}
}
}
}
} I hope this helps someone else. |
I was recently reading about a new vite-compatible bundler called Farm. It has an interesting set of options in what it calls Partial Bundling. Might be worth a look for those who are flexible with their build tools. I've not personally tried it myself, but hope to soon, as hand-tuning manualChunks is a real pain |
I found a way to drastically improve FP/FCP using The idea is to remove script lines with I have these manual chunks:
Now the waterfall looks like this (main chunk + manual chunks loads at the same time and blocks FP/FCP): But if I add a module that sets
As you can see the main JS chunk loads first and then it loads all manual chunks. Unfortunately works only with JS: ![]() The good thing is that FP/FCP happens after all sources from ![]() ![]() (thanks to https://github.com/johannschopplich/nuxt-vitalizer for the example of |
Same problem, use A quick look into load-script, vite mistakenly resolve the library dependency, I think it is not a simple problem. |
bump 🙏 |
Any update on this ? |
Describe the bug
To manage well the bundle, I am setting the packages into manualChunks e.g.:
The package fabric is used only in the admin area of my app, that is why I don't need it to be loaded directly in the front page.
If I don't set it in manualChunks, it works good and it will not be loaded in the front page, but my chunks then are too large, because vite place it automatically together with others in a huge backend chunk. Then as soon I open the backend it lazy loads all the other chunks, including the one that contains fabric. So this is the expected behavior.
If I set it in manualChunks, e.g.:
the fabric chunk is loaded directly on the front page. Even if I am not admin.
You can see the differences when I include fabric or not:
Not included in manualChunks
![image](https://user-images.githubusercontent.com/54528540/135865291-de898aa1-b70a-4569-a080-78e9f40e1407.png)
Included in manualChunks:
![image](https://user-images.githubusercontent.com/54528540/135865404-ca96c4fd-aa4b-43ce-b8d6-0bff831009ff.png)
Expected behavior is: fabric should only be loaded when really used, else it creates only network traffic and lowers the Lighthouse score because of unused code!
NOTE: I am using an example with fabric here, but in my project I have a bunch of other libraries that have the same issue with manualChunks.
Reproduction
I created a small reproduction in this repo:
https://github.com/a-tonchev/react-boilerplate/tree/vite_no_lazy_load
Steps to reproduce:
yarn
)Try bug:
yarn build && yarn serve
fabric
script is loadedTo try without, open vite.config.js:
https://github.com/a-tonchev/react-boilerplate/blob/vite_no_lazy_load/vite.config.js
and comment out line 40
yarn build && yarn serve
fabric
script is no more loaded,System Info
Windows 10 + WSL (Ubuntu) Vite version "^2.3.8"
Used Package Manager
yarn
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: