Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/suspense experiment #103

Closed
wants to merge 11 commits into from
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"private": true,
"dependencies": {
"@urql/exchange-suspense": "^0.1.0",
"dotenv": "8.0.0",
"graphql": "14.4.2",
"graphql-tag": "2.10.1",
Expand All @@ -32,9 +33,9 @@
"urql": "1.3.0"
},
"scripts": {
"dev": "now dev",
"build": "next build",
"start": "next start",
"dev": "now dev",
"test": "jest",
"tdd": "jest --watch",
"lint": "eslint '**/*.js'",
Expand Down
11 changes: 10 additions & 1 deletion pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ const dark = {
background: "#111"
};

const ClientSuspense = props =>
typeof window !== "undefined" ? (
<React.Suspense {...props} />
) : (
<>{props.children}</>
);

class CustomApp extends App {
render() {
const { Component, pageProps, store, urqlClient } = this.props;
Expand All @@ -24,7 +31,9 @@ class CustomApp extends App {
<UrqlProvider value={urqlClient}>
<ReduxProvider store={store}>
<CustomThemeProvider light={light} dark={dark}>
<Component {...pageProps} />
<ClientSuspense fallback="Loading...">
<Component {...pageProps} />
</ClientSuspense>
</CustomThemeProvider>
</ReduxProvider>
</UrqlProvider>
Expand Down
1 change: 1 addition & 0 deletions pages/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ const Repos = () => {
renderTopic={topic => (
<Topic
topic={topic}
key={topic}
onRename={() => {
const newTopic = prompt(
`Enter new name for topic "${topic}"`,
Expand Down
14 changes: 10 additions & 4 deletions util/urql/init-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
fetchExchange,
ssrExchange
} from "urql";

import { suspenseExchange } from "@urql/exchange-suspense";
import "isomorphic-unfetch";

let urqlClient = null;
Expand All @@ -17,15 +17,21 @@ export default function initUrqlClient(initialState, token) {
// Reuse the client on the client-side however
const isServer = typeof window === "undefined";
if (isServer || !urqlClient) {
ssrCache = ssrExchange({ initialState });
ssrCache = ssrExchange({ initialState, isClient: !isServer });

const headers = !token ? {} : { Authorization: `Bearer ${token}` };

urqlClient = createClient({
url: "https://api.github.com/graphql",
// Active suspense mode on the server-side
suspense: isServer,
exchanges: [dedupExchange, cacheExchange, ssrCache, fetchExchange],
suspense: true,
exchanges: [
dedupExchange,
suspenseExchange,
cacheExchange,
ssrCache,
Copy link

@kitten kitten Aug 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iamstarkov Found the bug 😁 The ssrExchange behaves differently on the client and server. Previously we haven't taken the suspenseExchange into account, so we simply assume !!client.suspense means we're on the server and !client.suspense means we're on the client.

So, what happens is, since you're using SSR + @urql/exchange-suspense the client-side is always assuming that it's actually on the server-side. So it never deletes its cached SSR data.

We'll have to fix this by maybe adding isServer: boolean to the ssrExchange({ ... }) options.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it fixed an issue

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are no errors, and update works. but I still can't make Suspense's fallback to be triggered even with 5s delay network throttling
Screen Shot 2019-08-06 at 2 39 43 AM

fetchExchange
],
fetchOptions: { headers }
});
}
Expand Down
15 changes: 11 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,13 @@
resolved "https://registry.yarnpkg.com/@types/yargs/-/yargs-12.0.11.tgz#a090d88e1f40a910e6443c95493c1c035c76ebdc"
integrity sha512-IsU1TD+8cQCyG76ZqxP0cVFnofvfzT8p/wO8ENT4jbN/KKN3grsHFgHNl/U+08s33ayX4LwI85cEhYXCOlOkMw==

"@urql/exchange-suspense@^0.1.0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@urql/exchange-suspense/-/exchange-suspense-0.1.0.tgz#75f1f928f949c1463458961c33640388f09c7efb"
integrity sha512-P8/bnvUFDWxWQPxEXvidUxZ5pBhqmTF2GWIkgMIo1F/c/ilf5YbmHLaMDTfbrVOEalBwwTCyZbypoX+ghs/xAQ==
dependencies:
wonka "^3.1.1"

"@webassemblyjs/[email protected]":
version "1.8.5"
resolved "https://registry.yarnpkg.com/@webassemblyjs/ast/-/ast-1.8.5.tgz#51b1c5fe6576a34953bf4b253df9f0d490d9e359"
Expand Down Expand Up @@ -5147,9 +5154,9 @@ merge-stream@^2.0.0:
integrity sha512-abv/qOcuPfk3URPfDzmZU1LKmuw8kT+0nIHvKrKgFrwifol/doWcdA4ZqsWQ8ENrFKkd67Mfpo/LovbIUsbt3w==

merge2@^1.2.3:
version "1.2.3"
resolved "https://registry.yarnpkg.com/merge2/-/merge2-1.2.3.tgz#7ee99dbd69bb6481689253f018488a1b902b0ed5"
integrity sha512-gdUU1Fwj5ep4kplwcmftruWofEFt6lfpkkr3h860CXbAB9c3hGb55EOL2ali0Td5oebvW0E1+3Sr+Ur7XfKpRA==
version "1.2.4"
resolved "https://registry.yarnpkg.com/merge2/-/merge2-1.2.4.tgz#c9269589e6885a60cf80605d9522d4b67ca646e3"
integrity sha512-FYE8xI+6pjFOhokZu0We3S5NKCirLbCzSh2Usf3qEyr4X8U+0jNg9P8RZ4qz+V2UoECLVwSyzU3LxXBaLGtD3A==

microevent.ts@~0.1.1:
version "0.1.1"
Expand Down Expand Up @@ -7897,7 +7904,7 @@ wide-align@^1.1.0:
dependencies:
string-width "^1.0.2 || 2"

wonka@^3.2.0:
wonka@^3.1.1, wonka@^3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/wonka/-/wonka-3.2.0.tgz#40b9ce8e0c375127437661290e98e5d9e5278440"
integrity sha512-sHCLouGzTclUlp1sdXAhuTqGMIoAQfpBit9puepaINPVqDBRtC8DPWtTUHC0DJIdFSO+DsO86AFBFCDor7Sn0g==
Expand Down