Skip to content

Updater 更新插件

Updater 插件支持应用自动检查更新和下载安装。

配置更新源

json
// src-tauri/tauri.conf.json
{
  "plugins": {
    "updater": {
      "endpoints": [
        "https://your-server.com/update/{{target}}/{{arch}}/{{current_version}}"
      ],
      "pubkey": "你的公钥"
    }
  }
}

前端检查更新

typescript
import { check } from '@tauri-apps/plugin-updater'

async function checkForUpdate() {
  const update = await check()
  if (update) {
    console.log(`发现新版本: ${update.version}`)
    // 下载并安装
    await update.downloadAndInstall()
    // 重启应用
    await relaunch()
  }
}

封装到 API 层

typescript
// src/lib/api/updater.ts
import { check } from '@tauri-apps/plugin-updater'
import { relaunch } from '@tauri-apps/plugin-process'

export async function checkUpdate() {
  const update = await check()
  if (!update) return null
  return {
    version: update.version,
    body: update.body,
    install: async () => {
      await update.downloadAndInstall()
      await relaunch()
    },
  }
}

在页面中使用

tsx
import { checkUpdate } from '@/lib/api'
import { Button, Modal } from 'antd'

function UpdateChecker() {
  const handleCheck = async () => {
    const update = await checkUpdate()
    if (update) {
      Modal.confirm({
        title: `发现新版本 ${update.version}`,
        content: update.body,
        onOk: () => update.install(),
      })
    } else {
      message.info('已是最新版本')
    }
  }

  return <Button onClick={handleCheck}>检查更新</Button>
}

基于 Tauri 2.x 构建的企业级桌面应用开发框架