vite icon indicating copy to clipboard operation
vite copied to clipboard

Persistent Cache

Open yyx990803 opened this issue 3 years ago • 18 comments

https://github.com/vitejs/vite/issues/1207#issuecomment-753564633

Vite's current biggest performance bottleneck is first page load with large amount of modules. This is mostly caused by congestion at the browser network layer, since every module results in a full HTTP GET request on fresh server start.

To improve this, we can dump the in memory transform cache to disk on server close, and "hydrate" the module graph on next server start (the hydration should only fill in the cache for files with still fresh mtimeMs). The cache can be dumped in one file so it should be relatively fast to deal with. This allows the server to directly send 304s even on a restart.

yyx990803 avatar Jan 03 '21 04:01 yyx990803

@yyx990803 Recently, I found that pre-binding has been implemented through esbuild in the optimizer directory.

Is this problem solved?

lubanproj avatar Feb 05 '21 04:02 lubanproj

we started to migrate our monorepo to vite. our biggest frontend project has around 2800 modules on load. we were able to reduce it to 1800 with lazyload after some work. currently, we offer vite as an alternative alongside webpack, but it's not the default bundler yet, in dev. the main reason why we can't deprecate our webpack dev in favor of vite alternative is that it takes a long time to reload the page. the load of 1800 creates a great bottleneck, and in our case, it can take from 10s to 20s depending on how much load there's on the developer machine.

Suggestion: cache the files with a service worker, that after the first load, at least any consecutive load would be fast, and when a module is being changed that info can be transmitted by the hmr mechanism/websocket and deprecate the out of dated module.

AlonMiz avatar Jul 09 '21 13:07 AlonMiz

Possibly related https://github.com/antfu/vite-plugin-optimize-persist

LifeIsStrange avatar Oct 14 '21 11:10 LifeIsStrange

The idea of persistent cache sounds great. It's been a while since the issue was created. What's the current status?

kjeske avatar Dec 06 '21 22:12 kjeske

最近把老项目从 webpack 迁移到 vite 时又遇到这个问题了,记录了下优化的方法, 虽然没有从实质上解决问题,但是也加速不少,https://carljin.com/vite-resolve-request-files-a-ton.html

carl-jin avatar Dec 24 '21 04:12 carl-jin

Any discussion opened for this issue? Hope to apply a service worker as @AlonMiz mentioned to do some pre-tasks with a more heuristic pre-bunding discovery algorithm and persist them.

zheeeng avatar Dec 30 '21 02:12 zheeeng

@AlonMiz, curious if you've improved your load performance in the interim -- we've got a project on a similar scale facing the same sorts of issues. Would love to compare notes!

davidwallacejackson avatar Dec 31 '21 01:12 davidwallacejackson

@AlonMiz, curious if you've improved your load performance in the interim -- we've got a project on a similar scale facing the same sorts of issues. Would love to compare notes!

Unfortunately, we've halted the vite migration from webpack. We couldn't find an intermediate solution for this. Developers preferred one slow load and fast refreshes than fast first load and long refreshes

AlonMiz avatar Dec 31 '21 05:12 AlonMiz

@AlonMiz, curious if you've improved your load performance in the interim -- we've got a project on a similar scale facing the same sorts of issues. Would love to compare notes!

Unfortunately, we've halted the vite migration from webpack. We couldn't find an intermediate solution for this. Developers preferred one slow load and fast refreshes than fast first load and long refreshes

Thanks for letting me know. Did HMR/Fast Refresh work for you at all? Curious because I'm currently betting big on optimizing my own app for HMR.

davidwallacejackson avatar Dec 31 '21 19:12 davidwallacejackson

@davidwallacejackson yes it did pretty well in most cases, some issues in edge cases, but not something significant. eg. developing react components worked great

AlonMiz avatar Jan 01 '22 17:01 AlonMiz

Hi, guys, I found a couple steps to optimize browser loading performance. Although it cannot solve the problem from the root cause, but it speeds up a lot.

