At the initialization of my application, Vue Router effortlessly loads all my dynamically imported components

My Vue application is utilizing lazy loading router setup with the following configuration:

router.ts

const Grid = () => import(/* webpackChunkName: "grid" */ './views/grid/Grid.vue');
const Calendar = () => import(/* webpackChunkName: "calendar" */ './views/calendar/Calendar.vue');
const Tree = () => import(/* webpackChunkName: "tree" */ './views/tree/Tree.vue');

const routes = [
    { path: '/', component: Grid, name: 'grid' },
    { path: '/calendar', component: Calendar, name: 'calendar' },
    { path: '/tree', component: Tree, name: 'tree' },
];

export const router = new VueRouter({ routes });

I have implemented Babel and incorporated the dynamic import plugin as recommended in the documentation

babel.config.js

module.exports = {
  presets: [
    '@vue/cli-plugin-babel/preset',
  ],
  plugins: [
    '@babel/plugin-syntax-dynamic-import',
  ],
};

Upon inspecting the application in a browser, I notice that all required assets are being loaded when navigating between routes. However, during the initial load, I observe that all chunks are already loaded.

This behavior does not appear to be due to prefetching since all chunks are preloaded without delay, rendering the app temporarily unusable while everything loads.

Why are all my chunks being loaded upfront? How can I approach debugging this issue?

Answer №1

To start, the first step is to verify that prefetching is not being utilized...

  1. After building your app, examine the generated index.html file - if you notice <link> tags with rel="prefetch" and your lazy loading chunks, this indicates that Vue CLI is automatically prefetching. You can refer to this link for information on how to disable or customize prefetching.

  2. Next, check the network tab in Dev Tools and choose one of your lazy loaded chunks - inspect the Request headers for the presence of Purpose: prefetch (Firefox uses X-Moz prefetch). If this header is present, it means that the browser requested the chunk due to the prefetch link...

Now, you can validate your suspicion that these chunks are slowing down the initial rendering of your app. The best approach is to utilize Dev Tools once again - specifically the Performance tool (I typically prefer using Chrome over Firefox).

In Chrome: Simply open Dev Tools, switch to the Performance tab, and click on the small "reload" icon with the tooltip "Start profiling and reload page." You can stop the profiling once the page has finished loading.

Here is a screenshot from one of my apps. This particular app is a Vue CLI application with only one lazy route ("admin"). It may appear that the DOMContentLoaded event (blue DCL) and First Paint (green FP) coincide strangely with the completion of the 'admin98...' chunk loading, suggesting that the lazy loaded "admin" chunk is blocking the initial paint of my app.

However, upon closer inspection, you'll notice a task running ("Parse HTML" + "Evaluate Script" - highlighted in the screenshot) - this task begins after the final "non-prefetch" script is loaded ("chunk-vendors" in this instance) and coincidentally finishes at the same time as the completion of the 'admin' chunk loading. Therefore, the browser is parsing other non-lazy loaded JS scripts of my app while simultaneously downloading prefetch scripts. It becomes evident that prefetching a lazy loaded chunk does not impede or delay the initial paint of my app, despite appearances...

This serves as just one example

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Stop the background from being visible using the jQuery Cycle plugin

I'm facing a challenge with the cycle plugin for jquery when creating a slideshow. The transition between slides allows what's underneath to show, but I want a smoother transition where one slide truly fades into the other without the background ...

- Challenges with internal systems

I have a dialog window where I want to display a confirm dialog when clicking Cancel. To achieve this, I am creating a div element with some text that should be shown in the confirm dialog. However, the issue I'm facing is that the text intended for t ...

There are no Vue.js updates reflected in Heroku when using Laravel

I've encountered an issue with Heroku (PaaS). I'm in the process of launching my first project using Laravel, and I'm consistently making changes. It's my understanding that every modification made during development needs to be pushed ...

Unable to get windw.location.href to function properly on local server

Having trouble with window.location.href not working for me :/ Everything is running on a localhost using "xampp" //javascript function 1 function onLogin() { window.location = "http://localhost:4000/deletetable"; console.log("running..."); con ...

What is the best way to deactivate all active classes from buttons before selecting a new one?

I'm fairly new to working with Vue and I'm struggling to figure out how to properly handle buttons that need to have only one active at a time. Currently, I am using the activeBtn method but it doesn't seem to deactivate the previously activ ...

Activate jQuery Following an Ajax Request - Extension for Enhanced Navigation Using Ajax - WooCommerce Plugin

After searching extensively, I couldn't find a solution directly related to my issue. My knowledge of jQuery is limited, so I'm hoping the solution is something simple. Here is my situation... • I am using a WooCommerce website and have integ ...

What is the best way to implement jQuery on my website?

Instead of copying all the example code here, you can check out the jQuery demo page: Demo Page I would like to integrate the Latest News example with scrolling text in red from the demo page into my website. The demo page also provides links to the sour ...

Vue - Child component not refreshing its data and triggering a re-render

In this scenario, I have the ability to upload a single LiteratureReview which can contain multiple Quote components. Essentially, I am dealing with parent-child relationships between LiteratureReview and Quote. Data retrieval and submission are done using ...

Attempting to display items using the map method, pulling in text from an array

I am working with an array state that tracks the text entered by the user in a text field. My goal is to display this text within a component so users can see what they have previously entered. However, I am facing an issue with my Hashtags component when ...

Obtaining a JavaScript proxy from a WCF service using webHttpBinding

I have configured all the necessary endpoints, bindings, and behaviors to consume a service using JSON. However, I am struggling to find a way to generate a JavaScript proxy for accessing the service from my client-side JavaScript with jQuery through Ajax. ...

What is the best way to send a POST request to a certain URL using Axios in a React application?

I am facing a challenge with making a post request to a rather complex URL: http://localhost:8080/api/login?email=example%40gmail.com&password=examplepassword as per the Swagger-UI documentation. The examples on axios's GitHub page mention that th ...

Prevent vertical axis rotation in OrbitControls with THREE.js

In my three.js project, I have a scene with OrbitControls that allows me to rotate the camera around the origin at position 0,0,0. I am attempting to restrict the camera rotation to only rotate vertically along the y-axis infinitely (up or down). You can ...

Crafting callback functions

My jQuery code looks like this: $('#current_image').fadeOut(function(){ $('#current_image').attr('src',newImage).show(); }); This process is wonderful - the nested function runs smoothly after the fadeOut. I ...

Image caption dynamically updated to match thumbnail caption using jQuery upon click event

My goal is to dynamically load the data-caption of thumbnail images when clicked, and then update the main image's data-caption when the main image is changed with a thumb image. I am currently struggling to make the data-caption update along with the ...

Add a dynamic "printthis" button to a specific class on the webpage

As a novice in JavaScript, I am struggling with implementing the PrintThis.js JQuery plugin to create a print button for any div with the "printdiv" class. My approach involves using jQuery to handle the numbering of buttons and div classes, appending nece ...

What is the best way to transform a unicode string into JSON format?

Looking to transform the Unicode string below into a JSON Object. var str = '{"method_title":"Bank. Transfer","instructions":"Account Name: Sriram Me Co.,Ltd.\r\n-------------------------------------------- ...

What are the best practices for storing an array of objects in MongoDB with Mongoose?

For my project, I needed to store data in a mongoDB document as an array of objects using Javascript, Node JS, Express framework, and Mongoose library. After researching online, I found two different methods to achieve this: Creating another sub-schema ...

JavaScript UDP pinger timeout using the dgram module for nodejs

Currently, I am enrolled in a course where we are tasked with coding a UDP pinger using Javascript with Node.js and Dgram. The assignment provided to us is as follows: Our objective is to create the client-side code for an application. This client should ...

Tips on how to choose all elements that do not include a specific value or group of values in cypress

Here's my situation: selector: ".list > div" const velue = [6, 9, 21] Each div contains a different value. I want to remove elements that contain a specific value, then select the first remaining element and click on it. It should look ...

Receiving an error when trying to access the 'get' property of $http within a nested function

Encountering the following error angular.js:13550 TypeError: Cannot read property 'get' of undefined at Object.DataService.getDataFromAPI I seem to be struggling with the $HTTP.get function. Any insights on what might be going wrong or ...