Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue router always loads the lazy loaded modules on intial loading

Here is the lazy loaded implementation using Vue official router

src/router/index.js

import Vue from "vue";
import VueRouter from "vue-router";

const Foo = () => import("@/components/Test2");

const Bar = () => import("@/components/Test");

Vue.use(VueRouter);

export default new VueRouter({
  mode: "history",
  routes: [
    {
      path: "/test",
      name: "test",
      component: Bar
    },
    {
      path: "/test2",
      name: "test2",
      component: Foo
    }
  ]
});

src/main.js

import Vue from "vue";
import App from "./App.vue";
import router from "./router";

Vue.config.productionTip = false;

new Vue({
  render: h => h(App),
  router
}).$mount("#app");

Routes work as expected however the lazy loading not working properly, when I inspect the network tab on the first load I can able to see the web pack generated lazily loaded files

like image 871
shellakkshellu Avatar asked Jan 22 '19 13:01

shellakkshellu


People also ask

What is lazy loading in vue router?

vue file. Lazy loading refers to an approach by which all the scripts are not loaded on the DOM as the application starts. Instead, they're only loaded when requested, which makes the JavaScript bundle size very small at initial load. Vue.

How is lazy loading achieved when is it useful what are its pitfalls?

One form of lazy loading is infinity scroll, in which, the content of the web page is loaded as and when the user scrolls down the page. It is a popular technique being used by various websites. Advantages of Lazy loading: On-demand loading reduces time consumption and memory usage thereby optimizing content delivery.


1 Answers

The problem is the webpack preloadplugin adds a prefetch tag to all async chunks. To prevent this, add the following to your vue.config.js

  chainWebpack: config => {
    config.plugins.delete('prefetch');
  }

Source: https://github.com/vuejs/vue-cli/issues/979#issuecomment-373027130

like image 193
ChuckFields Avatar answered Oct 07 '22 10:10

ChuckFields