Commit Graph

14 Commits

Author SHA1 Message Date
8f2a77702b feat: InspiraUI
Some checks failed
/ surge (push) Successful in 2m41s
/ build-and-deploy-to-vercel (push) Successful in 2m59s
/ lint-build-and-check (push) Has been cancelled
/ playwright (push) Has been cancelled
2025-04-01 11:55:16 +08:00
ba91e845d5 帮我把 @unhead/vue 的版本升级到 v2.0.2
Some checks failed
/ surge (push) Failing after 1m1s
/ playwright (push) Has been skipped
/ lint-build-and-check (push) Failing after 1m31s
---
title: Migrate Your TypeScript App To Unhead v2
description: Learn about how to migrate to TypeScript Unhead v2 from v1.
navigation:
  title: Upgrade Guide
---

## Introduction

With the release of Unhead v2, we now have first-class support for other frameworks. However, this guide will focus on
the changes that affec TypeScript users.

The high-level of Unhead v2 was to remove deprecations and remove the implicit context implementation.

### Legacy Support

Unhead v2 is mostly fully backwards compatible with Unhead v1.

While not recommended, if upgrading is not possible for you, you can change your imports to the following:

```diff [TypeScript]
-import { createServerHead, useHead } from 'unhead'
+import { createServerHead, useHead } from 'unhead/legacy'
```

This will be removed in a future minor version, so you should lock your dependencies to the version that works for you.

## Client / Server Subpath Exports

🚦 Impact Level: Critical

**⚠️ Breaking Changes:**

- `createServerHead()`{lang="ts"} and `createHead()`{lang="ts"} exports from `unhead` are removed

The path where you import `createHead` from has been updated to be a subpath export.

Please follow the updated installation instructions or simply update the import to use the subpath.

**Client bundle:**

```diff
-import { createServerHead } from 'unhead'
+import { createHead } from 'unhead/client'

// avoids bundling server plugins
createHead()
```

**Server bundle:**

```diff
-import { createServerHead } from 'unhead'
+import { createHead } from 'unhead/server'

// avoids bundling server plugins
-createServerHead()
+createHead()
```

## Removed Implicit Context

🚦 Impact Level: Critical

**⚠️ Breaking Changes:**

- `getActiveHead()`{lang="ts"}, `activeHead`{lang="ts"} exports are removed

The implicit context implementation kept a global instance of Unhead available so that you could use the `useHead()`{lang="ts"} composables
anywhere in your application.

```ts
useHead({
  title: 'This just worked!'
})
```

While safe client side, this was a leaky abstraction server side and led to memory leaks in some cases.

In v2, the core composables no longer have access to the Unhead instance. Instead, you must pass the Unhead instance to the composables.

::note
Passing the instance is only relevant if you're importing from `unhead`. In JavaScript frameworks we tie the context to the framework itself so you
don't need to worry about this.
::

::code-group

```ts [TypeScript v2]
import { useHead } from 'unhead'

// example of getting the instance
const unheadInstance = useMyApp().unhead
useHead(unheadInstance, {
  title: 'Looks good'
})
```

```ts [TypeScript v1]
import { useHead } from 'unhead'

useHead({
  title: 'Just worked! But with SSR issues'
})
```

::

## Removed `vmid`, `hid`, `children`, `body`

🚦 Impact Level: High

For legacy support with Vue Meta we allowed end users to provide deprecated properties:  `vmid`, `hid`, `children` and `body`.

You must either update these properties to the appropriate replacement, remove them, or you can use the `DeprecationsPlugin`.

**Meta tags with `vmid`, `hid`**

These are already deduped magically so you can safely remove them there.

```diff
useHead({
  meta: [{
    name: 'description',
-   vmid: 'description'
-   hid: 'description'
  }]
})
```

**Other Tags with `vmid`, `hid`**

Use `key` if you need the deduplication feature. This is useful for tags that may change from server to client
rendering.

```diff
useHead({
  script: [{
-   vmid: 'my-key'
-   hid: 'my-key'
+   key: 'my-key',
  }]
})
```

**Using `children`**

The `children` key is a direct replacement of `innerHTML` which you should use instead.

::Caution
When migrating your code ensure that you're not dynamically setting `innerHTML` as this can lead to XSS vulnerabilities.
::

