From 43c70c85c93a3d12606e35507dadf43f6a750729 Mon Sep 17 00:00:00 2001 From: Biboswan Roy Date: Wed, 9 Jun 2021 19:46:05 +0530 Subject: [PATCH 1/3] Use latest client and don't use old ssr cache on resetUrqlClient --- packages/next-urql/src/with-urql-client.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/next-urql/src/with-urql-client.ts b/packages/next-urql/src/with-urql-client.ts index 30e76fcb40..fa916b6f4e 100644 --- a/packages/next-urql/src/with-urql-client.ts +++ b/packages/next-urql/src/with-urql-client.ts @@ -48,14 +48,14 @@ export function withUrqlClient( // eslint-disable-next-line react-hooks/rules-of-hooks const client = React.useMemo(() => { - if (urqlClient) { + if (urqlClient && forceUpdate[0] === 0) { return urqlClient; } if (!ssr || typeof window === 'undefined') { // We want to force the cache to hydrate, we do this by setting the isClient flag to true ssr = ssrExchange({ initialState: urqlServerState, isClient: true }); - } else if (ssr && typeof window !== 'undefined') { + } else if (forceUpdate[0] === 0) { ssr.restoreData(urqlServerState); } From 25f7c43e7d2f1b00e06d7517aa723e8c0baf211b Mon Sep 17 00:00:00 2001 From: Phil Pluckthun Date: Tue, 6 Jul 2021 15:28:06 +0100 Subject: [PATCH 2/3] Refactor version check --- packages/next-urql/package.json | 1 + packages/next-urql/src/with-urql-client.ts | 22 +++++++++------------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/packages/next-urql/package.json b/packages/next-urql/package.json index b88aa596ad..e2ada7f80c 100644 --- a/packages/next-urql/package.json +++ b/packages/next-urql/package.json @@ -25,6 +25,7 @@ "test": "jest", "clean": "rimraf dist", "check": "tsc --noEmit", + "lint": "eslint --ext=js,jsx,ts,tsx .", "build": "rollup -c ../../scripts/rollup/config.js", "prepare": "node ../../scripts/prepare/index.js", "prepublishOnly": "run-s clean build" diff --git a/packages/next-urql/src/with-urql-client.ts b/packages/next-urql/src/with-urql-client.ts index fa916b6f4e..9dfdd8f39c 100644 --- a/packages/next-urql/src/with-urql-client.ts +++ b/packages/next-urql/src/with-urql-client.ts @@ -1,4 +1,4 @@ -import React, { createElement, useState } from 'react'; +import { createElement, useCallback, useReducer, useMemo } from 'react'; import ssrPrepass from 'react-ssr-prepass'; import { @@ -42,20 +42,18 @@ export function withUrqlClient( urqlState, ...rest }: WithUrqlProps) => { - // eslint-disable-next-line react-hooks/rules-of-hooks - const forceUpdate = useState(0); + const [version, forceUpdate] = useReducer(prev => prev + 1, 0); const urqlServerState = (pageProps && pageProps.urqlState) || urqlState; - // eslint-disable-next-line react-hooks/rules-of-hooks - const client = React.useMemo(() => { - if (urqlClient && forceUpdate[0] === 0) { + const client = useMemo(() => { + if (urqlClient && !version) { return urqlClient; } if (!ssr || typeof window === 'undefined') { // We want to force the cache to hydrate, we do this by setting the isClient flag to true ssr = ssrExchange({ initialState: urqlServerState, isClient: true }); - } else if (forceUpdate[0] === 0) { + } else if (!version) { ssr.restoreData(urqlServerState); } @@ -70,16 +68,14 @@ export function withUrqlClient( ]; } - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion return initUrqlClient(clientConfig, shouldEnableSuspense)!; - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [urqlClient, urqlServerState, forceUpdate[0]]); + }, [urqlClient, urqlServerState, version]); - const resetUrqlClient = () => { + const resetUrqlClient = useCallback(() => { resetClient(); ssr = ssrExchange({ initialState: undefined }); - forceUpdate[1](forceUpdate[0] + 1); - }; + forceUpdate(); + }, []); return createElement( Provider, From e8fa29b63f93906a106727aac66200c44e235bcc Mon Sep 17 00:00:00 2001 From: Phil Pluckthun Date: Tue, 6 Jul 2021 15:28:55 +0100 Subject: [PATCH 3/3] Add changeset --- .changeset/spicy-impalas-repair.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/spicy-impalas-repair.md diff --git a/.changeset/spicy-impalas-repair.md b/.changeset/spicy-impalas-repair.md new file mode 100644 index 0000000000..d8c8e7913f --- /dev/null +++ b/.changeset/spicy-impalas-repair.md @@ -0,0 +1,5 @@ +--- +'next-urql': patch +--- + +Fix `resetUrqlClient` not resetting the SSR cache itself and instead restoring data when all data related to this `Client` and session should've been deleted.