Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/swr-devtools-demo/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es5",
"target": "es2020",
"lib": [
"dom",
"dom.iterable",
Expand Down
5 changes: 4 additions & 1 deletion examples/swr-v1-devtools-demo/components/DevToolsView.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { useContext } from "react";
import { useSWRConfig } from "swr";
import { SWRDevToolsContext } from "swr-devtools";
import { SWRDevToolPanel } from "swr-devtools-panel";

// The way to use SWR DevTools as a React Component
export const DevToolsView = () => {
const { cache } = useSWRConfig();
const { events } = useContext(SWRDevToolsContext);
return (
<div
style={{
Expand All @@ -12,7 +15,7 @@ export const DevToolsView = () => {
}}
>
{/* @ts-expect-error */}
<SWRDevToolPanel cache={cache} events={null} />
<SWRDevToolPanel cache={cache} events={events} />
</div>
);
};
14 changes: 14 additions & 0 deletions examples/swr-v1-devtools-demo/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const path = require("path");

/**
* @type {import('next').NextConfig}
*/
module.exports = {
webpack(config) {
config.resolve.alias = {
...config.resolve.alias,
swr: path.resolve(__dirname, "node_modules/swr"),
};
return config;
},
};
2 changes: 1 addition & 1 deletion examples/swr-v1-devtools-demo/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es5",
"target": "es2020",
"lib": [
"dom",
"dom.iterable",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { useContext } from "react";
import { useSWRConfig } from "swr";
import { SWRDevToolsContext } from "swr-devtools";
import { SWRDevToolPanel } from "swr-devtools-panel";

// The way to use SWR DevTools as a React Component
export const DevToolsView = () => {
const { cache } = useSWRConfig();
const { events } = useContext(SWRDevToolsContext);
return (
<div
style={{
Expand All @@ -12,7 +15,7 @@ export const DevToolsView = () => {
}}
>
{/* @ts-expect-error */}
<SWRDevToolPanel cache={cache} events={null} />
<SWRDevToolPanel cache={cache} events={events} />
</div>
);
};
14 changes: 14 additions & 0 deletions examples/swr-v1-legacy-devtools-demo/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const path = require("path");

/**
* @type {import('next').NextConfig}
*/
module.exports = {
webpack(config) {
config.resolve.alias = {
...config.resolve.alias,
swr: path.resolve(__dirname, "node_modules/swr"),
};
return config;
},
};
2 changes: 1 addition & 1 deletion examples/swr-v1-legacy-devtools-demo/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es5",
"target": "es2020",
"lib": [
"dom",
"dom.iterable",
Expand Down
9 changes: 7 additions & 2 deletions packages/swr-devtools/src/SWRDevTools.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from "react";
import React, { useRef } from "react";
import { SWRConfig } from "swr";
import { EventEmitter, createSWRDevtools } from "./createSWRDevTools";

Expand All @@ -8,8 +8,13 @@ export const SWRDevToolsContext = React.createContext<SWRDevToolsContextValue>({
events: null,
});

if (typeof window !== "undefined") {
// @ts-expect-error
window.__SWR_DEVTOOLS_REACT__ = React;
}

export const SWRDevTools = ({ children }: { children: React.ReactNode }) => {
const [swrdevtools, events] = useState(() => createSWRDevtools())[0];
const [swrdevtools, events] = useRef(createSWRDevtools()).current;
return (
<SWRDevToolsContext.Provider value={{ events }}>
<SWRConfig value={{ use: [swrdevtools] }}>{children}</SWRConfig>
Expand Down
22 changes: 15 additions & 7 deletions packages/swr-devtools/src/createSWRDevTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,28 @@ const convertToSerializableObject = (value: any) => {
return value instanceof Error ? { message: value.message } : value;
};

const convertValue = (value: any) => {
if (typeof value !== "object" || value === null) return value;
if (Array.isArray(value)) {
return value.map(convertToSerializableObject);
}
return Object.keys(value).reduce(
(acc, cacheKey) => ({
...acc,
[cacheKey]: convertToSerializableObject(value[cacheKey]),
}),
{}
);
};

const inject = (cache: Cache) =>
injectSWRCache(cache, (key: string, value: any) => {
window.postMessage(
{
type: "updated_swr_cache",
payload: {
key,
value: Object.keys(value).reduce(
(acc, cacheKey) => ({
...acc,
[cacheKey]: convertToSerializableObject(value[cacheKey]),
}),
{}
),
value: convertValue(value),
},
},
"*"
Expand Down