```diff
useHead({
  script: [
      {
-        children: '..'
+        innerHTML: '..'
      }
   ]
})
```

**Using `body`**

The `body` key should be updated to use the Tag Position feature.

```diff
useHead({
  script: [
      {
-        body: true
+        tagPosition: 'bodyClose'
      }
   ]
})
```

**Use Deprecations Plugin**

```ts
import { createHead } from 'unhead'
import { DeprecationsPlugin } from 'unhead/plugins'

const unhead = createHead({
  plugins: [DeprecationsPlugin]
})
```

## Opt-in Template Params & Tag Alias Sorting

🚦 Impact Level: High

To reduce the bundle size and improve performance, we've moved the template params and tag alias sorting to optional plugins.

If you'd like to continue using these, please opt-in to the plugins.

```ts
import { AliasSortingPlugin, TemplateParamsPlugin } from 'unhead/plugins'

createHead({
  plugins: [TemplateParamsPlugin, AliasSortingPlugin]
})
```

## Vue 2 Support

🚦 Impact Level: Critical

Unhead v2 no longer supports Vue v2. If you're using Vue v2, you will need to lock your dependencies to the latest v1 version of Unhead.

## Promise Input Support

🚦 Impact Level: Medium

If you have promises as input they will no longer be resolved, either await the promise before passing it along or register the optional promises plugin.

**Option 1: Await Promise**

```diff
useHead({
  link: [
    {
-     href: import('~/assets/MyFont.css?url'),
+     href: await import('~/assets/MyFont.css?url'),
      rel: 'stylesheet',
      type: 'text/css'
    }
  ]
})
```

**Option 2: Promise Plugin**

```ts
import { PromisePlugin } from 'unhead/plugins'

const unhead = createHead({
  plugins: [PromisePlugin]
})
```

## Updated `useScript()`{lang="ts"}

🚦 Impact Level: High

**⚠️ Breaking Changes:**

- Script instance is no longer augmented as a proxy and promise
- `script.proxy`{lang="ts"} is rewritten for simpler, more stable behavior
- `stub()`{lang="ts"} and runtime hook `script:instance-fn` are removed

**Replacing promise usage**

If you're using the script as a promise you should instead opt to use the `onLoaded()` functions.

```diff
const script = useScript()

-script.then(() => console.log('loaded')
+script.onLoaded(() => console.log('loaded'))
```

**Replacing proxy usage**

If you're accessing the underlying API directly from the script instance, you will now need to only access it from the `.proxy`.

```diff
const script = useScript('..', {
  use() { return { foo: [] } }
})

-script.foo.push('bar')
+script.proxy.foo.push('bar')
```

**Replacing `stub()`**

If you were using stub for anything you should replace this with either custom `use()` behavior.

```diff
const script = useScript('...', {
-  stub() { return { foo: import.meta.server ? [] : undefined } }
})

+script.proxy = {} // your own implementation
```

## Tag Sorting Updated

🚦 Impact Level: :UBadge{color="success" variant="subtle" size="sm" label="Low"}

