Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.
/frameworkPublic archive

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

Merged
pi0 merged 9 commits into
mainfrom
feat/shake-templates
Jul 14, 2022
Merged

perf(nuxt): tree-shake client-only components from ssr bundle#5750
pi0 merged 9 commits into
mainfrom
feat/shake-templates

Conversation

@danielroe

@danielroedanielroe commented Jul 6, 2022

Copy link
Copy Markdown
Member

🔗 Linked issue

similar, but for composables rather than templates: #5749

together, they resolvenuxt/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.

@danielroedanielroe 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 antfuJuly 6, 2022 14:10
@danielroedanielroe self-assigned this Jul 6, 2022
@netlify

netlifyBot commented Jul 6, 2022

Copy link
Copy Markdown

Deploy Preview for nuxt3-docs canceled.

NameLink
🔨 Latest commitaaad00c
🔍 Latest deploy loghttps://app.netlify.com/sites/nuxt3-docs/deploys/62d054f7cad1a30008bfef26

@pi0pi0 changed the title feat(nuxt): tree-shake templatesfeat(nuxt): tree-shake <ClientOnly> contents in ssr bundleJul 6, 2022
@pi0

pi0 commented Jul 6, 2022

Copy link
Copy Markdown
Member

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?

@pi0pi0 added the pending label Jul 6, 2022
@danielroe

Copy link
Copy Markdown
MemberAuthor

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

pi0 commented Jul 6, 2022

Copy link
Copy Markdown
Member

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...

@pi0pi0 removed the pending label Jul 6, 2022
@pi0pi0 changed the title feat(nuxt): tree-shake <ClientOnly> contents in ssr bundlefeat(nuxt): tree-shake client-only components from ssr bundleJul 6, 2022
@pi0pi0 changed the title feat(nuxt): tree-shake client-only components from ssr bundleperf(nuxt): tree-shake client-only components from ssr bundleJul 6, 2022
Comment threadpackages/nuxt/src/components/tree-shake.ts Outdated
Comment threadpackages/nuxt/src/components/tree-shake.ts Outdated
Comment threadpackages/nuxt/src/components/tree-shake.ts Outdated
@danielroe
danielroeforce-pushed the feat/shake-templates branch from 6800707 to f8abd25CompareJuly 7, 2022 11:13
pi0
pi0 approved these changes Jul 12, 2022
@ZibbpZibbp mentioned this pull request Jul 12, 2022
pi0
pi0 approved these changes Jul 14, 2022
pi0
pi0 approved these changes Jul 14, 2022
@pi0
pi0 merged commit df04a66 into mainJul 14, 2022
@pi0
pi0 deleted the feat/shake-templates branch July 14, 2022 17:46
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

3.xenhancementNew feature or request🍰 p2-nice-to-havePriority 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

@danielroe@pi0@antfu