Cache with tagging

DATA: 2024-12-01T19:25:31.9983596


async function getWorldTime() {
  const endpoint =
    'https://timeapi.io/api/time/current/zone?timeZone=Europe%2FAmsterdam';
  const currentCache = await CheckCacheStatus(endpoint);

  const res = await fetch(
    'https://timeapi.io/api/time/current/zone?timeZone=Europe%2FAmsterdam',
    {
      next: {
        tags: ['worldtime-tag'],
      },
    }
  );

  if (res.ok) {
    const data = await res.json();
    const encodedData = Buffer.from(JSON.stringify(data)).toString('base64');

    return {
      dateTime: data.dateTime,
      cache: encodedData === currentCache ? 'HIT' : 'MISS',
    };
  }

  return null;
}

export default async function Home() {
  const data = await getWorldTime();
  if (!data) {
    return <div>loading...</div>;
  }
  return (
    <div>
      Cache tag: {data.dateTime}, CACHE: {data.cache}
      <div>
        <RevalidateButton tagName="worldtime-tag" />
      </div>
    </div>
  );
}