|
1 | 1 | import { NextResponse } from 'next/server'; |
2 | | -import { revalidatePath, revalidateTag } from 'next/cache'; |
3 | | -import { AVAILABLE_LOCALES } from '../../../i18n/routing'; |
4 | 2 | import { nonEmpty } from '../../utils/config'; |
| 3 | +import { |
| 4 | + revalidateFullSite, |
| 5 | + revalidateAllFeeds, |
| 6 | + revalidateAllGbfsFeeds, |
| 7 | + revalidateAllGtfsFeeds, |
| 8 | + revalidateAllGtfsRtFeeds, |
| 9 | + revalidateSpecificFeeds, |
| 10 | +} from '../../utils/revalidate-feeds'; |
5 | 11 |
|
6 | 12 | type RevalidateTypes = |
7 | 13 | | 'full' |
@@ -46,8 +52,7 @@ export async function GET(req: Request): Promise<NextResponse> { |
46 | 52 | } |
47 | 53 |
|
48 | 54 | try { |
49 | | - revalidateTag('guest-feeds', 'max'); |
50 | | - revalidatePath('/[locale]/feeds/[feedDataType]/[feedId]', 'layout'); |
| 55 | + revalidateAllFeeds(); |
51 | 56 | console.log( |
52 | 57 | '[cron] revalidate /api/revalidate: all-feeds revalidation triggered', |
53 | 58 | ); |
@@ -104,60 +109,13 @@ export async function POST(req: Request): Promise<NextResponse> { |
104 | 109 | // revalidateTag = triggers revalidation for API calls using `unstable_cache` with matching tags (e.g., feed-123, guest-feeds) |
105 | 110 |
|
106 | 111 | try { |
107 | | - // clears cache for entire site |
108 | | - if (payload.type === 'full') { |
109 | | - revalidateTag('guest-feeds', 'max'); |
110 | | - revalidatePath('/', 'layout'); |
111 | | - } |
112 | | - |
113 | | - // clears cache for all feed pages (ISR-cached layout) |
114 | | - if (payload.type === 'all-feeds') { |
115 | | - revalidateTag('guest-feeds', 'max'); |
116 | | - revalidatePath('/[locale]/feeds/[feedDataType]/[feedId]', 'layout'); |
117 | | - } |
118 | | - |
119 | | - // clears cache for all GBFS feed pages (ISR-cached layout) |
120 | | - if (payload.type === 'all-gbfs-feeds') { |
121 | | - revalidateTag('feed-type-gbfs', 'max'); |
122 | | - revalidatePath('/[locale]/feeds/gbfs/[feedId]', 'layout'); |
123 | | - } |
124 | | - |
125 | | - // clears cache for all GTFS feed pages (ISR-cached layout) |
126 | | - if (payload.type === 'all-gtfs-feeds') { |
127 | | - revalidateTag('feed-type-gtfs', 'max'); |
128 | | - revalidatePath('/[locale]/feeds/gtfs/[feedId]', 'layout'); |
129 | | - } |
130 | | - |
131 | | - // clears cache for all GTFS RT feed pages (ISR-cached layout) |
132 | | - if (payload.type === 'all-gtfs-rt-feeds') { |
133 | | - revalidateTag('feed-type-gtfs_rt', 'max'); |
134 | | - revalidatePath('/[locale]/feeds/gtfs_rt/[feedId]', 'layout'); |
135 | | - } |
136 | | - |
137 | | - // clears cache for specific feed pages (ISR-cached page) + localized paths |
138 | | - if (payload.type === 'specific-feeds') { |
139 | | - const localPaths = AVAILABLE_LOCALES.filter((loc) => loc !== 'en'); |
140 | | - const pathsToRevalidate: string[] = []; |
141 | | - |
142 | | - payload.feedIds.forEach((id) => { |
143 | | - revalidateTag(`feed-${id}`, 'max'); |
144 | | - // The id will try to revalidate all feed types with that id, but that's necessary since we don't know the feed type here and it's not a big deal if we revalidate some non-existent pages |
145 | | - pathsToRevalidate.push(`/feeds/gtfs/${id}`); |
146 | | - pathsToRevalidate.push(`/feeds/gtfs_rt/${id}`); |
147 | | - pathsToRevalidate.push(`/feeds/gbfs/${id}`); |
148 | | - }); |
149 | | - |
150 | | - console.log('Revalidating paths:', pathsToRevalidate); |
151 | | - |
152 | | - pathsToRevalidate.forEach((path) => { |
153 | | - revalidatePath(path); |
154 | | - revalidatePath(path + '/map'); |
155 | | - localPaths.forEach((loc) => { |
156 | | - revalidatePath(`/${loc}${path}`); |
157 | | - revalidatePath(`/${loc}${path}/map`); |
158 | | - }); |
159 | | - }); |
160 | | - } |
| 112 | + if (payload.type === 'full') revalidateFullSite(); |
| 113 | + if (payload.type === 'all-feeds') revalidateAllFeeds(); |
| 114 | + if (payload.type === 'all-gbfs-feeds') revalidateAllGbfsFeeds(); |
| 115 | + if (payload.type === 'all-gtfs-feeds') revalidateAllGtfsFeeds(); |
| 116 | + if (payload.type === 'all-gtfs-rt-feeds') revalidateAllGtfsRtFeeds(); |
| 117 | + if (payload.type === 'specific-feeds') |
| 118 | + revalidateSpecificFeeds(payload.feedIds); |
161 | 119 |
|
162 | 120 | return NextResponse.json({ |
163 | 121 | ok: true, |
|
0 commit comments