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

title in useHead composable, when a function/computed, should be allowed to return undefined #435

Open
andreww2012 opened this issue Jan 3, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@andreww2012
Copy link

Environment

"@unhead/vue": "1.11.14"

Reproduction

https://stackblitz.com/edit/vitejs-vite-qt6hy722

Describe the bug

useHead/useSeoMeta's title supports passing a function or a Vue composable. As the types suggest, the return type should be string | null.

However, if undefined is returned, the behavior differs from returning an empty string or null, and is desirable in my case. I have a component A, which declares a page title and renders a component B. I'd like B to also declare a page title, but conditionally. My first attempt was to use the following code:

// Component B
useHead({
  title: () => {
    if (condition) return 'Actual title';
    return ''; // I meant here "Please use parent component title"
  }
});

I was expecting the title from A to be shown when the empty string was returned. However, it was counted as a valid title of B. Then I tried null and got the same result (it turned out being the same only because I was using the title template in the root component - otherwise undefined would be set as a title!). Finally I changed it to undefined and it worked as expected, but TypeScript wasn't happy with that because returning undefined is illegal.

You can play around with this behavior in the reproduction, changing the return value in HelloWorld.vue.

My suggestion is to either allow returning undefined, or, which is probably better because I don't understand why null is allowed and how it would be different from undefined - treat null as how undefined is currently treated. Or maybe both null and undefined should be allowed (like in the most of other properties of the composable) and have the equivalent behavior.

Additional context

No response

Logs

No response

@andreww2012 andreww2012 added the bug Something isn't working label Jan 3, 2025
@harlan-zw
Copy link
Collaborator

harlan-zw commented Jan 3, 2025

Thanks for the detailed issue, I agree with your suggestion.

PRs welcome otherwise will take a look when I have a chance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants