diff --git a/examples/server-render/components/nav.js b/examples/server-render/components/nav.js
new file mode 100644
index 000000000..03cacf11f
--- /dev/null
+++ b/examples/server-render/components/nav.js
@@ -0,0 +1,11 @@
+export default function Nav() {
+ return (<>
+
+
+ >)
+}
diff --git a/examples/server-render/components/pokemon.js b/examples/server-render/components/pokemon.js
new file mode 100644
index 000000000..3e69c42a8
--- /dev/null
+++ b/examples/server-render/components/pokemon.js
@@ -0,0 +1,20 @@
+export default function Pokemon({ item, revalidate }) {
+ if (!item || !item.name) return (
loading...
);
+ return (
+
-
Trending Projects
-
- {data && data.results
- ? data.results.map(pokemon => (
-
-
- {pokemon.name}
-
-
- ))
- : 'loading...'}
-
+
+
)
}
-
-Home.getInitialProps = async () => {
- const data = await fetcher(URL)
- return { initialData: data }
-}
diff --git a/examples/server-render/pages/workaround.js b/examples/server-render/pages/workaround.js
new file mode 100644
index 000000000..5665d08df
--- /dev/null
+++ b/examples/server-render/pages/workaround.js
@@ -0,0 +1,67 @@
+import React from 'react';
+import Link from 'next/link'
+import fetcher from '../libs/fetcher'
+import Nav from '../components/nav'
+import Pokemon from '../components/pokemon'
+
+import useSWR from 'swr'
+
+const useFixedSWR = (key, fetcher, {initialData, ...config} = {}) => {
+ const initial = React.useRef(initialData);
+ React.useEffect(() => {
+ if(key){
+ initial.current = undefined;
+ }
+ }, [key]);
+ return useSWR(key, fetcher, {...config, initialData: initial.current});
+}
+
+const api = {
+ list: () => 'https://pokeapi.co/api/v2/pokemon/',
+ item: (pokemon) => `https://pokeapi.co/api/v2/pokemon/${pokemon}`,
+}
+
+export default function Home(props) {
+ const { data: list } = useFixedSWR(api.list(), fetcher, { initialData: props.list })
+ const [selected, setSelected] = React.useState(list.results[0].name);
+ const { data: item } = useFixedSWR(api.item(selected), fetcher, { initialData: props.item })
+
+ return (
+
+
+
Workaround
+
+
+
+
+
+
Workaround itself:
+
{`
+
+const useFixedSWR = (key, fetcher, {initialData, ...config} = {}) => {
+ const initial = React.useRef(initialData);
+ React.useEffect(() => {
+ if(key){
+ initial.current = undefined;
+ }
+ }, [key]);
+ return useSWR(key, fetcher, {...config, initialData: initial.current});
+}
+
+ `}
+
+
+ )
+}
+
+Home.getInitialProps = async () => {
+ const list = await fetcher(api.list())
+ const item = await fetcher(api.item(list.results[0].name))
+ return { list, item }
+}