Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code splitting not working with router.js #122

Open
ajoliveau opened this issue Jul 12, 2022 · 3 comments
Open

Code splitting not working with router.js #122

ajoliveau opened this issue Jul 12, 2022 · 3 comments

Comments

@ajoliveau
Copy link

Hey,

I need to use a custom router.js, but what I found was that Nuxt doesn't bundle different chunks for different pages when it's enabled.
Here's an example repo : https://github.com/ajoliveau/nuxt-router-bug
It's a default empty nuxt app with only "@nuxtjs/router" added and a few components to test.

When I keep it as-is with the router module commented and I go to localhost:3000/page1 this is what I see :
image
It loads all the default JS files, and then it loads chunks depending on when they're needed (page1 at first, and page2 asynchronously because there's a NuxtLink pointing to it).

If I uncomment the router module in nuxt.config.js and I go pack to page1 (this time the dynamic page), this is what I see :
image
No code splitting, no chunks for each page or component, it loads the whole JS of the whole application at once.

Is this normal behaviour ? Is there a way to enable code splitting when using the router-module ?

Thanks

@Scoottykun
Copy link

+1 on this, i have this issue on a large project and it really impact overall performance

@ajoliveau
Copy link
Author

I had this problem, for what it's worth I switched to using extendRoutes in the nuxt.config.js and all the use-cases I needed in a separate router.js could be changed to fit into extendRoutes and code-splitting and everything else works fine with it.

@leofredy
Copy link

leofredy commented Sep 6, 2023

Hey guys, i find a solution.

I can use Code Splitting + @nuxtjs/router, but you need to import your page like my example router below:

export function createRouter () {
  return new Router({
    mode: 'history',
    routes: [
      {
        path: '/',
        component: () => import('@/views/ViewHome.vue' /* webpackChunkName: "ViewHome" */).then(view => view.default || view)
      },
    ]
  });
}

OBS: the "then" call is what nuxt would do under the hood

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants