-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
[✨Feature Request]: Add prefetch
option for async chunks
#10600
Comments
Currently Vite automatically preloads dynamic imports by default ( |
I think it doesn't. If you mean where
and
Vite definitely won't Also, if you go to Vite | Next Generation Frontend Tooling, and you will fine those in the I'm not sure and have not tried yet to use the same strategy to get the build file name of chunks as |
This comment was marked as spam.
This comment was marked as spam.
Is this feature be implemented? As @xsjcTony said, prefetch is important because it start to load chunk files only after the page completely loads. It means that start to load the file on browser idle time and not in parallel with the main chunks. |
@xsjcTony |
@enjoy-wind Yes I can, and I've already achieved it. The problem is how can I get the hash map, like, I've no idea which |
1.定义一个usePrefetch.js
2.定义一个useLoadAsyncComponents.js
3.使用 |
@enjoy-wind Yeah it seems like a valid way, appreciate that. But I may be misleading in my descriptions, what I actually want is to achieve the same thing So basically I still need the hashmap, since it doesn't make sense to use the custom hook everywhere in my code (like whenever there's a new link, I need to use it in the corresponding component) |
@xsjcTony 是否可以加个微信,我们一起看看怎么解决 |
@xsjcTony @enjoy-wind Did you finally implement this solution? |
We use a hack which appears to work (it assumes some internal workings of (() => import("./pages/some_page")).toString() gives us a string
when running the built application, which can be parsed to get the assets corresponding to the dynamic import (and then prefetch them when suitable). However - we would very much like to remove this hack and get the hash-map through an official |
通过import.meta.glob获取类似组件的混淆后的hash,然后通过正则命中Key,完成动态导入.
|
Any updates on this? I would love to convert from Webpack to Vite but without this, it will be tough. |
I found this blogpost that suggests a workaround: https://medium.com/@kiranv07/how-to-prefetch-javascript-bundle-in-a-vite-js-react-app-d38de7fe34fc But for what I can tell it will just load the splitted bundles in parallel, so the benefits of prefetching are lost. I think this is a very important feature for vite to have, I'm looking forward to see if it will be implemented :). |
Yeah exactly as you said, it's just downloading packages in parallel, even if the link is not in user's viewport that user is not likely going to click it |
Here our approach for Vue: blog post & gist We created a Vue plugin that monkey patch the
Concerning the OP issue with hashmap, we bypass it by actually loading the page file (the It could certainly be improved though! |
prefetch
option for async chunksprefetch
option for async chunks
I create a package to append assets into index.html as |
if we can get the generated html file during build, then use the regex to match the link we need and add prefetch to this link, can it works ? i haven't tried, this is just a idea |
It seems that this method will still block other components in the same fileThe only way I found around this is using something like this to load the component only after the page is loaded, but im not sure if it's a good solution. Also in this example I assume the component will be needed as soon as the page loads and everything else is loaded. Exampleconst fetchComponentOnLoad = new Promise((resolve, _reject) => {
window.addEventListener("load", () => resolve(import("./components/MyComponent")));
});
const MyComponent = lazy(() => fetchComponentOnLoad); |
hi~我也需要这个功能, 为此我写了一个Vite Plugin, 它将会分析 |
Is there any development on this? We also need to be able to load packages in parallel and as soon as possible. |
I built vite-preload which will let you inject and Link headers dynamically based on which dynamic imports was rendered. With this plugin, there is no need to manually do something, any dynamically imported react component that gets rendered will be preloaded. |
any solution to use it in |
Description
English:
Webpack
enables developer toprefetch
an async chunk using magic comment/* webpackPrefetch: true */
which will make a<link rel="prefetch" src="...">
tag in<head>
Code Splitting | webpack
will result in
<link rel="prefetch" href="login-modal-chunk.js">
It's nice to have the same feature in
Vite
, but I currently find no alternative.I tried adding those
<link>
tags manually usingIntersectionObserver
whenever an internal link goes into the user's view that the user is likely to click on, just like whatVitePress
did. (https://vitejs.dev/ did this)But the thing is, I'm NOT able to access the
hash
of the bundled chunk file, hence it's pretty hard to do unless I remove thehash
from bundled chunk file (so the filename is predictable) but I don't think it's a good way to do so, as it may result in more problems (naming conflict, etc.).Thus, I think the only solution is that vite can implement this feature internally.
(maybe an extra feature for something similar to
/* webpackPreload: true */
as well which result in<link rel="preload" ...>
tag)I personally think adding
prefetch
for lazy loading routes is pretty important to UX, but I just can't implement it at the moment.中文:
Webpack
可以通过/* webpackPrefetch: true */
让开发者prefetch
一些异步模块, 通过<head>
中的<link rel="prefetch" src="...">
实现.Code Splitting | webpack
结果为
<link rel="prefetch" href="login-modal-chunk.js">
我希望
Vite
也有类似的功能, 但是我没有找到相关的内容.我试着使用
IntersectionObserver
手动添加这些<link>
标签, 每当有app内部的超链接 (路由跳转之类的) 出现在用户的viewport
里, 也就是说用户有概率点击这个链接. (就像VitePress
使用的那样, 使用在了Vite
的官网上).但是我无法做到, 因为我没有办法访问打包之后异步模块的
hash
, 所以我不知道应该加载的文件名 (确实我也不应该知道, 因为这是在开发中不可预测的). 唯一的解法是在打包时不使用hash
, 但我并不认为这是一个好的选项, 因为他会导致更多的问题 (比如命名冲突).所以我觉得如果只能让
Vite
开发这个功能 (因为能够获取打包之后的hash
(大概吧?)), 然后让用户通过一些方式使用.(可能也可以顺便开发一个类似
/* webpackPreload: true */
的功能, 结果为<link rel="preload" ...>
标签)我个人认为针对懒加载路由做
prefetch
是挺重要的, 但是我目前没法实现.Related: #5818
Suggested solution
Something similar to this
will result in
<link rel="prefetch" href="login-modal-chunk-with-hash.js">
in<head>
Alternative
No response
Additional context
No response
Validations
The text was updated successfully, but these errors were encountered: