Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

perf(nuxt): tree-shake client-only components from ssr bundle #5750

Merged
merged 9 commits into from Jul 14, 2022

Conversation

danielroe
Copy link
Member

@danielroe danielroe commented Jul 6, 2022

πŸ”— Linked issue

similar, but for composables rather than templates: #5749

together, they resolve nuxt/nuxt#14192

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

Vue will still render the slots/children of a client-only component, which often creates the non-intuitive situation that it's not enough to wrap a component that's incompatible with SSR (often a third-party component). Instead, it needs to be used only with in a ~/components/SomeWrapper.client.vue. But we can do better. This PR strips out any children from client-only components, rendering just the wrapper on SSR.

The one caveat is that because we are naively matching <template> with RegExp, this will break with nested versions of the same component, e.g. <ClientOnly><ClientOnly></ClientOnly></ClientOnly>. I think this is an edge case and it's probably enough to warn in the docs, but if we want to support it, I can rewrite to use a different matching method other than RegExp.

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@danielroe danielroe added enhancement New feature or request 🍰 p2-nice-to-have Priority 2: nothing is broken but it's worth addressing labels Jul 6, 2022
@danielroe danielroe requested a review from antfu July 6, 2022 14:10
@danielroe danielroe self-assigned this Jul 6, 2022
@netlify
Copy link

netlify bot commented Jul 6, 2022

βœ… Deploy Preview for nuxt3-docs canceled.

Name Link
πŸ”¨ Latest commit aaad00c
πŸ” Latest deploy log https://app.netlify.com/sites/nuxt3-docs/deploys/62d054f7cad1a30008bfef26

@pi0 pi0 changed the title feat(nuxt): tree-shake templates feat(nuxt): tree-shake <ClientOnly> contents in ssr bundle Jul 6, 2022
@pi0
Copy link
Member

pi0 commented Jul 6, 2022

This is a nice to have enhancement but as we discussed and you also mentioned in the description, we have the new method of .client components which are explicitly declared to be in client-side bundle and are not included in server bundle. Adding this plugin, also has performance downside when not needed and probably edge cases.

Is there any case do you think we cannot offer to migrate from <ClientOnly> to .client/.server components?

@pi0 pi0 added the pending label Jul 6, 2022
@danielroe
Copy link
Member Author

Even with .client components, there is still an enhancement to be made by tree shaking out their children, which would otherwise be rendered even if not in the produced HTML.

Moreover, it's not always compatible with best practices in component design. If we have to extract a separate .client component for each layout chunk, that has a negative and fragmentary impact on codebase.

I think it is better to have both options in the code base, for different purposes.

@pi0
Copy link
Member

pi0 commented Jul 6, 2022

Fair point about tree-shaking children πŸ‘ Then let's try it :)

Off topic: regarding .client vs Client-only, using wrapper beings fragmentation too since developer has to always remember adding wrapper and can easily forget to by mistake...

@pi0 pi0 removed the pending label Jul 6, 2022
@pi0 pi0 changed the title feat(nuxt): tree-shake <ClientOnly> contents in ssr bundle feat(nuxt): tree-shake client-only components from ssr bundle Jul 6, 2022
@pi0 pi0 changed the title feat(nuxt): tree-shake client-only components from ssr bundle perf(nuxt): tree-shake client-only components from ssr bundle Jul 6, 2022
@Zibbp Zibbp mentioned this pull request Jul 12, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
3.x enhancement New feature or request 🍰 p2-nice-to-have Priority 2: nothing is broken but it's worth addressing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

tree-shake client-only components and composables
3 participants