question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: How do I code split by route using vue-router@next?

See original GitHub issue

I’d like to lazy load routes / create a chunk for each route but I can’t figure out how to make it work. In Webpack I can use a dynamic import with a “magic comment” to name the chunk.

Something like this would be nice but magic comments are something I don’t really care about, I’d just like the output of my build to be split by route and to have the dynamic imports reference the .js file.

import { RouteRecordRaw } from "vue-router"

export const routes: Array<RouteRecordRaw> = [
  {
    name: "About",
    path: "/about",
    component: () => import(/* webpackChunkName: "about" */, "../views/About.vue")
  }
]

This thread seems to indicate it works now using rollup-plugin-dynamic-import-variables but using a dynamic import in the router doesn’t ouput what I’d expect – is there something else I need to configure?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:15 (1 by maintainers)

github_iconTop GitHub Comments

16reactions
onx2commented, Feb 16, 2021

Sure! I simply changed it to the following:

import { defineAsyncComponent } from "vue"
import { RouteRecordRaw } from "vue-router"

export const routes: Array<RouteRecordRaw> = [
  {
    name: "About",
    path: "/about",
    component: defineAsyncComponent(() => import("../views/About.vue"))
  }
]

[Section about generate routes removed to prevent misinformation]

If someone knows how to make dynamically generated routes work, please comment.

11reactions
fjeddycommented, Feb 14, 2021

Hate that no one answered the original question 😕 How do you set a custom name on the chunk.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Use Vue Router: A Complete Tutorial - Vue School Blog
Let's open up the router/index. js file. We are importing createRouter and createWebHistory from the vue-router library. Next, we import the ...
Read more >
the best way to split view between login page and dashboard ...
One way to handle this is to have an intermediary page component that just contains the layout you want, with your other routes...
Read more >
Lazy loading components and code splitting in Vue.js
This is done by splitting each route's components into chunks that are separate from the main chunk loaded on initialization.
Read more >
How To Use Nested Routes in Vue.js - DigitalOcean
Then, navigate to the newly created project directory: cd vue-nested-routes-example.
Read more >
Lazy Loading Routes | Vue Router
When building apps with a bundler, the JavaScript bundle can become quite large, ... It would be more efficient if we can split...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found