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

fix(compat): do not auto invoke scoped slots when accessed via $slots #10875

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

andreww2012
Copy link

@andreww2012 andreww2012 commented May 6, 2024

The problem

If the component has a render function and uses this.$slots.name expression somewhere, this usage might throw if name is used as a scoped slot and the data the slot passes is destructured.

Why this happens?

If RENDER_FUNCTION compat feature is enabled and the component is using a render function (_compatWrapped), all $slots.name accesses will be intercepted by a proxy which will auto-invoke the corresponding function with no arguments:

$slots: i => {
if (
isCompatEnabled(DeprecationTypes.RENDER_FUNCTION, i) &&
i.render &&
i.render._compatWrapped
) {
return new Proxy(i.slots, legacySlotProxyHandlers)
}
return __DEV__ ? shallowReadonly(i.slots) : i.slots
},

If these arguments are destructured by a component user, this weird runtime error will occur: (destructured parameter) is undefined in Firefox or Cannot destructure property '<property name>' of 'undefined' as it is undefined in Chromium.

The fix

This PR attempts to resolve the issue by avoiding invoking the slot if it has _ns (non-scoped slot) flag.

Real-world examples

I think this fix should not break the existing code which is written and used correctly because

  • If a slot was never intended to be used as a scoped slot but is used as such, that is a component user's mistake - and runtime errors in this case are a possibility (for example caused by non auto-invoking $slots.name)
  • If a slot can be used as a scoped and a non-scoped one, it is lib author's responsibility to use $slots or $scopedSlots respectively (which vuetify actually does).

@andreww2012 andreww2012 force-pushed the fix-compat-invoking-scoped-slots-with-no-arguments branch from bab9eaa to 834512e Compare May 6, 2024 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants