-
-
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
Preload Directives Generation not working #2460
Comments
This is expected: Vite only generates preload directives for direct imports. If Vite preloads every lazy/dynamic import, then they are not "lazy" anymore. Imagine you have an app with 100 lazy routes, you definitely don't want to preload all of them upfront for mobile users. You can, however, write a plugin with the |
Hi and thanks for looking into this @yyx990803! However, I think that I didn't explained myself very well.
I'm not suggesting that Vite should preload every lazy dynamic import. What I'm suggesting is that it preloads only those ones that are eagerly required from the root of the main chunk. For instance, consider this example: import { Suspense, useState } from "react"
const mod = import("./LoadAsap")
const LoadAsap = lazy(() => mod)
const LoadWhenActive = lazy(() => import("./LoadWhenActive"))
export default Main: React.FC = () => {
const [isActive, setIsActive] = useState(false)
return (
<div>
<Suspense fallback={null}>
<LoadAsap />
</Suspense>
<Suspense fallback={null}>
{isActive ? <LoadWhenActive /> : null}
</Suspense>
{!isActive ? (
<button
onClick={() => {
setIsActive(true)
}}
>
Load
</button>
) : null}
</div>
)
} Let's say that this is the root component and that therefore it's part of the main chunk. This component is using 2 different lazy imports: IMO what would be ideal is that Vite would preload the chunk for the
Thanks! this is useful! I already found a way to "hack" this rollup plugin for my own needs, but I may end up writing my own plugin for this. Thanks anyways! |
Hi @josepot can you share you solution, and how did you manage to tellwhich chunks to be shared? I also need something similar for the 5 top chunks used. Thanks in advance |
Describe the bug
I just migrated a TS React project from CRA to Vite and I'm very happy with the results. However, the
index.html
file doesn't have the<link rel="preload">
entries for the dynamic imports. At first, I thought that maybe the issue had to do with the fact that I was usingReact.lazy
. However, after refactoring my code to look like this:the chunks for those dynamic imports are still not present into the
index.html
file. They all get loaded immediately after the main chunk gets parsed, of course.I think that this is a bug, because the docs state that:
Reproduction
In order to isolate the issue, I've created this small repo with
npm init @vitejs/app test-dynamic-imports-vite-react-ts -- --template react-ts
, then I've added a basic component namedDynamicComponent.tsx
and I have imported that component from the root component like this:then after running
npm run build && cat ./dist/index.html
we can appreciate that theindex.html
is missing an entry that should look more or less like this:<link rel="preload" as="script" href="/assets/DynamicComponent.7f9cf23b.js">
.The actual project where I found this issue is this one, but that one is a lot more complex.
System Info
vite
version: 2.0.5Logs (Optional if provided reproduction)
The text was updated successfully, but these errors were encountered: