'use client'; import { useState } from 'react'; // Define API routes for path-based revalidation const apiRoutePaths: Record = { 'Cached Static Fetch': '/api/cached-static-fetch', 'Uncached Fetch': '/api/uncached-fetch', 'Revalidated Fetch': '/api/revalidated-fetch', 'Nested Fetch in API Route': '/api/nested-fetch-in-api-route/revalidated-fetch', }; // Define API routes with tags for tag-based revalidation const apiRouteTags: Record = { 'Revalidated Fetch in Nested API Route': 'revalidated-fetch-revalidate3-nested-fetch-in-api-route', 'Revalidated Fetch API': 'revalidated-fetch-api', 'Cached Static Fetch API': 'cached-static-fetch-api', 'Uncached Fetch API': 'uncached-fetch-api', }; // Revalidation Interface Component export function RevalidationInterface() { const [selectedPathRoute, setSelectedPathRoute] = useState(''); const [selectedTagRoute, setSelectedTagRoute] = useState(''); const [revalidationStatus, setRevalidationStatus] = useState(''); const [isLoading, setIsLoading] = useState(false); const handlePathRevalidate = async () => { if (!selectedPathRoute) { setRevalidationStatus('Please select an API route to revalidate by path'); return; } setIsLoading(true); setRevalidationStatus('Revalidating API route by path...'); try { const path = apiRoutePaths[selectedPathRoute]; const response = await fetch(`/api/revalidatePath?path=${path}`); const data = await response.json(); if (response.ok) { setRevalidationStatus( `Successfully revalidated API route by path: ${selectedPathRoute} (${path})`, ); } else { setRevalidationStatus(`Error: ${data.error || 'Unknown error'}`); } } catch (error: unknown) { const errorMessage = error instanceof Error ? error.message : 'Unknown error'; setRevalidationStatus(`Error: ${errorMessage}`); } finally { setIsLoading(false); } }; const handleTagRevalidate = async () => { if (!selectedTagRoute) { setRevalidationStatus('Please select an API route to revalidate by tag'); return; } setIsLoading(true); setRevalidationStatus('Revalidating API route by tag...'); try { const tag = apiRouteTags[selectedTagRoute]; const response = await fetch(`/api/revalidateTag?tag=${tag}`); const data = await response.json(); if (response.ok) { setRevalidationStatus( `Successfully revalidated API route by tag: ${selectedTagRoute} (tag: ${tag})`, ); } else { setRevalidationStatus(`Error: ${data.error || 'Unknown error'}`); } } catch (error: unknown) { const errorMessage = error instanceof Error ? error.message : 'Unknown error'; setRevalidationStatus(`Error: ${errorMessage}`); } finally { setIsLoading(false); } }; return (

API Route Revalidation Interface

{/* Path-based revalidation section */}

Revalidate API Route by Path

{/* Tag-based revalidation section */}

Revalidate API Route by Tag

{/* Status display */} {revalidationStatus && (
{revalidationStatus}
)}
); }