From 7919b0d3a5021fbfbf86aeded9112961ebf9757c Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 17 Oct 2025 20:39:48 +0800 Subject: [PATCH] chore: use cf workers (#1069) --- .github/workflows/cloudflare-preview.yml | 2 +- .github/workflows/deploy.yml | 2 +- apps/web/package.json | 4 ++-- apps/web/src/components/editor/UploadImgDialog.vue | 6 +++--- apps/web/src/utils/file.ts | 9 +++++---- apps/web/vite.config.ts | 12 +++++------- 6 files changed, 17 insertions(+), 18 deletions(-) diff --git a/.github/workflows/cloudflare-preview.yml b/.github/workflows/cloudflare-preview.yml index 5e6aee66..ed78fa84 100644 --- a/.github/workflows/cloudflare-preview.yml +++ b/.github/workflows/cloudflare-preview.yml @@ -39,7 +39,7 @@ jobs: - name: Build for Cloudflare Workers run: pnpm web build:h5-netlify env: - CF_PAGES: 1 + CF_WORKERS: 1 - name: Deploy to Cloudflare Workers id: deploy diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 7747d126..64ffbc64 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -92,7 +92,7 @@ jobs: - name: Build for Cloudflare Workers run: pnpm web build:h5-netlify env: - CF_PAGES: 1 + CF_WORKERS: 1 - name: Deploy to Cloudflare Workers uses: cloudflare/wrangler-action@v3 diff --git a/apps/web/package.json b/apps/web/package.json index 0b7e8d77..e2806efc 100644 --- a/apps/web/package.json +++ b/apps/web/package.json @@ -17,8 +17,8 @@ "build:analyze": "cross-env ANALYZE=true vite build", "compile:extension": "pnpm --prefix ./src/extension run compile", "preview": "pnpm run build && vite preview", - "wrangler:dev": "cross-env CF_PAGES=1 vite --host", - "wrangler:deploy": "cross-env CF_PAGES=1 pnpm run build:h5-netlify && wrangler deploy", + "wrangler:dev": "cross-env CF_WORKERS=1 vite --host", + "wrangler:deploy": "cross-env CF_WORKERS=1 pnpm run build:h5-netlify && wrangler deploy", "release:cli": "node ./scripts/release.js", "ext:dev": "wxt", "ext:zip": "wxt zip", diff --git a/apps/web/src/components/editor/UploadImgDialog.vue b/apps/web/src/components/editor/UploadImgDialog.vue index 77178ede..d4693d9a 100644 --- a/apps/web/src/components/editor/UploadImgDialog.vue +++ b/apps/web/src/components/editor/UploadImgDialog.vue @@ -159,15 +159,15 @@ function telegramSubmit(values: any) { // 当前是否为网页(http/https 协议) const isWebsite = window.location.protocol.startsWith(`http`) -// Cloudflare Pages 环境 -const isCfPage = import.meta.env.CF_PAGES === `1` +// Cloudflare Workers 环境 +const isCfWorkers = import.meta.env.CF_WORKERS === `1` // 插件模式运行(如 chrome-extension://) const isPluginMode = !isWebsite // 是否需要填写 proxyOrigin(只在 非插件 且 非CF页面 时需要) const isProxyRequired = computed(() => { - return !isPluginMode && !isCfPage + return !isPluginMode && !isCfWorkers }) const mpPlaceholder = computed(() => { diff --git a/apps/web/src/utils/file.ts b/apps/web/src/utils/file.ts index db89adfa..dd299db8 100644 --- a/apps/web/src/utils/file.ts +++ b/apps/web/src/utils/file.ts @@ -333,14 +333,15 @@ async function getMpToken(appID: string, appsecret: string, proxyOrigin: string) } return `` } -// Cloudflare Pages 环境 -const isCfPage = import.meta.env.CF_PAGES === `1` +// Cloudflare Workers 环境 +const isCfWorkers = import.meta.env.CF_WORKERS === `1` + async function mpFileUpload(file: File) { let { appID, appsecret, proxyOrigin } = JSON.parse( localStorage.getItem(`mpConfig`)!, ) - // 未填写代理域名且是cfpages环境 - if (!proxyOrigin && isCfPage) { + // 未填写代理域名且是 Cloudflare Workers 环境时,使用当前域名作为代理域名 + if (!proxyOrigin && isCfWorkers) { proxyOrigin = window.location.origin } const access_token = await getMpToken(appID, appsecret, proxyOrigin) diff --git a/apps/web/vite.config.ts b/apps/web/vite.config.ts index eea0ef21..c6d6529f 100644 --- a/apps/web/vite.config.ts +++ b/apps/web/vite.config.ts @@ -13,14 +13,12 @@ import { VitePWA } from 'vite-plugin-pwa' import { VitePluginRadar } from 'vite-plugin-radar' import vueDevTools from 'vite-plugin-vue-devtools' +const isNetlify = process.env.SERVER_ENV === `NETLIFY` const isUTools = process.env.SERVER_ENV === `UTOOLS` -const isCfWorkers = process.env.CF_PAGES === `1` -const base - = process.env.SERVER_ENV === `NETLIFY` || isCfWorkers - ? `/` - : isUTools - ? `./` - : `/md/` +const isCfWorkers = process.env.CF_WORKERS === `1` +const isCfPages = process.env.CF_PAGES === `1` + +const base = isNetlify || isCfWorkers || isCfPages ? `/` : isUTools ? `./` : `/md/` export default defineConfig(({ mode }) => { const env = loadEnv(mode, process.cwd())