From 54ecf89b4bc1c90d7121e10acb2661d8772a0277 Mon Sep 17 00:00:00 2001 From: Maxime Bouveron Date: Fri, 3 May 2024 16:39:27 +0200 Subject: [PATCH] Add cache revalidation endpoint --- src/api/customObservations.ts | 6 +++--- src/api/details.ts | 2 +- src/api/geojson.ts | 2 +- src/api/observations.ts | 2 +- src/api/page.ts | 2 +- src/api/poi.ts | 2 +- src/api/settings.ts | 2 +- src/api/stations.ts | 6 +++--- src/app/api/revalidate/[tag]/route.ts | 13 +++++++++++++ 9 files changed, 25 insertions(+), 12 deletions(-) create mode 100644 src/app/api/revalidate/[tag]/route.ts diff --git a/src/api/customObservations.ts b/src/api/customObservations.ts index d7699eb..39b7d8a 100644 --- a/src/api/customObservations.ts +++ b/src/api/customObservations.ts @@ -33,7 +33,7 @@ async function fetchObservations(): Promise { const res = await fetch( `${process.env.apiHost}/api/portal/fr/${process.env.portal}/custom-contribution-types/`, { - next: { revalidate: 5 * 60 }, + next: { revalidate: 5 * 60, tags: ['admin', 'contributions'] }, headers: { Accept: 'application/json', }, @@ -49,7 +49,7 @@ async function fetchObservation(id: string): Promise { const res = await fetch( `${process.env.apiHost}/api/portal/fr/${process.env.portal}/custom-contribution-types/${id}`, { - next: { revalidate: 5 * 60 }, + next: { revalidate: 5 * 60, tags: ['admin', 'contributions'] }, headers: { Accept: 'application/json', }, @@ -67,7 +67,7 @@ async function fetchObservationDetails( const res = await fetch( `${process.env.apiHost}/api/portal/fr/${process.env.portal}/custom-contribution-types/${id}/contributions`, { - next: { revalidate: 5 * 60 }, + next: { revalidate: 5 * 60, tags: ['admin', 'contributions'] }, headers: { Accept: 'application/json', }, diff --git a/src/api/details.ts b/src/api/details.ts index 7ca1922..1a4508e 100644 --- a/src/api/details.ts +++ b/src/api/details.ts @@ -2,7 +2,7 @@ import { getDetailsUrl } from './settings'; async function fetchDetails(url: string) { const res = await fetch(`${process.env.apiHost}${url}`, { - next: { revalidate: 5 * 60 }, + next: { revalidate: 5 * 60, tags: ['details', 'admin'] }, headers: { Accept: 'application/json', }, diff --git a/src/api/geojson.ts b/src/api/geojson.ts index 6b583bf..2544ac0 100644 --- a/src/api/geojson.ts +++ b/src/api/geojson.ts @@ -2,7 +2,7 @@ import { GeoJSON } from 'geojson'; async function fetchGeoJSON(url: string) { const res = await fetch(`${process.env.apiHost}${url}`, { - next: { revalidate: 20 * 60 }, + next: { revalidate: 20 * 60, tags: ['admin', 'geojson'] }, }); if (res.status < 200 || res.status > 299) { return null; diff --git a/src/api/observations.ts b/src/api/observations.ts index b4e9b76..cac8f0a 100644 --- a/src/api/observations.ts +++ b/src/api/observations.ts @@ -10,7 +10,7 @@ async function fetchObservation() { const res = await fetch( `${process.env.apiHost}/api/portal/fr/${process.env.portal}/contributions/json_schema/`, { - next: { revalidate: 5 * 60 }, + next: { revalidate: 5 * 60, tags: ['admin', 'observations'] }, headers: { Accept: 'application/json', }, diff --git a/src/api/page.ts b/src/api/page.ts index 997fb05..2ad4ed1 100644 --- a/src/api/page.ts +++ b/src/api/page.ts @@ -2,7 +2,7 @@ import { Menu, getMenuSettings } from './settings'; async function fetchDetails(url: string) { const res = await fetch(`${process.env.apiHost}${url}`, { - next: { revalidate: 5 * 60 }, + next: { revalidate: 5 * 60, tags: ['admin', 'staticpages'] }, headers: { Accept: 'application/json', }, diff --git a/src/api/poi.ts b/src/api/poi.ts index c21a79a..7dfff19 100644 --- a/src/api/poi.ts +++ b/src/api/poi.ts @@ -23,7 +23,7 @@ async function fetchPois({ params }: FetchPoisProps = {}): Promise { headers: { Accept: 'application/json', }, - next: { revalidate: 5 * 60 }, + next: { revalidate: 5 * 60, tags: ['admin', 'pois'] }, }); if (res.status < 200 || res.status > 299) { return []; diff --git a/src/api/settings.ts b/src/api/settings.ts index 58487b4..b813a22 100644 --- a/src/api/settings.ts +++ b/src/api/settings.ts @@ -158,7 +158,7 @@ export async function fetchSettings(): Promise { const res = await fetch( `${process.env.apiHost}/api/portal/fr/portal/${process.env.portal}/`, { - next: { revalidate: 5 * 60 }, + next: { revalidate: 5 * 60, tags: ['admin', 'settings'] }, headers: { Accept: 'application/json', }, diff --git a/src/api/stations.ts b/src/api/stations.ts index e3ed615..2621006 100644 --- a/src/api/stations.ts +++ b/src/api/stations.ts @@ -25,7 +25,7 @@ async function fetchStations(): Promise { headers: { Accept: 'application/json', }, - next: { revalidate: 5 * 60 }, + next: { revalidate: 5 * 60, tags: ['admin', 'stations'] }, }); if (res.status < 200 || res.status > 299) { return []; @@ -39,7 +39,7 @@ async function fetchStation(id: number): Promise { headers: { Accept: 'application/json', }, - next: { revalidate: 5 * 60 }, + next: { revalidate: 5 * 60, tags: ['admin', 'stations'] }, }); if (res.status < 200 || res.status > 299) { return null; @@ -55,7 +55,7 @@ async function fetchStationObservations( headers: { Accept: 'application/json', }, - next: { revalidate: 5 * 60 }, + next: { revalidate: 5 * 60, tags: ['admin', 'stations'] }, }); if (res.status < 200 || res.status > 299) { return null; diff --git a/src/app/api/revalidate/[tag]/route.ts b/src/app/api/revalidate/[tag]/route.ts new file mode 100644 index 0000000..520ad3e --- /dev/null +++ b/src/app/api/revalidate/[tag]/route.ts @@ -0,0 +1,13 @@ +import { revalidateTag } from 'next/cache'; +import { NextRequest, NextResponse } from 'next/server'; + +export async function POST( + request: NextRequest, + { params }: { params: { tag: string } }, +) { + const tag = params.tag; + + revalidateTag(tag); + + return NextResponse.json({ revalidated: tag }); +}