1

I'm trying to use Webpack's code splitting feature to decrease load times on my application, but I am struggling to get this to work. There are no errors in the console, and the file is being loaded in the network tab. However, the Vue component doesn't display on the screen. When inspecting the element in Chrome DevTools, I see <!----->, which indicates that the component is being loaded, but not shown for some reason.

I've tried toying around with the settings in Laravel Mix (i.e. renaming the chunks and changing the destination folder), but nothing seems to resolve the problem.

This is how I am trying to import the additional components:

let todoList = () => {
    import( '../../components/dashboard/todoList' );
}

export default {
  components: {
    todoList
  }
}

I would expect that this would import the component when required. It seems to be referencing the split script, but for some reason the component is not loading. What am I doing wrong here?


This is what I am seeing in the network tab in DevTools.

Showing the additional JS files being loaded

4
  • If you click 1.js and look at the response for it, that's a bundle generated by Webpack. So it likely is loading your component in one of those bundles. Is there any sort of conditional rendering on your component that could be causing it to not render, like v-if? Commented Aug 13, 2019 at 19:16
  • What code are you using to registering your components? Commented Aug 13, 2019 at 19:36
  • @NickG I have a v-if statement on the parent for loading which is v-if="!isLoading" Commented Aug 13, 2019 at 19:59
  • @tony19 to register my components I am simply doing the following components: {todoList} Commented Aug 13, 2019 at 20:00

1 Answer 1

3

The todoList arrow function needs to return the dynamically imported component definition. Currently, it returns nothing.

You could either use the shorthand return, removing the statement brackets around the dynamic import (preferred for concise syntax and improved readability):

export default {
  components: {
    todoList: () => import(/* ... */)
  }
}

...or add a return keyword inside the statement brackets:

export default {
  components: {
    todoList: () => {
      return import(/* ... */)
    }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Cheers Tony, massive help!
@GarethJones No problem :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.