fix: disable webhook on Vercel

This commit is contained in:
Hossein Mehrabi 2023-08-31 16:14:23 +03:30
parent 7143946418
commit 9e3336b42c
No known key found for this signature in database
GPG Key ID: 45C04964191AFAA1
2 changed files with 8 additions and 2 deletions

View File

@ -2,6 +2,7 @@ import { readFile, writeFile } from 'fs/promises'
import type { NextApiRequest, NextApiResponse } from 'next' import type { NextApiRequest, NextApiResponse } from 'next'
import path from 'path' import path from 'path'
const IS_VERCEL = process.env.VERCEL === '1'
const WEBHOOK_DATA_PATH = path.join(__dirname, '../../webhook_data.json') const WEBHOOK_DATA_PATH = path.join(__dirname, '../../webhook_data.json')
const TOKEN = const TOKEN =
process.env.REVALIDATE_WEBHOOK_TOKEN || process.env.REVALIDATE_WEBHOOK_TOKEN ||
@ -18,7 +19,7 @@ export const getWebhookData = async (): Promise<WebhookData> =>
JSON.parse((await readFile(WEBHOOK_DATA_PATH, 'utf-8')) || '{}') JSON.parse((await readFile(WEBHOOK_DATA_PATH, 'utf-8')) || '{}')
let initialized = false let initialized = false
if (!initialized) writeWebhookData({ lastUpdate: +new Date() }) if (IS_VERCEL && !initialized) writeWebhookData({ lastUpdate: +new Date() })
export default async function handler( export default async function handler(
req: NextApiRequest, req: NextApiRequest,
@ -28,6 +29,8 @@ export default async function handler(
query: { token = '' }, query: { token = '' },
} = req } = req
if (IS_VERCEL) return res.status(404).json({ message: 'Not found!' })
if (token !== TOKEN) return res.status(401).json({ message: 'Invalid token' }) if (token !== TOKEN) return res.status(401).json({ message: 'Invalid token' })
writeWebhookData({ writeWebhookData({

View File

@ -23,6 +23,8 @@ import {
import { unbodyDataTypes } from './dataTypes' import { unbodyDataTypes } from './dataTypes'
import { UnbodyHelpers } from './unbody.helpers' import { UnbodyHelpers } from './unbody.helpers'
const isVercel = process.env.VERCEL === '1'
const articleDocument = unbodyDataTypes.get({ const articleDocument = unbodyDataTypes.get({
objectType: 'GoogleDoc', objectType: 'GoogleDoc',
classes: ['article', 'document'], classes: ['article', 'document'],
@ -107,7 +109,8 @@ export class UnbodyService {
this.loadInitialData(true) this.loadInitialData(true)
if (process.env.NODE_ENV !== 'development') this.checkForUpdates() if (!isVercel && process.env.NODE_ENV !== 'development')
this.checkForUpdates()
} }
private checkForUpdates = async () => { private checkForUpdates = async () => {