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

feat: allow automatic injection of head() into components #43

Merged
merged 13 commits into from
May 9, 2020

Conversation

danielroe
Copy link
Member

@danielroe danielroe commented May 8, 2020

This improves the useMeta() helper by removing the need to pass head into defineComponent. Instead, a helper function is injected into components that can then be hooked into by useMeta.

BREAKING CHANGE: useHead() no longer exists and instead useMeta can be used directly within setup, as long as a minimal head: {} is defined in the component options.

Note: Work has already begun on composable hooks for vue-meta that will mean we don't even need to define a minimal head object: see here.

TODO:

  • Add documentation
  • allow accessing head values from useMeta

closes #41

@vercel
Copy link

vercel bot commented May 8, 2020

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/nuxt-community/composition-api/auv8mou50
✅ Preview: https://composition-api-git-component-redefinition.nuxt-community.now.sh

@danielroe danielroe self-assigned this May 8, 2020
@danielroe danielroe added the enhancement New feature or request label May 8, 2020
@mathe42
Copy link
Collaborator

mathe42 commented May 8, 2020

When I use defineComponent in all of my components I have to.Import it directly from @vue/composition-api but I want one import.

I think renaming it to something like definePageComponent with this change i can get asyncData to work.

Head does only Work on components in the Page folder.

@danielroe
Copy link
Member Author

danielroe commented May 8, 2020

Head does only Work on components in the Page folder.

I think head works for any component (see vue-meta docs).

This is a drop-in replacement for defineComponent but obviously it does a little more work. Can you think of any more performant approach, or is it just a case of opt-in via a different method? (Or we could pass options - e.g. head: true enables the feature.)

Do you have a view from DX perspective @chanlito?

src/component.ts Outdated Show resolved Hide resolved
src/component.ts Outdated Show resolved Hide resolved
src/component.ts Outdated
): ReturnType<typeof define>

export function defineComponent(options: any) {
const reactiveHead = reactive<MetaInfo>({})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use ssrRef?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your thinking...?

@@ -0,0 +1 @@
declare module 'defu'
Copy link
Member Author

@danielroe danielroe May 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will need to go. defu has types but for some reason rollup wasn't recognising them until I edited its package.json.

unjs/defu#15

src/component.ts Outdated Show resolved Hide resolved
src/component.ts Outdated
if (!vm) throw new Error('useMeta must be called within a component.')

const { head } = vm.$options
if (!head || !(head instanceof Function))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does vm.$options has head property if I set one in my nuxt.config.js ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it is per-component.

@danielroe danielroe marked this pull request as ready for review May 9, 2020 16:12
src/meta.ts Show resolved Hide resolved
@mathe42
Copy link
Collaborator

mathe42 commented May 9, 2020

We can add a value to the Option so we can check if your defineComponent was used

@vercel vercel bot temporarily deployed to Preview May 9, 2020 19:45 Inactive
@danielroe danielroe merged commit f1bda39 into master May 9, 2020
@danielroe danielroe deleted the component-redefinition branch May 9, 2020 19:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: improve the head API
3 participants