Files
vercel__next.js/examples/cache-handler-redis/app/cache-state-watcher.tsx
Mauro Accornero 0d3a353dfb fix[examples]: update example cache-handler-redis (#67350)
### What?

Fix cache-handler-redis example with working cache handler 

### Why?

It was not working because next-shared-cache was updated but some code
inside the cache-handler.js was deprecated after version 1.0.0.

When starting the app without Redis the cache handler was not switching
to the LRU handler.

### How?

- Updated cache-handler-redis example with working cache handler from
[next-shared-cache's
example](https://caching-tools.github.io/next-shared-cache/redis)
- Add check for PHASE_PRODUCTION_BUILD to avoid [issues while building
](https://github.com/caching-tools/next-shared-cache/issues/284#issuecomment-1919145094)
- Add throw error for Redis error to switch to the LRU handler
- Updated dependencies
- Updated readme broken links
- Remove "version" from docker-compose's file because it's outdated
[version outdated](https://github.com/docker/compose/issues/11628)

Closes NEXT-66812
Fixes #66812

Co-authored-by: Delba de Oliveira <32464864+delbaoliveira@users.noreply.github.com>
2024-07-18 13:57:54 +01:00

51 lines
1.0 KiB
TypeScript

"use client";
import { ReactNode, useEffect, useState } from "react";
type CacheStateWatcherProps = { time: number; revalidateAfter: number };
export function CacheStateWatcher({
time,
revalidateAfter,
}: CacheStateWatcherProps): ReactNode {
const [cacheState, setCacheState] = useState("");
const [countDown, setCountDown] = useState("");
useEffect(() => {
let id = -1;
function check(): void {
const now = Date.now();
setCountDown(
Math.max(0, (time + revalidateAfter - now) / 1000).toFixed(3),
);
if (now > time + revalidateAfter) {
setCacheState("stale");
return;
}
setCacheState("fresh");
id = requestAnimationFrame(check);
}
id = requestAnimationFrame(check);
return () => {
cancelAnimationFrame(id);
};
}, [revalidateAfter, time]);
return (
<>
<div className={`cache-state ${cacheState}`}>
Cache state: {cacheState}
</div>
<div className="stale-after">Stale in: {countDown}</div>
</>
);
}