|
| 1 | +// Next.js API route support: https://nextjs.org/docs/api-routes/introduction |
| 2 | +import { |
| 3 | + getAllArticles, |
| 4 | + getAllPages, |
| 5 | + APP_LANGUAGES, |
| 6 | +} from '@events-helsinki/components'; |
| 7 | +import type { NextApiRequest, NextApiResponse } from 'next'; |
| 8 | +import { hobbiesApolloClient } from '../../domain/clients/hobbiesApolloClient'; |
| 9 | +import { staticGenerationLogger } from '../../logger'; |
| 10 | + |
| 11 | +// remove duplicates from array |
| 12 | +function uniqBySetWithArrayFrom<T>(array: T[]): T[] { |
| 13 | + return Array.from(new Set(array)); |
| 14 | +} |
| 15 | + |
| 16 | +export default async function handler( |
| 17 | + req: NextApiRequest, |
| 18 | + res: NextApiResponse |
| 19 | +) { |
| 20 | + let customUri = ''; |
| 21 | + |
| 22 | + // only POST allowed |
| 23 | + if (req.method !== 'POST') { |
| 24 | + return res.status(405).send('Only POST requests allowed'); |
| 25 | + } |
| 26 | + |
| 27 | + // Check for secret to confirm this is a valid request |
| 28 | + if (req.body.secret !== process.env.REVALIDATE_TOKEN) { |
| 29 | + return res.status(401).json({ message: 'Invalid token' }); |
| 30 | + } |
| 31 | + if (req.body.uri) { |
| 32 | + customUri = req.body.uri; |
| 33 | + } |
| 34 | + |
| 35 | + try { |
| 36 | + if (customUri === '') { |
| 37 | + // get all articles and pages |
| 38 | + const pages = [ |
| 39 | + ...(await getAllArticles(hobbiesApolloClient)), |
| 40 | + ...(await getAllPages(hobbiesApolloClient)), |
| 41 | + ].map((pageInfo) => { |
| 42 | + return pageInfo.uri.replace(/\/$/, ''); |
| 43 | + }); |
| 44 | + |
| 45 | + // root pages |
| 46 | + for (const lng of APP_LANGUAGES) { |
| 47 | + pages.push('/' + lng); |
| 48 | + } |
| 49 | + |
| 50 | + _revalidateAll(res, pages).catch((err) => { |
| 51 | + staticGenerationLogger.error(`Error during _revalidateAll`, err); |
| 52 | + }); |
| 53 | + } else { |
| 54 | + _revalidate(res, customUri).catch((err) => { |
| 55 | + staticGenerationLogger.error( |
| 56 | + `Error during _revalidate ${customUri}`, |
| 57 | + err |
| 58 | + ); |
| 59 | + }); |
| 60 | + } |
| 61 | + return res.status(200).json({ revalidatetrigger: true }); |
| 62 | + } catch (err) { |
| 63 | + // If there was an error, Next.js will continue |
| 64 | + // to show the last successfully generated page |
| 65 | + return res.status(500).send('Error revalidating'); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// revalidate all pages one by one |
| 70 | +async function _revalidateAll(res: NextApiResponse, pages: string[]) { |
| 71 | + for (const page of uniqBySetWithArrayFrom(pages)) { |
| 72 | + await _revalidate(res, page); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// revalidate a page |
| 77 | +// return the uri if revalidation failed |
| 78 | +async function _revalidate(res: NextApiResponse, uri: string) { |
| 79 | + try { |
| 80 | + const _uri = uri.replace(/\/$/, ''); |
| 81 | + if (_uri.length < 1) return null; |
| 82 | + |
| 83 | + staticGenerationLogger.info(`Revalidate a page: ${uri}`); |
| 84 | + await res.revalidate(_uri); |
| 85 | + } catch (err) { |
| 86 | + staticGenerationLogger.error(`Error while revalidate a page: ${uri}`, err); |
| 87 | + return uri; |
| 88 | + } |
| 89 | + return null; |
| 90 | +} |
0 commit comments