chile-kit in Next.js
Pure ESM, subpath exports, no Node-only APIs and sideEffects: false: every module works in Server Components, Client Components, Route Handlers, proxy.ts (formerly middleware) and the Edge runtime, and unused data (like the 346 communes) stays out of client bundles. No React or Next dependency.
Server Component with a cached UF
// app/page.tsx: works with cacheComponents: true, the create-next-app default
import { formatCLP } from "chile-kit/clp";
import { getIndicator } from "chile-kit/indicators";
import { cacheLife } from "next/cache";
async function Uf() {
"use cache";
cacheLife("hours");
try {
const uf = await getIndicator("uf", { timeoutMs: 5000 });
return <p>UF hoy: {formatCLP(uf.value)}</p>;
} catch {
return <p>UF no disponible.</p>; // API down or slow: the build still passes
}
}
export default function Page() {
return <Uf />;
}
A cached component is rendered at build time, so next build calls mindicador.cl: keep the timeoutMs and the try/catch so a slow API cannot fail or stall the build. Without cacheComponents, pass fetchOptions: { next: { revalidate: 3600 } } instead. "use cache" is not available in the Edge runtime; there the library's in-memory cache (one hour) applies.
Client-side RUT validation
"use client";
import { formatRut, isValidRut } from "chile-kit/rut";
import { useState } from "react";
export function RutInput() {
const [rut, setRut] = useState("");
return (
<input
value={rut}
onChange={(e) => setRut(e.target.value)}
onBlur={() => isValidRut(rut) && setRut(formatRut(rut))}
aria-invalid={rut !== "" && !isValidRut(rut)}
/>
);
}
Region and commune selects
"use client";
import { getCommunesByRegion, REGIONS } from "chile-kit/regions";
import { useState } from "react";
export function CommuneSelect() {
const [region, setRegion] = useState("13");
return (
<>
<select value={region} onChange={(e) => setRegion(e.target.value)}>
{REGIONS.map((r) => <option key={r.code} value={r.code}>{r.name}</option>)}
</select>
<select name="commune">
{getCommunesByRegion(region).map((c) => <option key={c.code} value={c.code}>{c.name}</option>)}
</select>
</>
);
}
Dates without hydration mismatch
import { formatChileDateTime } from "chile-kit/date";
// Same string on the UTC server and in the browser in Chile.
<time dateTime={order.createdAt}>{formatChileDateTime(order.createdAt, "long")}</time>
A complete example app lives in examples/next.