fix: add manifest to purge cache
Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
import crypto from "node:crypto"
|
||||
|
||||
import type { VercelRequest, VercelResponse } from "@vercel/node"
|
||||
import getRawBody from "raw-body"
|
||||
|
||||
export default async function handler(request: VercelRequest, response: VercelResponse) {
|
||||
const { WEBHOOK_SECRET: INTEGRATION_SECRET } = process.env
|
||||
|
||||
if (typeof INTEGRATION_SECRET != "string") {
|
||||
throw new TypeError("No integration secret found")
|
||||
}
|
||||
|
||||
const rawBody = await getRawBody(request)
|
||||
const bodySignature = sha1(rawBody, INTEGRATION_SECRET)
|
||||
|
||||
if (bodySignature !== request.headers["x-vercel-signature"]) {
|
||||
return response.status(403).json({
|
||||
code: "invalid_signature",
|
||||
error: "signature didn't match",
|
||||
})
|
||||
}
|
||||
|
||||
const json = JSON.parse(rawBody.toString("utf-8"))
|
||||
|
||||
switch (json.type) {
|
||||
// https://vercel.com/docs/observability/webhooks-overview/webhooks-api#deployment.succeeded
|
||||
case "deployment.succeeded": {
|
||||
const { target } = json.payload || json.data
|
||||
|
||||
if (target === "production") {
|
||||
purgeCloudflareCache()
|
||||
}
|
||||
}
|
||||
// ...
|
||||
}
|
||||
|
||||
return response.status(200).end("OK")
|
||||
}
|
||||
|
||||
function sha1(data: Buffer, secret: string): string {
|
||||
return crypto.createHmac("sha1", secret).update(data).digest("hex")
|
||||
}
|
||||
|
||||
export const config = {
|
||||
api: {
|
||||
bodyParser: false,
|
||||
},
|
||||
}
|
||||
|
||||
async function purgeCloudflareCache() {
|
||||
const { CF_TOKEN, CF_ZONE_ID } = process.env
|
||||
|
||||
if (typeof CF_TOKEN !== "string" || typeof CF_ZONE_ID !== "string") {
|
||||
throw new TypeError("No Cloudflare token or zone ID found")
|
||||
}
|
||||
|
||||
const apiUrl = `https://api.cloudflare.com/client/v4/zones/${CF_ZONE_ID}/purge_cache`
|
||||
|
||||
const manifestPath = await fetch(
|
||||
`https://app.follow.is/assets/manifest.txt?t=${Date.now()}`,
|
||||
).then((res) => res.text())
|
||||
|
||||
const allPath = manifestPath.split("\n").map((path) => `https://app.follow.is/${path}`)
|
||||
|
||||
// Function to delay execution
|
||||
const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
|
||||
|
||||
const taskPromise = [] as Promise<Response>[]
|
||||
// Batch processing
|
||||
for (let i = 0; i < allPath.length; i += 30) {
|
||||
const batch = allPath.slice(i, i + 30)
|
||||
|
||||
const r = fetch(apiUrl, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: CF_TOKEN,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
files: batch,
|
||||
}),
|
||||
})
|
||||
|
||||
taskPromise.push(r)
|
||||
|
||||
// Delay for 0.5 seconds between batches
|
||||
if (i + 30 < allPath.length) {
|
||||
await delay(500)
|
||||
}
|
||||
}
|
||||
|
||||
const result = await Promise.allSettled(taskPromise)
|
||||
console.info(`Success: ${result.filter((r) => r.status === "fulfilled").length}`)
|
||||
console.info(`Failed: ${result.filter((r) => r.status === "rejected").length}`)
|
||||
}
|
||||
@@ -63,7 +63,12 @@ export const viteRenderBaseConfig = {
|
||||
electron: false,
|
||||
},
|
||||
sourcemaps: {
|
||||
filesToDeleteAfterUpload: ["out/web/assets/*.js.map", "dist/renderer/assets/*.js.map"],
|
||||
filesToDeleteAfterUpload: [
|
||||
"out/web/assets/*.js.map",
|
||||
"out/web/vendor/*.js.map",
|
||||
"dist/renderer/assets/*.js.map",
|
||||
"dist/renderer/vendor/*.css.map",
|
||||
],
|
||||
},
|
||||
}),
|
||||
|
||||
|
||||
@@ -68,6 +68,7 @@
|
||||
"@types/node": "^22.8.7",
|
||||
"@types/react": "^18.3.12",
|
||||
"@types/react-dom": "^18.3.1",
|
||||
"@vercel/node": "3.2.24",
|
||||
"@vitejs/plugin-legacy": "5.4.3",
|
||||
"@vitejs/plugin-react": "^4.3.3",
|
||||
"cross-env": "7.0.3",
|
||||
@@ -90,6 +91,7 @@
|
||||
"postcss": "8.4.47",
|
||||
"postcss-js": "4.0.1",
|
||||
"prettier": "3.3.3",
|
||||
"raw-body": "3.0.0",
|
||||
"react": "^18.3.1",
|
||||
"react-dom": "^18.3.1",
|
||||
"rimraf": "6.0.1",
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
import fs from "node:fs"
|
||||
import path from "node:path"
|
||||
|
||||
import type { Plugin } from "vite"
|
||||
|
||||
export default function manifestPlugin(): Plugin {
|
||||
let config
|
||||
|
||||
return {
|
||||
name: "manifest",
|
||||
enforce: "post",
|
||||
configResolved(resolvedConfig) {
|
||||
config = resolvedConfig
|
||||
},
|
||||
generateBundle(_options, bundle) {
|
||||
const outputDir = config.build.outDir
|
||||
const assetsDir = path.join(outputDir, "assets")
|
||||
const outputPath = path.join(assetsDir, "manifest.txt")
|
||||
|
||||
if (!fs.existsSync(assetsDir)) {
|
||||
fs.mkdirSync(assetsDir, { recursive: true })
|
||||
}
|
||||
|
||||
const fileStream = fs.createWriteStream(outputPath)
|
||||
|
||||
for (const fileName in bundle) {
|
||||
fileStream.write(`${fileName}\n`)
|
||||
}
|
||||
|
||||
fileStream.end()
|
||||
},
|
||||
}
|
||||
}
|
||||
Generated
+822
-30
File diff suppressed because it is too large
Load Diff
@@ -11,6 +11,7 @@ import { viteRenderBaseConfig } from "./configs/vite.render.config"
|
||||
import type { env as EnvType } from "./packages/shared/src/env"
|
||||
import { createDependencyChunksPlugin } from "./plugins/vite/deps"
|
||||
import { htmlInjectPlugin } from "./plugins/vite/html-inject"
|
||||
import manifestPlugin from "./plugins/vite/manifest"
|
||||
import { createPlatformSpecificImportPlugin } from "./plugins/vite/specific-import"
|
||||
|
||||
const __dirname = fileURLToPath(new URL(".", import.meta.url))
|
||||
@@ -147,6 +148,7 @@ export default ({ mode }) => {
|
||||
]),
|
||||
|
||||
createPlatformSpecificImportPlugin(false),
|
||||
manifestPlugin(),
|
||||
],
|
||||
|
||||
define: {
|
||||
|
||||
Reference in New Issue
Block a user