Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue loading all the chunks on the first request

I am learning Vue and just got to the routing chapter. I am able to create a template project with vue/cli with a initial router configuration. This is the Router code:

export default new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/',
      name: 'home',
      component: Home,
    },
    {
      path: '/about',
      name: 'about',
      // route level code-splitting
      // this generates a separate chunk (about.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import(/* webpackChunkName: "about" */ './views/About.vue'),
    },
  ],
});

According to the coments on the code and the docs this should be enough to lazy load the About component.

When I try to load the app page the "about.js" script is fetched:

About code not lazy loaded

If I navigate to About the browser fetches the script from disk cache:

enter image description here

I got confused. I was expecting to see the script about.js loaded only after I navigate to the About page. Am I missing something?

like image 452
marcellorvalle Avatar asked Oct 28 '25 03:10

marcellorvalle


1 Answers

The discussion pointed by @TJWeems was very elucidative.

I obtained the expected behavior after disabling the prefetch plugin with webpack-chain. I just created a vue.config.js on my root dir (the same as package.json) with the following:

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

Just to remember I did it for the sake of curiosity. I will probably leave the prefetch plugin enabled on future projects and just consider to disable it on specific situations.

like image 142
marcellorvalle Avatar answered Oct 29 '25 23:10

marcellorvalle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!