透過 100 多個技巧學習 Nuxt!

well-known
@zadigetvoltaire/nuxt-well-known

一個 Nuxt 模組,使用中介軟體新增 well-known URI

npm versionnpm downloadsLicenseNuxt

Nuxt Well-Known

Nuxt (v3.x) 模組,用於使用中介軟體處理 .well-known URI

請參閱 https://www.iana.org/assignments/well-known-uris/well-known-uris.xhtml

⚠️ 僅在 SSR 模式下測試 (非 SSG 和 CSR)

Nuxt Well-Known 模組已與 Nuxt Devtools 整合。

支援的 Well-Known 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.tsmodules 區段
export default defineNuxtConfig({
  modules: [
    '@zadigetvoltaire/nuxt-well-known'
  ],
})
  1. nuxtConfig.wellKnownnuxtConfig.runtimeConfig.public.wellKnown 中新增設定

此模組支援兩種設定方式

  • 直接在 Nuxt 設定的 wellKnown 鍵中
  • 在 public runtimeConfig 中:對於使用環境變數覆寫設定和處理多個環境很有用
export default defineNuxtConfig({
  ...
  wellKnown: {
    devtools: true,
    securityTxt: {
      disabled: false,
      contacts: ['me@example.com'],
      expires: new Date('2025-02-03')
    },
    changePassword: {
      disabled: false,
      redirectTo: 'https://example.com/password-recovery'
    }
  }
  ...
  runtimeConfig: {
    public: {
      wellKnown: {
        devtools: true,
        securityTxt: {
          disabled: false,
          contacts: ['me@example.com'],
          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. 執行發布指令

⚠ 此指令應僅在 main 分支上執行

此指令將會

  • 產生 CHANGELOG.md 並以發布提交推送
  • 更新套件版本
  • 建立並推送新的標籤
  • 建立 GitHub 發布以觸發程式庫發布管線
pnpm release

© Zadig&Voltaire 是 ZV FRANCE 的註冊商標