Nuxt.js: Lazy loading

Created on 3 Dec 2016  路  4Comments  路  Source: nuxt/nuxt.js

Is it possible to only execute certain bits of code like we can with base webpack or vue-router require.ensure, System.import? If so would this be possible after generate too?

My use case is using highlight.js but i'd only wanna load this on certain routes

This question is available on Nuxt.js community (#c29)
question

Most helpful comment

@dan-gamble if you want to use highlight.js only on a specific page component, juste import it in your page component:

pages/about.vue:

<script>
import highlightjs from 'highlight.js'

export default {
}
</script>

Make sure that you don't use highlight.js in your config.build.vendor and config.plugins.

This way, highlight.js will be in the bundle of the /about page and we will be imported only when loading this route 馃憤

This is due to the code splitting we define in the router of Nuxt.js, like @declandewet described in his comment.

All 4 comments

Hi @dan-gamble - I haven't used Nuxt.js much but @Atinux and I discussed this a lot a few weeks ago so based on that my assumption is it should be possible - and the in the code it looks like it is the default for the client build: https://github.com/nuxt/nuxt.js/blob/master/lib/app/router.js#L9

@dan-gamble if you want to use highlight.js only on a specific page component, juste import it in your page component:

pages/about.vue:

<script>
import highlightjs from 'highlight.js'

export default {
}
</script>

Make sure that you don't use highlight.js in your config.build.vendor and config.plugins.

This way, highlight.js will be in the bundle of the /about page and we will be imported only when loading this route 馃憤

This is due to the code splitting we define in the router of Nuxt.js, like @declandewet described in his comment.

Awesome, thanks guys.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vadimsg picture vadimsg  路  3Comments

uptownhr picture uptownhr  路  3Comments

vadimsg picture vadimsg  路  3Comments

pehbehbeh picture pehbehbeh  路  3Comments

lazycrazy picture lazycrazy  路  3Comments