From 970d1ac3ed3cd589f3e435be50b4a48d7ec49cf7 Mon Sep 17 00:00:00 2001 From: typist Date: Thu, 30 Oct 2025 09:43:50 +0800 Subject: [PATCH] feat: conditionally inject Cloudflare Analytics script - Updated index.html to include a placeholder for Cloudflare Web Analytics, which is now only injected in production mode. - Modified vite.config.ts to implement an HTML transform plugin that dynamically adds the Cloudflare script based on the environment. --- index.html | 4 ++-- vite.config.ts | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/index.html b/index.html index f67a837..bde143c 100644 --- a/index.html +++ b/index.html @@ -65,8 +65,8 @@
- - + + diff --git a/vite.config.ts b/vite.config.ts index 3c9dcf8..db45ab9 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -5,10 +5,20 @@ import tailwindcss from "@tailwindcss/vite" import { VitePWA } from 'vite-plugin-pwa' // https://vite.dev/config/ -export default defineConfig({ +export default defineConfig(({ mode }) => ({ plugins: [ react(), tailwindcss(), + // HTML 替换插件 - 仅在生产环境注入 Cloudflare Analytics + { + name: 'html-transform', + transformIndexHtml(html) { + const cloudflareScript = mode === 'production' + ? `` + : ''; + return html.replace('', cloudflareScript); + } + }, VitePWA({ registerType: 'autoUpdate', includeAssets: ['lite.svg', 'robots.txt', 'sitemap.xml'], @@ -130,4 +140,4 @@ export default defineConfig({ }, chunkSizeWarningLimit: 500, }, -}) +}))