UNPKG

remix-utils

Version:

This package contains simple utility functions to use with [React Router](https://reactrouter.com/).

42 lines 1.68 kB
import { TimingCollector } from "@edgefirst-dev/server-timing"; import { unstable_createContext } from "react-router"; /** * Create a middleware that gives you access to a `TimingCollector` instance, * used to collect server timing metrics and report them in the response * headers. * * The reported metrics will be available in the browser's developer tools. * * @returns A tuple with the middleware function and a function to get the * `TimingCollector` instance from the context. * @example * import { unstable_createServerTimingMiddleware } from "remix-utils/middleware/server-timing"; * * const [serverTimingMiddleware, getTimingCollector] = unstable_createServerTimingMiddleware(); * * export const unstable_middleware = [serverTimingMiddleware]; * * export async function loader({ context }: Route.LoaderArgs) { * let collector = getTimingCollector(context); * return await collector.measure("name", "optional description", () => { * return getData(); * }); * } */ export function unstable_createServerTimingMiddleware() { const timingContext = unstable_createContext(); return [ async function serverTimingMiddleware({ context }, next) { let collector = new TimingCollector(); context.set(timingContext, collector); let response = await next(); collector.toHeaders(response.headers); return response; }, function getTimingCollector(context) { return context.get(timingContext); }, ]; } export const [serverTimingMiddleware, getTimingCollector] = unstable_createServerTimingMiddleware(); //# sourceMappingURL=server-timing.js.map