Skip to content

Commit

Permalink
feat: support swr v1 beta
Browse files Browse the repository at this point in the history
BREAKING CHANGE: drop swr v0.x support
  • Loading branch information
koba04 committed Aug 25, 2021
1 parent 5a55a6a commit 9170f48
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 23 deletions.
1 change: 1 addition & 0 deletions examples/swr-devtools-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"react": "17.0.2",
"react-dom": "17.0.2",
"styled-components": "^5.3.0",
"swr": "^1.0.0-beta.13",
"swr-devtools": "0.1.0"
},
"devDependencies": {
Expand Down
14 changes: 4 additions & 10 deletions examples/swr-devtools-demo/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import "../styles/globals.css";
import { SWRDevTools, launch } from "swr-devtools";
import { cache } from "swr";

// The way to use SWR DevTools as a Chrome extension
if (typeof window !== "undefined") {
launch(cache);
}
import { /* SWRDevToolPanel, */ SWRDevTools } from "swr-devtools";

// The way to use SWR DevTools as a React Component
const DevToolsArea = () => (
Expand All @@ -17,16 +11,16 @@ const DevToolsArea = () => (
height: "400px",
}}
>
<SWRDevTools cache={cache} />
{/* <SWRDevToolPanel cache={cache} /> */}
</div>
);

function MyApp({ Component, pageProps }) {
return (
<>
<SWRDevTools>
<Component {...pageProps} />
<DevToolsArea />
</>
</SWRDevTools>
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/swr-devtools-extensions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"react": "^17.0.2",
"react-dom": "^17.0.2",
"styled-components": "^5.3.0",
"swr": "^0.5.6",
"swr": "^1.0.0-beta.13",
"swr-devtools": "0.1.0"
}
}
4 changes: 2 additions & 2 deletions packages/swr-devtools-extensions/src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import ReactDOM from "react-dom";
import { SWRDevTools } from "swr-devtools";
import { SWRDevToolPanel } from "swr-devtools";

const cache = new Map();

const render = () => {
ReactDOM.render(
<SWRDevTools cache={cache} />,
<SWRDevToolPanel cache={cache} />,
document.getElementById("app")
);
};
Expand Down
1 change: 0 additions & 1 deletion packages/swr-devtools-extensions/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ module.exports = {
background: "./src/background.ts",
content: "./src/content.ts",
devtools: "./src/devtools.ts",
runtime: "./src/runtime.ts",
},
output: {
path: path.resolve(__dirname, "dist"),
Expand Down
4 changes: 2 additions & 2 deletions packages/swr-devtools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
"peerDependencies": {
"react": "^17.0.2",
"styled-components": "^5.3.0",
"swr": "^0.5.6"
"swr": "^1.0.0-beta.13"
},
"devDependencies": {
"@types/react": "^17.0.16",
"@types/styled-components": "^5.1.12",
"react": "^17.0.2",
"styled-components": "^5.3.0",
"swr": "^0.5.6",
"swr": "^1.0.0-beta.13",
"typescript": "^4.1.3"
},
"dependencies": {
Expand Down
32 changes: 32 additions & 0 deletions packages/swr-devtools/src/SWRDevTools.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from "react";
import { SWRConfig, Middleware } from "swr";
import { SWRCache } from "./devtools-cache";

import { injectSWRCache, isMetaCache } from "./swr-cache";

const injected = new WeakSet();

const inject = (cache: SWRCache) =>
injectSWRCache(cache, (key: string, value: any) => {
if (isMetaCache(key)) {
return;
}
window.postMessage(
{ __SWR_DEVTOOLS__: { cacheData: { key, value } } },
"*"
);
});

const swrdevtools: Middleware = (useSWRNext) => (key, fn, config) => {
// @ts-expect-error
if (!injected.has(config.cache)) {
// @ts-expect-error
inject(config.cache);
}
console.log({ key, fn, config });
return useSWRNext(key, fn, config);
};

export const SWRDevTools = ({ children }: { children: React.ReactNode }) => {
return <SWRConfig value={{ use: [swrdevtools] }}>{children}</SWRConfig>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const GlobalStyle = createGlobalStyle`
}
`;

export const SWRDevTools = ({ cache }: Props) => {
export const SWRDevToolPanel = ({ cache }: Props) => {
const [currentCache, historyCache] = useDevToolsCache(cache);
const [activePanel, setActivePanel] = useState<Panel["key"]>("current");
const [selectedItemKey, setSelectedItemKey] = useState<ItemKey | null>(null);
Expand Down
2 changes: 1 addition & 1 deletion packages/swr-devtools/src/components/Tab.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import styled from "styled-components";
import { PanelType, Panel } from "./SWRDevTool";
import { PanelType, Panel } from "./SWRDevToolPanel";

type TabProps = {
onChange: (panel: PanelType) => void;
Expand Down
3 changes: 2 additions & 1 deletion packages/swr-devtools/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { SWRDevTools } from "./components/SWRDevTool";
export { SWRDevToolPanel } from "./components/SWRDevToolPanel";
export { SWRDevTools } from "./SWRDevTools";
export { launch } from "./runtime";
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4473,10 +4473,10 @@ supports-color@^8.0.0:
dependencies:
has-flag "^4.0.0"

swr@^0.5.6:
version "0.5.6"
resolved "https://registry.yarnpkg.com/swr/-/swr-0.5.6.tgz#70bfe9bc9d7ac49a064be4a0f4acf57982e55a31"
integrity sha512-Bmx3L4geMZjYT5S2Z6EE6/5Cx6v1Ka0LhqZKq8d6WL2eu9y6gHWz3dUzfIK/ymZVHVfwT/EweFXiYGgfifei3w==
swr@^1.0.0-beta.13:
version "1.0.0-beta.13"
resolved "https://registry.yarnpkg.com/swr/-/swr-1.0.0-beta.13.tgz#f555bc1d314c69d42d665016f6c39e0881b9c58d"
integrity sha512-m5+KB6wA4YS1BVUfnZA51NbTF7uQ5YqP3crRwF1CrUvoRXn7TU0U/Zb2Il4hvW9QzzqwZwcbJQnlysLTdnYb/A==
dependencies:
dequal "2.0.2"

Expand Down

0 comments on commit 9170f48

Please sign in to comment.