UNPKG

@jjordy/swr-devtools

Version:

Devtools for SWR

32 lines (31 loc) 1.08 kB
import React from "react"; import { SWRConfig, useSWRConfig } from "swr"; import { injectSWRCache, isMetaCache } from "./SWRDevtools/Cache"; const injected = new WeakSet(); const inject = (c) => injectSWRCache(c, (key, value) => { if (isMetaCache(key)) { return; } if (typeof "window" !== undefined) { window.postMessage({ key, value }, "*"); } }); const devtools = (useSWRNext) => (key, fn, config) => { const { cache } = useSWRConfig(); if (!injected.has(cache)) { inject(cache); injected.add(cache); } const swr = useSWRNext(key, fn, config); return swr; }; export function SWRDevtools({ children, ...rest }) { if (process.env.NODE_ENV === "development") { const Devtools = require("./SWRDevtools").default; return (React.createElement(SWRConfig, { value: { use: [devtools] } }, children, React.createElement(Devtools, { ...rest }))); } return React.createElement(React.Fragment, null, children); } export default SWRDevtools;