An optional [Capo.js](https://rviscomi.github.io/capo.js/) plugin was added to Unhead, in v2 we make this the default sorting behavior.

::warning
As all head tags may be re-ordered this will break any snapshot tests that you have in place and in some rare cases may lead to performance regressions.
::

You can opt-out of Capo.js sorting by providing the option.

```ts
createHead({
  disableCapoSorting: true,
})
```

## Default SSR Tags

🚦 Impact Level: Low

When SSR Unhead will now insert important default tags for you:
- `<meta charset="utf-8">`
- `<meta name="viewport" content="width=device-width, initial-scale=1">`
- `<html lang="en">`

If you were previously relying on these being left empty, you may need to either disable them by using `disableDefaultTags` or insert tags
to override them.

```ts
import { createHead, useHead } from 'unhead/server'

// disable when creating the head instance
const head = createHead({
  disableDefaults: true,
})

useHead(head, {
  htmlAttrs: {
    lang: 'fr'
  }
})
```

## CJS Exports Removed

🚦 Impact Level: Low

CommonJS exports have been removed in favor of ESM only.

```diff
-const { createHead } = require('unhead/client')
+import { createHead } from 'unhead/client'
```

## Deprecated `@unhead/schema`

🚦 Impact Level: Low

The `@unhead/schema` package is now deprecated and will be removed in a future version. You should instead import
the schema from `unhead/types` or `unhead`.

```diff
-import { HeadTag } from '@unhead/schema'
+import { HeadTag } from 'unhead/types'
```
2025-03-28 00:06:28 +08:00
4542944f52 整理
Some checks failed
/ depcheck (push) Successful in 2m24s
/ lint-build-and-check (push) Successful in 2m36s
/ build-and-deploy-to-vercel (push) Successful in 3m13s
/ surge (push) Successful in 2m40s
/ playwright (push) Failing after 8m16s
2025-03-10 13:15:38 +08:00
8733c3e42e chore(deps): 升级 oxlint
All checks were successful
/ depcheck (push) Successful in 2m3s
/ build-and-deploy-to-vercel (push) Successful in 2m59s
/ surge (push) Successful in 3m27s
/ playwright (push) Successful in 2m48s
2025-03-09 23:30:29 +08:00
2b7186ef69 chore: eslint 配置
All checks were successful
/ depcheck (push) Successful in 2m19s
/ playwright (push) Successful in 1m45s
/ surge (push) Successful in 2m48s
/ build-and-deploy-to-vercel (push) Successful in 3m10s
2025-03-05 00:57:59 +08:00
c7047146e4 chore: eslint.config.ts
All checks were successful
/ depcheck (push) Successful in 2m12s
/ build-and-deploy-to-vercel (push) Successful in 2m22s
/ surge (push) Successful in 1m53s
/ playwright (push) Successful in 3m18s
2025-03-01 18:20:11 +08:00
97431f932f feat: add unocss-preset-chinese and update related configurations
All checks were successful
/ depcheck (push) Successful in 1m39s
/ build-and-deploy-to-vercel (push) Successful in 2m7s
/ surge (push) Successful in 1m47s
/ playwright (push) Successful in 3m23s
2025-02-25 00:27:05 +08:00
83117bbf4b feat: 优化菜单项,添加路由名称和标题支持,更新首页元信息
All checks were successful
/ playwright (push) Successful in 1m21s
/ build-and-deploy-to-vercel (push) Successful in 1m24s
/ depcheck (push) Successful in 1m2s
2025-01-08 17:41:36 +08:00
eff30bd6c0 feat: 更新组件样式和结构,优化函数式组件展示
Some checks failed
/ build-and-deploy-to-vercel (push) Successful in 1m14s
/ depcheck (push) Successful in 1m10s
/ playwright (push) Failing after 2m57s
2024-12-27 15:02:29 +08:00
1dac41ce88 feat: 更新 VSCode 设置,更新 package.json 脚本,添加新功能页面和重构组件
Some checks failed
/ playwright (push) Failing after 2m49s
/ depcheck (push) Successful in 1m54s
/ build-and-deploy-to-vercel (push) Successful in 1m37s
2024-12-26 16:25:39 +08:00
49e18facda feat: 更新 VSCode 配置,添加 oxc 插件,调整 eslint 配置,优化构建和类型检查命令
Some checks failed
/ playwright (push) Failing after 2m19s
/ depcheck (push) Successful in 1m30s
/ build-and-deploy-to-vercel (push) Successful in 1m29s
2024-12-25 23:44:18 +08:00
eed0f4f3f4 feat: 重构 AppMenu 组件以动态生成菜单项,更新路由和样式,添加返回按钮
Some checks failed
/ build-and-deploy-to-vercel (push) Successful in 1m33s
/ playwright (push) Failing after 2m43s
/ depcheck (push) Successful in 1m35s
2024-12-25 19:19:21 +08:00
e91582db23 feat: 添加新的 Sakai Vue 布局和样式,重构基础样式,更新 README,调整插件配置
Some checks failed
/ build-and-deploy-to-vercel (push) Successful in 1m41s
/ playwright (push) Failing after 3m26s
/ depcheck (push) Successful in 1m10s
2024-12-25 14:14:55 +08:00
99c8e8d892 feat: 更新路由名称映射,调整插件配置 2024-12-25 11:12:29 +08:00