fix: ensure Strapi service clears cache upon webhook calls

This commit is contained in:
Hossein Mehrabi 2024-02-01 14:46:49 +03:30 committed by Alexis Pentori
parent c218b89da4
commit 94a4613d72
No known key found for this signature in database
GPG Key ID: 65250D2801E47A10
2 changed files with 9 additions and 3 deletions

View File

@ -19,7 +19,10 @@ export const getWebhookData = async (): Promise<WebhookData> =>
JSON.parse((await readFile(WEBHOOK_DATA_PATH, 'utf-8')) || '{}')
let initialized = false
if (!IS_VERCEL && !initialized) writeWebhookData({ lastUpdate: +new Date() })
if (!IS_VERCEL && !initialized) {
writeWebhookData({ lastUpdate: +new Date() })
initialized = true
}
export default async function handler(
req: NextApiRequest,

View File

@ -72,7 +72,7 @@ export class StrapiService {
},
})
if (!isVercel() && process.env.NODE_ENV !== 'development') {
if (!isVercel()) {
this.checkForUpdate()
} else {
setInterval(this.clearCache.bind(this), 5000)
@ -85,12 +85,15 @@ export class StrapiService {
checkForUpdate = async () => {
const { lastUpdate } = await getWebhookData()
if (this.lastUpdate < lastUpdate) {
await this.clearCache()
this.lastUpdate = lastUpdate
}
setTimeout(() => this.checkForUpdate.bind(this), 5000)
setTimeout(() => {
this.checkForUpdate()
}, 5000)
}
handleResponse = <T>(