Nuxt.js: Preload async chunks on a specific route

Created on 3 Oct 2019  路  3Comments  路  Source: nuxt/nuxt.js

What problem does this feature solve?

Hello,

I wonder if it's possible to preload/prefetch async chunks for a specific route.
I tried to use bundleRenderer/shouldPreload but it's only available on nuxt.config.js, right?

Any hint about this?

This feature request is available on Nuxt community (#c9860)
feature-request

Most helpful comment

You can already do this thanks to "webpack magic comments" (what a name 馃槄):

// inside your component, or page or layout...

const MyPreloadedComponent = () =>
  import(
    /* webpackPreload: true */
    /* webpackChunkName: "MyPreloadedComponent" */ '~/components/MyPreloadedComponent'
  )

export default {
  components: {
    MyPreloadedComponent,
  },
}

Dynamic import will make the chunk to be loaded asynchronously and the /* webpackPreload: true */ will make a request with the preload option. The webpackChunkName is to help you identify those chunks in the Network tab on development.

All 3 comments

Are you talking about doing this dynamically during runtime or you want a specific route to _always_ be preloaded?

Let's consider I'm loading an async chunk xyz.js on route B, and not on route A. I'd like to be able to always preload xyz.js when I'm landing on route B.

You can already do this thanks to "webpack magic comments" (what a name 馃槄):

// inside your component, or page or layout...

const MyPreloadedComponent = () =>
  import(
    /* webpackPreload: true */
    /* webpackChunkName: "MyPreloadedComponent" */ '~/components/MyPreloadedComponent'
  )

export default {
  components: {
    MyPreloadedComponent,
  },
}

Dynamic import will make the chunk to be loaded asynchronously and the /* webpackPreload: true */ will make a request with the preload option. The webpackChunkName is to help you identify those chunks in the Network tab on development.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

o-alexandrov picture o-alexandrov  路  3Comments

jaredreich picture jaredreich  路  3Comments

bimohxh picture bimohxh  路  3Comments

maicong picture maicong  路  3Comments

lazycrazy picture lazycrazy  路  3Comments