Skip to content

Commit

Permalink
put the ssr and client creation inside of the React function body to …
Browse files Browse the repository at this point in the history
…avoid leaking ssr results
  • Loading branch information
JoviDeCroock committed Sep 2, 2023
1 parent 47fdb66 commit a317772
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
19 changes: 12 additions & 7 deletions docs/advanced/server-side-rendering.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,16 +232,21 @@ structure it as the following.
// app/client/layout.tsx
'use client';

import { useMemo } from 'react';
import { UrqlProvider, ssrExchange, cacheExchange, fetchExchange, createClient } from '@urql/next';

const ssr = ssrExchange();
const client = createClient({
url: 'https://trygql.formidable.dev/graphql/web-collections',
exchanges: [cacheExchange, ssr, fetchExchange],
suspense: true,
});

export default function Layout({ children }: React.PropsWithChildren) {
const [client, ssr] = useMemo(() => {
const ssr = ssrExchange();
const client = createClient({
url: 'https://trygql.formidable.dev/graphql/web-collections',
exchanges: [cacheExchange, ssr, fetchExchange],
suspense: true,
});

return [client, ssr];
}, []);

return (
<UrqlProvider client={client} ssr={ssr}>
{children}
Expand Down
19 changes: 12 additions & 7 deletions examples/with-next/app/non-rsc/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use client';

import { useMemo } from 'react';
import {
UrqlProvider,
ssrExchange,
Expand All @@ -8,14 +9,18 @@ import {
createClient,
} from '@urql/next';

const ssr = ssrExchange();
const client = createClient({
url: 'https://graphql-pokeapi.graphcdn.app/',
exchanges: [cacheExchange, ssr, fetchExchange],
suspense: true,
});

export default function Layout({ children }: React.PropsWithChildren) {
const [client, ssr] = useMemo(() => {
const ssr = ssrExchange();
const client = createClient({
url: 'https://trygql.formidable.dev/graphql/web-collections',
exchanges: [cacheExchange, ssr, fetchExchange],
suspense: true,
});

return [client, ssr];
}, []);

return (
<UrqlProvider client={client} ssr={ssr}>
{children}
Expand Down

0 comments on commit a317772

Please sign in to comment.