Nuxt Nation 大会即将到来。加入我们,11 月 12 日至 13 日。

well-known
@zadigetvoltaire/nuxt-well-known

一个使用中间件添加知名 URI 的 Nuxt 模块

npm versionnpm downloadsLicenseNuxt

Nuxt 知名 URI

用于处理 .well-known URI 的 Nuxt (v3.x) 模块,使用中间件实现

参见 https://www.iana.org/assignments/well-known-uris/well-known-uris.xhtml

⚠️ 仅在 SSR 模式下测试(非 SSG 和 CSR)

Nuxt Well-Known 模块已集成到 Nuxt Devtools 中。

支持的知名 URI

  1. security.txt
  2. change-password
  3. 带有内容的动态路由:content-uris

快速设置

  1. @zadigetvoltaire/nuxt-well-known 依赖项添加到您的项目中
# Using pnpm
pnpm add -D @zadigetvoltaire/nuxt-well-known

# Using yarn
yarn add --dev @zadigetvoltaire/nuxt-well-known

# Using npm
npm install --save-dev @zadigetvoltaire/nuxt-well-known
  1. @zadigetvoltaire/nuxt-well-known 添加到 nuxt.config.ts 文件的 modules 部分
export default defineNuxtConfig({
  modules: [
    '@zadigetvoltaire/nuxt-well-known'
  ],
})
  1. nuxtConfig.wellKnownnuxtConfig.runtimeConfig.public.wellKnown 中添加配置

此模块支持两种配置方式

  • 直接在 Nuxt 配置的 wellKnown 键中
  • 在公共 runtimeConfig 中:用于使用环境变量覆盖配置并处理多个环境
export default defineNuxtConfig({
  ...
  wellKnown: {
    devtools: true,
    securityTxt: {
      disabled: false,
      contacts: ['[email protected]'],
      expires: new Date('2025-02-03')
    },
    changePassword: {
      disabled: false,
      redirectTo: 'https://example.com/password-recovery'
    }
  }
  ...
  runtimeConfig: {
    public: {
      wellKnown: {
        devtools: true,
        securityTxt: {
          disabled: false,
          contacts: ['[email protected]'],
          expires: new Date('2025-02-03').toISOString() // ⚠️ in runtime config, `expires` should be a string
        },
        changePassword: {
          disabled: false,
          redirectTo: 'https://example.com/password-recovery'
        }
      }
    }
  }
})

模块选项

interface ModuleOptions {
  /**
   * Enable Nuxt Devtools integration
   *
   * @default true
   */
  devtools?: boolean
  securityTxt?: SecurityTxtOptions,
  changePassword?: ChangePasswordOptions,
  contentUris?: ContentUriOptions[],
}

中间件

security.txt

此中间件将生成一个 security.txt 文件,可在 /.well-known/security.txt URI 下访问。

模型选项

type SecurityTxtOptions = {
  disabled?: boolean;
  contacts: string[]; // https://www.rfc-editor.org/rfc/rfc9116#section-2.5.3
  expires: string | Date; // https://www.rfc-editor.org/rfc/rfc9116#section-2.5.5
  encryption?: string[]; // https://www.rfc-editor.org/rfc/rfc9116#section-2.5.4
  acknowledgments?: string[]; // https://www.rfc-editor.org/rfc/rfc9116#section-2.5.1
  preferredLanguages?: string[]; // https://www.rfc-editor.org/rfc/rfc9116#section-2.5.8
  canonical?: string[]; // https://www.rfc-editor.org/rfc/rfc9116#section-2.5.2
  policy?: string[]; // https://www.rfc-editor.org/rfc/rfc9116#section-2.5.7
  hiring?: string[]; // https://www.rfc-editor.org/rfc/rfc9116#section-2.5.6
}

change-password

此中间件将把 /.well-known/change-password 的请求重定向到配置的目标 URL。

type ChangePasswordOptions = {
  disabled?: boolean;
  redirectTo: string;
}

content-uris

使用此中间件,您可以生成包含内容的 URI

type ContentUriOptions = {
  disabled?: boolean;
  path: string;
  content: string;
}

示例

// nuxt.config.ts
export default defineNuxtConfig({
  modules: [
    '@zadigetvoltaire/nuxt-well-known',
  ],
  wellKnown: {
    contentUris: [
      { path: 'apple-developer-merchantid-domain-association', content: 'merchantid' },
      { path: 'content-uri.txt', content: 'content-uri' }
    ]
  }
})

将渲染

  • https://example.com/.well-known/apple-developer-merchantid-domain-association --> merchantid
  • https://example.com/.well-known/content-uri.txt --> content-uri

就是这样!您现在可以在您的 Nuxt 应用中使用 Nuxt Well-Known ✨

贡献

# Install dependencies, prepare apps & run dev server
make start

# Run dev server
pnpm dev

# Develop with playground, with bundled client ui
pnpm play:prod

# Run ESLint
pnpm lint

# Run Vitest
pnpm test
pnpm test:watch

发布新版本

  1. 执行发布命令

⚠ 此命令仅应在主分支上执行

此命令将

  • 生成 CHANGELOG.md 并将其与发布提交一起推送
  • 提升包版本
  • 创建并推送新标签
  • 创建一个 GitHub 发布以触发库发布管道
pnpm release

© Zadig&Voltaire 是 ZV FRANCE 的注册商标