Skip to content

Commit

Permalink
Merge pull request #332 from maretol/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
maretol authored Feb 24, 2025
2 parents 94c5da3 + 306f21d commit df18a83
Show file tree
Hide file tree
Showing 9 changed files with 741 additions and 1,353 deletions.
2 changes: 1 addition & 1 deletion cms-cache-purger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "CMSのWebhookを受け取り影響のあるキャッシュを破棄する処理",
"main": "index.js",
"devDependencies": {
"@cloudflare/workers-types": "^4.20250204.0",
"@cloudflare/workers-types": "^4.20250214.0",
"@types/node": "^22.13.4",
"cms-cache-key-gen": "^1.0.0"
},
Expand Down
13 changes: 12 additions & 1 deletion cms-cache-purger/src/cms_webhook_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,16 @@ export type WebhookPayload = {
api: string
id: string
type: string
contents: any
contents: {
old: Content | null
new: Content
}
}

export type Content = {
id: string
status: ('PUBLISH' | 'DRAFT' | 'CLOSED')[]
draftKey: string | null
publishValue: any
draftValue: any
}
34 changes: 26 additions & 8 deletions cms-cache-purger/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type Request as WorkerRequest, type ExecutionContext, type KVNamespace } from '@cloudflare/workers-types'
import crypto from 'node:crypto'
import { WebhookPayload } from './cms_webhook_types'
import { Content, WebhookPayload } from './cms_webhook_types'
import { generateContentKey, generateInfoKey, generateTagsKey } from 'cms-cache-key-gen'

export interface Env {
Expand Down Expand Up @@ -41,14 +41,25 @@ export default {
if (bodyJSON.type === 'new') {
// ブログのメインコンテンツに新規作成があった場合
// contentsのキャッシュを削除する
console.log('start deleteContentsCache')
await deleteContentsCache(env)
if (bodyJSON.contents.new.status.includes('PUBLISH')) {
console.log('start deleteContentsCache')
await deleteContentsCache(env)
} else {
console.log('status is not PUBLISH')
}
} else if (bodyJSON.type === 'edit') {
// ブログのメインコンテンツに編集があった場合
// 対象のIDのコンテンツのキャッシュを削除する
console.log('start deleteContentCache')
console.log('id: ' + bodyJSON.id)
await deleteContentCache(env, bodyJSON.id)
if (isDraftToPublish(bodyJSON.contents.old, bodyJSON.contents.new)) {
// 下書きから公開に変更された場合
// contentsのキャッシュを削除する
console.log('start deleteContentsCache')
await deleteContentsCache(env)
} else {
// ブログのメインコンテンツに編集があった場合
// 対象のIDのコンテンツのキャッシュを削除する
console.log('start deleteContentCache')
console.log('id: ' + bodyJSON.id)
await deleteContentCache(env, bodyJSON.id)
}
} else if (bodyJSON.type === 'delete') {
// ブログのメインコンテンツに削除があった場合
// contentsのキャッシュと対象のIDのコンテンツのキャッシュを削除する
Expand Down Expand Up @@ -104,3 +115,10 @@ async function deleteCache(env: Env, cacheKey: string) {
}
await env.CMS_CACHE.delete(cacheKey)
}

function isDraftToPublish(old: Content | null, newContent: Content): boolean {
if (!old) {
return false
}
return old.status.includes('DRAFT') && newContent.status.includes('PUBLISH')
}
Loading

0 comments on commit df18a83

Please sign in to comment.