-
-
Notifications
You must be signed in to change notification settings - Fork 5k
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
document getSSRProps
for directives
#14568
Comments
I am currently using a directive to toggle tabIndex based on the focus. It avoids focus trap if two tabindex (one link and one with an attribute tabindex=1) are nested. The code below is used: import { useFocusWithin } from "@vueuse/core"
export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.vueApp.directive('v-outside-focus-only', {
mounted(el: HTMLElement) {
const { focused } = useFocusWithin(el)
watch(focused, focused => {
el.tabIndex = focused ? -1 : 0
})
},
getSSRProps(binding, vnode) {
// you can provide SSR-specific props here
return {}
}
})
}) It returns the error (on version 3.0.0-rc.8): |
The current function in function ssrGetDirectiveProps(instance, dir, value, arg, modifiers = {}) {
if (typeof dir !== 'function' && dir.getSSRProps) {
return (dir.getSSRProps({
dir,
instance,
value,
oldValue: undefined,
arg,
modifiers
}, null) || {});
}
return {};
} Here In the vue source code (https://github.com/vuejs/core/blob/main/packages/server-renderer/src/helpers/ssrGetDirectiveProps.ts#L5), Ok. |
It is compiled to __vite_ssr_import_30__.ssrGetDirectiveProps(_ctx, _directive_outside_focus_only)) in
const _directive_outside_focus_only = __vite_ssr_import_29__.resolveDirective("outside-focus-only");
|
In the typescript type definition of |
As stated by the vue documentation https://vuejs.org/guide/reusability/custom-directives.html#introduction, vue directives can be imported the same way composables are... |
Turning it into a composable made it works: import { Directive } from "vue"
type UseVOutsideFocusOnlyReturnType = {
vOutsideFocusOnly: Directive
}
export const useVOutsideFocusOnly = (): UseVOutsideFocusOnlyReturnType => ({
// recommend to use the same name in the whole app with a property
vOutsideFocusOnly: {
mounted(el: HTMLElement) {
const { focused } = useFocusWithin(el)
watch(focused, focused => {
el.tabIndex = focused ? -1 : 0
})
},
getSSRProps(binding, vnode) {
// you can provide SSR-specific props here
return {}
}
}
}) and in a const { vOutsideFocusOnly } = useVOutsideFocusOnly() and in the template: <NuxtLink :to="myBeautifulLink" v-outside-focus-only>...</NuxtLink> It solves my problem in Vue 3 with |
getSSRProps
for directives
I'm getting this error when I use the wrong name for the directive in a SFC. So maybe its a matter of proper error handling, telling a dev that the wrong name is used or some error occured which made the directive unloadable? For exampleI've been able to reproduce the error using the below test: export default defineNuxtPlugin(nuxtApp => {
nuxtApp.vueApp.directive('click-outside', {
mounted: (el, binding, vnode) => {},
beforeUnmount: (el) => {}
})
}) And in my SFC this works:
But this produces the exact error (note the typo)
|
Unfortunately this error is coming from Vue itself and not Nuxt. Would you raise an issue on https://github.com/vuejs/core to improve the error? |
Oh wauw. Just realized I posted my finding on my Passionate People account. I will repost this issue on the Vue repo. Thanks! |
BTW: Feel free to link this issue in the new Vue issue then |
Encountered the same error following the docs for a simple focus directive. Only after adding // nuxt.config.ts
export default defineNuxtConfig({
vue: {
compilerOptions: {
directiveTransforms: {
focus: () => ({
props: [],
needRuntime: true
})
}
}
}
}); from #13351 once (!) it worked. |
The SSR error is also triggered if you have an unknown directive and a Nuxt layout. |
I have met the same problem.
|
How's it going? I have the same problem |
Also here unfortunately still stuck with the problem. I am using a directive or a globally installed plugin to add a directive. |
This is super annoying. Any updates on that? |
Workaround: add a abc.client.ts with the logic and a stub with abc.server.ts to at least ignore the problem for the moment … |
@BananaAcid how can we register the plugin for both client and server side? using Nuxt App? TIA |
sir @danielroe |
how can we register the plugin for both client/server? sir @danielroe |
@Kram-V just use filename.ts (no .client) to be loaded for both. You may check with if statements inside, it it got loaded on client or server side. |
For client-only directive, @BananaAcid 's solution works for me. Re-cap: Make your directive: Then copy it, rename "client" to "server" and strip out any logic: Moreover, Nuxt docs describes this exactly:
|
I believe the documentation is missing something or there is a bug about getSSRProps: https://v3.nuxtjs.org/guide/directory-structure/plugins/#vue-directives
When I copy the content and paste it into
/plugins/focus.ts
or/plugins/focus.client.ts
(in a project without any directives just with pages) I get the same getSSRProps missing error as anyone above - but the besides the error, it seems to work on most mounted elements.(To check, the
.focus()
changed to.style.color = 'green'
did its job)_Originally posted by @BananaAcid in #13351
The text was updated successfully, but these errors were encountered: