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

(next-urql) - add example of using the built-in data-fetching functions #1168

Merged
merged 6 commits into from
Nov 18, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/curvy-terms-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/core': patch
---

Don't return `undefined` in the serialized ssr-result, this would crash in Next.JS
JoviDeCroock marked this conversation as resolved.
Show resolved Hide resolved
63 changes: 63 additions & 0 deletions docs/advanced/server-side-rendering.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,69 @@ When you are using `getStaticProps`, `getServerSideProps`, or `getStaticPaths`,
During the prepass of your component tree `next-urql` can't know how these functions will alter the props passed to your page component. This injection
could change the `variables` used in your `useQuery`. This will lead to error being thrown during the subsequent `toString` pass, which isn't supported in React 16.

### Using getStaticProps or getServerSideProps

By default `withUrqlClient` will add `getInitialProps` to the component you're wrapping it in, this however excludes us from using
`getStaticProps` and `getServerSideProps`. However we can enable this, let's look at an example:

```js
import { withUrqlClient, initUrqlClient } from "next-urql";
import {
ssrExchange,
dedupExchange,
cacheExchange,
fetchExchange,
useQuery
} from "urql";

const TODOS_QUERY = `
query { todos { id text } }
`;

const createUrqlClient = (ssr, ctx) => ({
url: "your-url"
exchanges: [dedupExchange, cacheExchange, ssrCache, fetchExchange]

Choose a reason for hiding this comment

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

Suggested change
exchanges: [dedupExchange, cacheExchange, ssrCache, fetchExchange]
exchanges: [dedupExchange, cacheExchange, ssr, fetchExchange]

I believe the ssr should be passed instead, no? 🤔

});

function Todos() {
const [res] = useQuery({ query: TODOS_QUERY });
return (
<div>
{res.data.todos.map((todo) => (
<div key={todo.id}>
{todo.id} - {todo.text}
</div>
))}
</div>
);
}

export async function getStaticProps(ctx) {
const ssrCache = ssrExchange({ isClient: false });
const client = initUrqlClient(createUrqlClient(ssrCache, ctx));

// This query is used to populate the cache for the query
// used on this page.
await client.query(TODOS_QUERY).toPromise();
kitten marked this conversation as resolved.
Show resolved Hide resolved

return {
props: {
// urqlState is a keyword here so withUrqlClient can pick it up.
urqlState: ssrCache.extractData()
},
};
}

export default withUrqlClient(
createUrqlClient,
{ ssr: false } // Important so we don't wrap our component in getInitialProps
)(Todos);
```

The above example will make sure the page is rendered as a static-page, it's important that you fully pre-populate your cache
so in our case we were only interested in getting our todos, if there are child components relying on data you'll have to make
sure these are fetched as well.

### Resetting the client instance

In rare scenario's you possibly will have to reset the client instance (reset all cache, ...), this is an uncommon scenario
Expand Down
14 changes: 9 additions & 5 deletions packages/core/src/exchanges/ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ const serializeResult = ({
data,
error,
}: OperationResult): SerializedResult => {
const result: SerializedResult = {
data: JSON.stringify(data),
error: undefined,
};
const result: SerializedResult = {};

if (data) {
JoviDeCroock marked this conversation as resolved.
Show resolved Hide resolved
result.data = JSON.stringify(data);
}

if (error) {
result.error = {
Expand All @@ -51,8 +52,11 @@ const serializeResult = ({
extensions: error.extensions,
};
}),
networkError: error.networkError ? '' + error.networkError : undefined,
};

if (error.networkError) {
result.error.networkError = '' + error.networkError;
}
}

return result;
Expand Down