From 9e3336b42c5a582065994c1a48ecb6aed51cfada Mon Sep 17 00:00:00 2001 From: Hossein Mehrabi Date: Thu, 31 Aug 2023 16:14:23 +0330 Subject: [PATCH] fix: disable webhook on Vercel --- src/pages/api/webhook.ts | 5 ++++- src/services/unbody/unbody.service.ts | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/pages/api/webhook.ts b/src/pages/api/webhook.ts index 09fb151..e3e5efa 100644 --- a/src/pages/api/webhook.ts +++ b/src/pages/api/webhook.ts @@ -2,6 +2,7 @@ import { readFile, writeFile } from 'fs/promises' import type { NextApiRequest, NextApiResponse } from 'next' import path from 'path' +const IS_VERCEL = process.env.VERCEL === '1' const WEBHOOK_DATA_PATH = path.join(__dirname, '../../webhook_data.json') const TOKEN = process.env.REVALIDATE_WEBHOOK_TOKEN || @@ -18,7 +19,7 @@ export const getWebhookData = async (): Promise => JSON.parse((await readFile(WEBHOOK_DATA_PATH, 'utf-8')) || '{}') let initialized = false -if (!initialized) writeWebhookData({ lastUpdate: +new Date() }) +if (IS_VERCEL && !initialized) writeWebhookData({ lastUpdate: +new Date() }) export default async function handler( req: NextApiRequest, @@ -28,6 +29,8 @@ export default async function handler( query: { token = '' }, } = req + if (IS_VERCEL) return res.status(404).json({ message: 'Not found!' }) + if (token !== TOKEN) return res.status(401).json({ message: 'Invalid token' }) writeWebhookData({ diff --git a/src/services/unbody/unbody.service.ts b/src/services/unbody/unbody.service.ts index b63255f..4e48b28 100644 --- a/src/services/unbody/unbody.service.ts +++ b/src/services/unbody/unbody.service.ts @@ -23,6 +23,8 @@ import { import { unbodyDataTypes } from './dataTypes' import { UnbodyHelpers } from './unbody.helpers' +const isVercel = process.env.VERCEL === '1' + const articleDocument = unbodyDataTypes.get({ objectType: 'GoogleDoc', classes: ['article', 'document'], @@ -107,7 +109,8 @@ export class UnbodyService { this.loadInitialData(true) - if (process.env.NODE_ENV !== 'development') this.checkForUpdates() + if (!isVercel && process.env.NODE_ENV !== 'development') + this.checkForUpdates() } private checkForUpdates = async () => {