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

Lazy loading routes don't work in generated build #339

Closed
husayt opened this issue Jun 3, 2020 · 6 comments
Closed

Lazy loading routes don't work in generated build #339

husayt opened this issue Jun 3, 2020 · 6 comments
Labels
enhancement New feature or request

Comments

@husayt
Copy link
Contributor

husayt commented Jun 3, 2020

Describe the bug

Routes stop working in generated bundle when I use dynamically imported (async) route components (in dev all works fine):

export const routes =   [
     { path: "/", component: "Home" },
     { path: "/about", component: "About" }
  ].map(({ path, component, meta }) => ({
     path,
     component: () => import(`./views/${component}.vue`)
   }))

Reproduction

Reproduction Repo: https://github.com/husayt/vite-tailwind-starter

Reproduction step

yarn build
yarn servestatic

When i go to localhost:8080 I get this error in the console:

index.d91b05ae.js:1 GET http://localhost:8080/_assets/views/Home.vue net::ERR_ABORTED 404 (Not Found)

Same with /about. So routes don't work, although they work fine with yarn dev

System Info

  • required vite version: 0.20.2
  • required Operating System: Windows 10
  • required Node version: 14.3
  • Optional:
    • npm/yarn version yarn 1.22.4

Detailed description

Routes work fine in the original project from this repo where pages imported statically.

import Home from './views/Home.vue'
import About from './views/About.vue'
import NotFound from './views/NotFound.vue'

/** @type {import('vue-router').RouterOptions['routes']} */
export let routes = [
  { path: '/', component: Home, meta: { title: 'Home' } },
  { path: '/about', component: About, meta: { title: 'About' } },
  { path: '/:path(.*)', component: NotFound },
]

But once I change routes to dynamic import, they stop working in generated bundle (dev still works fine)

const list = [
  { path: "/", component: "Home", meta: { title: "Home" } },
  { path: "/about", component: "About" },
  { path: "/:path(.*)", component: "NotFound" },
]


/** @type {import('vue-router').RouterOptions['routes']} */
export const routes = list.map(({ path, component, meta }) => ({
  path,
  component: () => import(`./views/${component}.vue`),
  meta: meta || { title: component },
}))
@husayt husayt changed the title Dynamicly imported routes don't work in generated build Lazy loading routes don't work in generated build Jun 3, 2020
@ChrisRu
Copy link

ChrisRu commented Jun 3, 2020

This currently seems to be a limitation of esbuild.

@husayt
Copy link
Contributor Author

husayt commented Jun 3, 2020

I see, but this is such an important case it will be good to have some kind of workaround. Technically routes are known, so build tool can process them and add tree shakable dynamically loaded chunks.

@yyx990803
Copy link
Member

Full dynamic import path imports are currently untouched during build, because Rollup doesn't provide default handling for it.

@yyx990803 yyx990803 added enhancement New feature or request and removed pending triage labels Jun 4, 2020
@husayt
Copy link
Contributor Author

husayt commented Jun 4, 2020

@yyx990803 Is there any alternative syntax to use, until esbuild starts to support this?

@csr632
Copy link
Member

csr632 commented Jun 5, 2020

@husayt In 0.20.3 vite use rollup-plugin-dynamic-import-variables, so it should works.

@husayt
Copy link
Contributor Author

husayt commented Jun 5, 2020

Actually indeed it does work now. Thanks @yyx990803

@github-actions github-actions bot locked and limited conversation to collaborators Jul 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants