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

feat(nuxt): migrate to latest @vueuse/head#8000

Merged
danielroe merged 36 commits intonuxt:mainfrom
harlan-zw:feat/vueuse-head-migration
Oct 12, 2022
Merged

feat(nuxt): migrate to latest @vueuse/head#8000
danielroe merged 36 commits intonuxt:mainfrom
harlan-zw:feat/vueuse-head-migration

Conversation

@harlan-zw
Copy link
Copy Markdown
Collaborator

@harlan-zw harlan-zw commented Oct 5, 2022

🔗 Linked issue

resolves nuxt/nuxt#14271, resolves nuxt/nuxt#15029, resolves nuxt/nuxt#13848, (maybe resolves) nuxt/nuxt#15093

❓ 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

A number of updates have gone out for https://github.com/vueuse/head to try and improve the DX, fix bugs and unblock integration features. This PR aims to get Nuxt using them.

nuxt-hedge

To make the upgrade easier to test, I created the module https://github.com/harlan-zw/nuxt-hedge, it has core logic parity to this PR.

You can play with it here: https://stackblitz.com/edit/nuxt-starter-we3qh7?file=pages/index.vue


Nuxt meta Module Update

Missing Component props

Adds support for body and render-priority to components (Note: key is not supported by Vue for deduping)

Block DOM updates until page is ready

This solves the suspense issue and improves hydration speed. (nuxt/nuxt#14271)

Simplify ref wrapping/unwrapping

⚠️ Changes the shortcut props (viewport and charset) to only be typed for the nuxt.config.ts app.head. This avoids the complexity around wrapping every input in a ref

app.head is typed as a plain object - no reactivity

(see breaking changes)


@vueuse/head Updates

Type Improvements

Nuxt has not migrated to the latest @vueuse/head types (powered by zhead), this PR introduces them.

Legacy key support

Help users migrating from vue-meta / nuxt v2

Better Reactivity

Currently, users are restricted to refs and computed, now they can use computed getters like they can elsewhere in Vue and VueUse.

useHead({
  title: () => `${title.value} - My Site`
})

Hooks

These are not documented yet as they are intended to be for framework integrations.

More performant

When there are multiple useHead's, there is a significant performance improvement. This also allows tags will be ordered when CSR'd.


⚠️ Breaking Changes

The shortcuts viewport and charset are available to make configuring these global meta tags easier. However, it complicates useHead reactivity logic, so we choose to drop support for them within the composable.

Instead, these shortcuts can only be provided when used in nuxt.config.ts. If you are using the values (width=device-width, initial-scale=1, utf-8), then you can simply delete these as they are the defaults.

Before - Your page

useHead({
  viewport: 'width=device-width, initial-scale=2',
  charset: 'utf-16'
})

After - nuxt.config.ts

export default defineNuxtConfig({
    // ....
    app: {
      head: {
        viewport: 'width=device-width, initial-scale=2',
        charset: 'utf-16',
      },
    },
})    

Alternatively, you can always type out the full meta tag

After - Your page

useHead({
  meta: [
    { name: 'viewport', content: 'width=device-width, initial-scale=2' },
    { charset: 'utf-16' }
  ],
})

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@codesandbox
Copy link
Copy Markdown

codesandbox Bot commented Oct 5, 2022

CodeSandbox logoCodeSandbox logo  Open in CodeSandbox Web Editor | VS Code | VS Code Insiders

@netlify
Copy link
Copy Markdown

netlify Bot commented Oct 5, 2022

Deploy Preview for nuxt3-docs canceled.

Name Link
🔨 Latest commit b21a8ce
🔍 Latest deploy log https://app.netlify.com/sites/nuxt3-docs/deploys/6346ddc8973f0a00093f0e6e

Comment thread packages/nuxt/src/head/runtime/components.ts Outdated
@harlan-zw
Copy link
Copy Markdown
Collaborator Author

harlan-zw commented Oct 5, 2022

Still working on this and want to build out full tests, feel free to review the code but it may change

@jgupta
Copy link
Copy Markdown

jgupta commented Oct 10, 2022

It would be very nice if this could be part of rc12.

@socket-security
Copy link
Copy Markdown

socket-security Bot commented Oct 12, 2022

Socket Security Report

👍 No new dependency issues detected in pull request

Socket.dev scan summary
Issue Status
Did you mean? ✅ no new possible package typos
Install scripts ✅ no new install scripts
Telemetry ✅ no new telemetry
Troll package ✅ no new troll packages
Malware ✅ no new malware
Native code ✅ no new native modules
Bot Commands

To ignore an alert, reply with a comment starting with @SocketSecurity ignore followed by a space separated list of package-name@version specifiers. e.g. @SocketSecurity ignore foo@1.0.0 bar@2.4.2

⚠️ Please accept the latest app permissions to ensure bot commands work properly. Accept the new permissions here.

Powered by socket.dev

@danielroe danielroe mentioned this pull request Oct 12, 2022
7 tasks
Copy link
Copy Markdown
Member

@danielroe danielroe left a comment

Choose a reason for hiding this comment

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

This is very exciting ❤️ A few comments below.

Comment thread docs/content/1.getting-started/5.seo-meta.md Outdated
Comment thread docs/content/1.getting-started/5.seo-meta.md Outdated
Comment thread packages/nuxt/package.json Outdated
"@vue/reactivity": "^3.2.40",
"@vue/shared": "^3.2.40",
"@vueuse/head": "^0.7.12",
"@vueuse/head": "^1.0.0-rc.5",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

My only worry is if there are any bugs in @vueuse/head it will be difficult to fix them until the next Nuxt release. If you're happy to commit to no (intended) breaking changes between rc5 -> v1 then maybe we could consider ~ constraint?

wdyt @antfu?

Comment thread packages/schema/src/types/meta.ts Outdated
Comment thread packages/schema/src/config/_app.ts Outdated
Comment thread docs/content/1.getting-started/5.seo-meta.md Outdated
Comment thread docs/content/3.api/1.composables/use-head.md Outdated
Comment thread docs/content/3.api/1.composables/use-head.md Outdated
Comment thread packages/nuxt/src/head/runtime/lib/vueuse-head.plugin.ts Outdated
Comment thread packages/schema/src/config/_app.ts Outdated
@danielroe danielroe requested a review from atinux October 12, 2022 15:33
Comment thread test/fixtures/basic/types.ts
@danielroe danielroe changed the title feat(head): migrate to latest @vueuse/head feat(nuxt): migrate to latest @vueuse/head Oct 12, 2022
@danielroe danielroe merged commit 9e6d292 into nuxt:main Oct 12, 2022
@nicooprat
Copy link
Copy Markdown

I just migrated to latest Nuxt "nuxt": "3.0.0-rc.12", & Nuxt Content "@nuxt/content": "2.2.0", but it seems I still can't import useHeadRaw:

image

Did I miss something?

Thanks!

@harlan-zw
Copy link
Copy Markdown
Collaborator Author

harlan-zw commented Oct 21, 2022

I just migrated to latest Nuxt "nuxt": "3.0.0-rc.12", & Nuxt Content "@nuxt/content": "2.2.0", but it seems I still can't import useHeadRaw:

image

Did I miss something?

Thanks!

Hey @nicooprat

By default in the latest RC of @vueuse/head, all input is deemed raw.

There is an exposed function called useHeadSafe which would be used if you are inserting data from an untrusted source. If it's coming from nuxt/content you're probably fine to just use useHead. See v1 docs https://github.com/vueuse/head/tree/v1

Nuxt does not have this composable (yet), if you have any more questions on it please make an issue on the @vueuse/head repo.

@nicooprat
Copy link
Copy Markdown

Thanks for your answer.

I just understood that I was looking at the wrong version of the docs, the correct ones are from the latest RC tag: https://github.com/vueuse/head/tree/v1.0.0-rc.12

So useHeadRaw has been replaced by useHeadSafe. I successfully imoprted my analytics script this way:

if (process.client) {
  useHeadSafe({
    script: [
      {
        children: '...'
      },
    ],
  });
}

@danielroe danielroe added the 3.x label Jan 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

7 participants