If u want to quickly feel the changes after optimization. Here are some suggestions

  1. use Chrome dev version as u dev browser
  2. Install as less browser plug-ins as possible,Especially The AdGuard and AdBlock who tracking every record you request when opening the page.
  3. use https and make sure ur certificate is valid, vite-plugin-mkcert. like this issue mentioned https://github.com/vitejs/vite/issues/2725. You don't want 304 cache not working, right?
  4. As mentioned in the article, optimize your pre-build https://vitejs.dev/config/#optimizedeps-include

I recently migrated the aPaaS project from webpack to vite and optimized it according to the above steps. The following is a speed comparison

tool Server startup time First time page loaded (no cache) Second time page loaded (with cache) HMR built
Webpack 83s 4.78s 3.35s 4.78s 3mins 37s
Vite 4.72s (second time 0.72s) 1.71s 1.33s less then 300s 51.45s

carl-jin avatar Jan 03 '22 03:01 carl-jin

Hi, guys, I found a couple steps to optimize browser loading performance. Although it cannot solve the problem from the root cause, but it speeds up a lot.

If u want to quickly feel the changes after optimization. Here are some suggestions

  1. use Chrome dev version as u dev browser
  2. Install as less browser plug-ins as possible,Especially The AdGuard and AdBlock who tracking every record you request when opening the page.
  3. use https and make sure ur certificate is valid, vite-plugin-mkcert. like this issue mentioned https dev server does not use browser cache #2725. You don't want 304 cache not working, right?
  4. As mentioned in the article, optimize your pre-build https://vitejs.dev/config/#optimizedeps-include

I recently migrated the aPaaS project from webpack to vite and optimized it according to the above steps. The following is a speed comparison

tool Server startup time First time page loaded (no cache) Second time page loaded (with cache) HMR built Webpack 83s 4.78s 3.35s 4.78s 3mins 37s Vite 4.72s (second time 0.72s) 1.71s 1.33s less then 300s 51.45s

This is the details of optimization step and why https://carljin.com/vite-resolve-request-files-a-ton.html

carl-jin avatar Jan 03 '22 03:01 carl-jin

@AlonMiz, curious if you've improved your load performance in the interim -- we've got a project on a similar scale facing the same sorts of issues. Would love to compare notes!

Unfortunately, we've halted the vite migration from webpack. We couldn't find an intermediate solution for this. Developers preferred one slow load and fast refreshes than fast first load and long refreshes

I successfully migrated a large project from webpack to vite. The biggest problem I face is that some packages do not support ESM, you have to write compatibility or re-fork and release. For example, ant-desgin-vue needs to write a vite plugin to solve the ESM problem in their code. It took me about 1 week to process the migration, and now I am very satisfied, because the HMR speed of vite is so dope, once you experience it, you can’t forget it.

carl-jin avatar Jan 03 '22 03:01 carl-jin

If anyone interested I open this repo to play with this (POC), recenlty pushed and still not working: https://github.com/userquin/vite-dev-sw

The sw is being registered with type: 'module' and so we can use esm on it: in fact I'm using import.meta.env.BASE_URL on the sw rn.

Initial gist from @davidwallacejackson.

EDIT: rn it is also working with F5 and Crtl + F5, we can now play with the sw

imagen

userquin avatar Jan 16 '22 16:01 userquin

we can also inject some resources such as precaching on the plugin, rn we read it from file, if we have the transform cache on disk we can inject it on the sw.

userquin avatar Jan 16 '22 17:01 userquin

The slow initial page loads are still the main issue we have that impacts vite adoption. On some pages of our app there are 1900 module requests, and especially on Linux laptops it can take up to 30 seconds to do the initial load, even if no files were actually changed and it was just vite that was started again.

For this reason we have kept webpack around and some devs prefer using webpack especially when they work mostly on the (Python) backend of the app, since then bundling only happens once and afterwards all page loads are fast.

Any way to speed this up, whether it's using a persistent file cache, service workers, or something else, would be great.

Bunkerbewohner avatar Jul 12 '22 14:07 Bunkerbewohner

If you're using React you can get a speed up by using https://github.com/ArnaudBarre/vite-plugin-swc-react-refresh

ArnaudBarre avatar Jul 12 '22 15:07 ArnaudBarre

Has anyone considered using websockets to get around the HTTP connection concurrency limits?

justinbhopper avatar Jul 30 '22 21:07 justinbhopper

always use http2

lovetingyuan avatar Oct 19 '22 03:10 lovetingyuan

less then 300s

@carl-jin less than 0.3s?

milahu avatar Dec 28 '22 16:12 milahu

不仅仅是大型项目加载慢,我的项目只有两个页面,登录页和首页,组件不超过10个,但是首屏加载竟然需要45秒,惊呆我了...

WangJincheng4869 avatar Apr 08 '23 08:04 WangJincheng4869

Hi everyone, The current decision of the team is to make the core faster so cache is not needed. And if one plugin/processor is slow, the cache should be done at this level, not for everything. You can help make Vite faster by providing link to slow apps or sending profile output (vite --profile and press p when the page is loaded)

Also http1 is not the limitation: https://github.com/ArnaudBarre/esm-dev-server-limit

ArnaudBarre avatar Apr 08 '23 08:04 ArnaudBarre

With RSC, an increasing number of npm packages will need transpilation. If an RSC app has 20 npm packages that need transpilation, then it seems to me that we'll need a cache. Especially considering that RSC code living at node_modules/ can't be transpiled by esbuild alone but will require a slow JavaScript plugin vite-plugin-react-server-components.

E.g. a global cache ~/.local/share/vite/cache/npm/some-npm-package/ for npm packages. Essentially something like this: https://github.com/vitejs/vite/discussions/8721#discussioncomment-3016493. A bit like pnpm's global cache ~/.local/share/pnpm/store/.

brillout avatar Apr 08 '23 09:04 brillout

Let's see how this RSC move plays out in the ecosystem, but I'm confident that either someone will make a fast transformer for it, either a small cache on top of the RSC transformer will be good enough and easy to implement (or maybe the RSC architecture will not play nicely with Vite, but that's another issue)

ArnaudBarre avatar Apr 08 '23 10:04 ArnaudBarre

👍 I agree.

Although a global cache could be a pretty cool addition nevertheless: a global cache guarantees that Vite's dev speed stays instantaneous (library sizes increases while on-demand user code size doesn't).

On a tengent: a global cache would allow use to aggressively transform npm packages, e.g. to remove CJS-ESM compatibility problems.

But, I agree, let's see how things turn out.

brillout avatar Apr 08 '23 12:04 brillout

For RSC case, the cache problem is something like this? https://github.com/Shopify/hydrogen-v1/blob/v1.x-2022-07/packages/hydrogen/src/framework/plugins/vite-plugin-hydrogen-client-components-cache.ts

JiangWeixian avatar Apr 08 '23 15:04 JiangWeixian

不仅仅是大型项目加载慢,我的项目只有两个页面,登录页和首页,组件不超过10个,但是首屏加载竟然需要45秒,惊呆我了...

这不太可能是vite的问题

lovetingyuan avatar Apr 08 '23 15:04 lovetingyuan

不仅仅是大型项目加载慢,我的项目只有两个页面,登录页和首页,组件不超过10个,但是首屏加载竟然需要45秒,惊呆我了...

这不太可能是vite的问题

没必要洗地的,官网已经承认现在vite的性能就是差,在想办法优化。只要用了SCSS就会卡,你可以试试配置element plus 自定义主题,配置scss之后明显变卡。

WangJincheng4869 avatar Apr 08 '23 16:04 WangJincheng4869

Lack of persistent cache and annoying unnecessary rebuilds is my main frustration with Vite in comparison with Parcel 2 (which has its own flaws too, but speed and annoying unnecessary rebuilds are not those).

burdiyan avatar Nov 29 '23 13:11 burdiyan