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

[Vue] Add support for lazy-loading with Async Components #482

Merged
merged 1 commit into from
Oct 4, 2022

Conversation

Kocal
Copy link
Member

@Kocal Kocal commented Sep 25, 2022

Q A
Bug fix? no
New feature? yes
Tickets Fix #...
License MIT

I was a bit surprised when trying the Symfony UX' Vue integration, to see all Vue components/controllers to be loaded to render at least only one controllers.
This PR aims to reduce the bundle size and only load the required code and Vue controllers to render on the page.

To enable lazy-loading, add the 4th parameter "lazy" to require.context():

- registerVueControllerComponents(require.context('./vue/controllers', true, /\.vue$/));
+ registerVueControllerComponents(require.context('./vue/controllers', true, /\.vue$/, "lazy"));

Note: the sync-loading still works

Real life example

Given 6 really simple Vue controllers which all look the same:
image

If I render only two of them on a single page:
image

Before

Capture d’écran 2022-09-25 à 11 03 16

You can see the app.js is about 4.8 kB, it contains all of the 6 Vue controllers.

After

Capture d’écran 2022-09-25 à 11 03 38

You can see the app.js is now about 3.4 kB, and two more files have been lazy-loaded (our two Vue controllers rendered on the page).

On a real project, with a lot of Vue controllers, the difference would be very significative. I will try to post some results when migrating our main project at work.


Note: I still have to add tests and update the documentation. done

Copy link
Member

@weaverryan weaverryan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome! I've never used defineAsycComponent and require.context is always complex :). But you've got the tests, I love it.

Do you have anything else you need to do?

@weaverryan weaverryan added the Status: Waiting Feedback Needs feedback from the author label Sep 27, 2022
@Kocal
Copy link
Member Author

Kocal commented Sep 27, 2022

Thanks for the review @weaverryan!

On this PR, I think nothing is missing, however I thought about a usecase for a 2nd PR: I think some people would like to have both lazy-loaded and not lazy-loaded controllers/components at the same time.
E.g.: common components like a NavBar or Aside that would be always shown on every pages, and specific components like a date picker that would be sometimes shown in some context.

Currently, that's not possible to have both registerVueControllerComponents calls like this:

registerVueControllerComponents(require.context('./vue/controllers', true, /\.vue$/));
registerVueControllerComponents(require.context('./vue/controllers-lazy', true, /\.vue$/, 'lazy'));

because vueControllers and loadComponent, which are used by the global function resolveVueComponent(), will be erased.

@weaverryan
Copy link
Member

@Kocal I think you're right - but I think that will be solved by #461

@weaverryan weaverryan force-pushed the feat/vue-lazy-components branch from 4305e5a to 8fa08d0 Compare October 4, 2022 13:49
@weaverryan
Copy link
Member

Thanks Hugo!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Waiting Feedback Needs feedback from the author
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants