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

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

Merged
merged 36 commits into from Oct 12, 2022
Merged

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

merged 36 commits into from Oct 12, 2022

Conversation

harlan-zw
Copy link
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

codesandbox bot commented Oct 5, 2022

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

@netlify
Copy link

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

@harlan-zw
Copy link
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

jgupta commented Oct 10, 2022

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

@socket-security
Copy link

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
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.

docs/content/1.getting-started/5.seo-meta.md Outdated Show resolved Hide resolved
docs/content/1.getting-started/5.seo-meta.md Outdated Show resolved Hide resolved
@@ -44,7 +44,7 @@
"@nuxt/vite-builder": "3.0.0-rc.11",
"@vue/reactivity": "^3.2.40",
"@vue/shared": "^3.2.40",
"@vueuse/head": "^0.7.12",
"@vueuse/head": "^1.0.0-rc.5",
Copy link
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?

packages/schema/src/types/meta.ts Outdated Show resolved Hide resolved
packages/schema/src/config/_app.ts Outdated Show resolved Hide resolved
docs/content/1.getting-started/5.seo-meta.md Outdated Show resolved Hide resolved
@danielroe danielroe requested a review from Atinux October 12, 2022 15:33
@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

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
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

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