Slide 14
Slide 14 text
ΫϥΠΞϯτΛߋ৽͢ΔLinkίϯϙʔωϯτ
14
import { type LinkProps, Link as RemixLink, useHref } from "@remix-run/react";
import { forwardRef, useCallback } from "react";
import clientVersion from "~/version.json";
export const Link = forwardRef((props, ref) => {
const { onClick, ...rest } = props;
const href = useHref(props.to, { relative: props.relative });
const handleClick = useCallback(
async (e: React.MouseEvent) => {
onClick?.(e);
if (e.defaultPrevented) {
return;
}
const currentUrl = new URL(window.location.href);
const res = await fetch(new URL("/version.json", currentUrl).href);
if (res.ok) {
const serverVersion = await res.json<{ version: string }>();
if (serverVersion.version !== clientVersion.version) {
location.href = href;
}
}
},
[onClick, href],
);
return ;
});