Commit Graph
55 Commits
Author SHA1 Message Date
严浩 695e583136 feat: 添加对 .md 文件的类型声明 2025-05-02 23:10:38 +08:00
严浩 aacca31384 feat: 更新随机图片选择逻辑,支持随机选择2或3 2025-04-20 22:11:51 +08:00
严浩 5f9eeddc77 feat: 添加随机图片功能 2025-04-20 22:09:08 +08:00
严浩 f4e248a2b1 feat: vite-assets 2025-04-20 22:06:49 +08:00
mini2024 7abc6a8f65 fix: 添加 @ts-nocheck 注释以禁用 TypeScript 检查 2025-04-07 21:49:41 +08:00
mini2024 9e8affc52d chore: 更新项目配置并添加 p5.js 粒子演示 2025-04-06 21:38:06 +08:00
mini2024 aecfd22055 chore(husky): 更新钩子脚本注释和 emoji [no ci] 2025-04-06 19:49:41 +08:00
mini2024 bf68819b4a feat: 适配 FlowbiteSidebar 页面 iPhone 横屏安全区域 2025-04-06 19:18:00 +08:00
mini2024 3cad491aac Merge remote-tracking branch 'origin/main' 2025-04-06 03:03:17 +08:00
mini2024 f77b63cb8c chore: 移除 @unocss/preset-rem-to-px 依赖 2025-04-06 03:03:07 +08:00
mini2024 5e652561a1 feat: gitattributes [no ci] 2025-04-05 21:18:02 +08:00
mini2024 4ee2048ae4 fix: 在 lint-staged 中添加 oxlint 修复命令 2025-04-05 16:31:42 +08:00
mini2024 f0c6290e89 chore: 添加 Vercel 工作流文档链接 [no ci] 2025-04-05 15:43:22 +08:00
mini2024 cc3567d022 fix: 更新 Vercel 工作流 [no ci] 2025-04-03 23:53:29 +08:00
mini2024 5b28d93122 fix: 在 Vercel 工作流中启用 Vue 生产环境开发工具 2025-04-03 22:37:28 +08:00
mini2024 fbff6e0542 Merge remote-tracking branch 'origin/main'
# Conflicts:
#	.github/workflows/playwright.yaml
#	.github/workflows/vercel.yaml
2025-04-03 00:47:24 +08:00
mini2024 3d20ff6e83 feat: 优化 GitHub Actions 工作流中的步骤名称以增强可读性 [skip ci] 2025-04-03 00:39:18 +08:00
mini2024 bcec165c2f feat: 为 GitHub Actions 步骤添加名称以增强可读性 2025-04-03 00:27:03 +08:00
mini2024 5ba87212b9 feat: 添加 oxlint 配置文件并更新 lint 脚本 2025-03-31 01:41:17 +08:00
mini2024 3adafb58d5 chore: 移除不必要的 oxlint lint 脚本 2025-03-31 00:57:16 +08:00
mini2024 2fd7f2fcbc feat: 更新配置和依赖项,启用 oxc 插件,移除 cdnImport 插件 2025-03-31 00:55:50 +08:00
mini2024 0cc1730b2c feat: 更新依赖 2025-03-31 00:38:50 +08:00
mini2024 8d0ed93ee0 feat: 添加 InspiraUI 组件 2025-03-30 23:45:01 +08:00
mini2024 05d6a71da8 feat: <Isolate /> 2025-03-30 22:58:32 +08:00
mini2024 0cf585be90 手动修复 2025-03-28 00:43:59 +08:00
mini2024 ba91e845d5 帮我把 @unhead/vue 的版本升级到 v2.0.2
---
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
mini2024 ea50699dc1 feat: 移动 depcheck 工作流到 lint.yaml,删除旧的 depcheck.yaml 文件 2025-03-27 22:55:36 +08:00
mini2024 2482608a2e feat: 添加 FlowbiteSidebar 组件及路由定义 2025-03-25 01:03:03 +08:00
mini2024 295f2d0c95 feat: 添加主题配置支持并优化 UI 组件页面的暗黑模式样式 2025-03-24 01:19:00 +08:00
mini2024 cf95ef7e8e feat: 添加输入组件并更新 UI 组件页面布局 2025-03-24 00:54:43 +08:00
mini2024 22d3eedea0 feat: UIComponentsComponents 2025-03-24 00:22:24 +08:00
mini2024 e42241ddc5 feat: unocss-preset-shadcn 2025-03-23 22:10:38 +08:00
mini2024 e66dc097f7 chore: 更新 Vercel 命令为 pnpm dlx 以提高执行效率 2025-03-23 02:19:13 +08:00
mini2024 e83f6c9247 docs(vite.config.plugin.archiver): 添加相关参考链接
在vite.config.plugin.archiver.ts文件中添加了两个参考链接,以便开发者更好地理解插件的实现背景和用途。
2025-03-23 00:55:18 +08:00
mini2024 051244dde6 refactor(archiver): 优化打包插件逻辑并支持更多压缩格式
重构打包插件以支持多种压缩格式(zip, tar, tgz),并增加时间戳选项。同时,将插件调用移至VS Code终端检测逻辑中,确保仅在VS Code环境中执行
2025-03-23 00:46:56 +08:00
mini2024 e4f2ad3110 feat: 添加构建后自动打包dist目录为zip文件的功能
引入archiver依赖,新增vite插件viteArchiverPlugin,用于在构建完成后自动将dist目录打包成zip文件。同时更新.gitignore和package.json以支持该功能。
2025-03-23 00:29:19 +08:00
mini2024 6883cd2dfe feat: 禁用 vite-plugin-purgecss 2025-03-22 23:58:21 +08:00
mini2024 bee73670b7 feat: 更新 Playwright 工作流,增加以KB为单位的构建大小统计 2025-03-22 23:54:24 +08:00
mini2024 d23fcd72c7 feat: 更新 Playwright 工作流以计算构建大小;添加 vite-plugin-purgecss-updated-v5 插件 2025-03-22 23:50:24 +08:00
mini2024 9e6d526ada feat: 添加构建大小计算步骤;更新依赖项,新增 vite-plugin-singlefile 插件 2025-03-22 23:44:06 +08:00
mini2024 974f05719e feat: 添加 StyleLayer 组件,展示 @layer 优先级规则及样式示例;新增 style.page.vue 页面 2025-03-22 18:29:23 +08:00
mini2024 01adfcd908 chore: 更新 package.json,优化依赖命令并添加 knip 工具 2025-03-16 19:36:49 +08:00
mini2024 eafe01fcb3 feat: 更新 cesium-helper 的 README.md,添加 vue-vite-cesium-demo 链接
[skip ci]
2025-03-16 17:36:59 +08:00
mini2024 74631308f6 feat: 更新 cesium-helper 的 README.md,添加新的链接
[skip ci]
2025-03-16 17:24:56 +08:00
mini2024 8733c3e42e chore(deps): 升级 oxlint 2025-03-09 23:30:29 +08:00
mini2024 2b7186ef69 chore: eslint 配置 2025-03-05 00:57:59 +08:00
mini2024 feb7659b75 feat: 在轨道生成演示中添加确认提示,增强用户交互体验 2025-03-05 00:48:02 +08:00
mini2024 dc318c04e3 feat: 添加 Cesium 相关功能和配置,更新样式及文档 2025-03-05 00:42:30 +08:00
mini2024 49a7ef0dae fix: ERR_PNPM_LOCKFILE_CONFIG_MISMATCH 2025-03-02 16:48:49 +08:00
mini2024 41a036e60f chore: 更新依赖项版本 2025-03-01 18:30:11 +08:00
mini2024 c7047146e4 chore: eslint.config.ts 2025-03-01 18:20:11 +08:00
mini2024 5ada043ee9 feat: 添加 APopconfirmButton 组件并更新 index.page.vue 以集成确认删除功能 2025-02-28 00:35:13 +08:00
mini2024 97431f932f feat: add unocss-preset-chinese and update related configurations 2025-02-25 00:27:05 +08:00
mini2024 14fdb7ef1f chore(ci): reorganize CI workflows and add depcheck and playwright jobs 2025-01-24 00:28:13 +08:00
mini2024 b0cd8b7c61 feat: 添加 eslint-plugin-perfectionist 插件,优化导入排序规则 2025-01-21 23:05:11 +08:00