apollographql / apollographql/apollo-client-integrations

How to stop useSuspenseQuery from running during static generation?

Open
#76 12 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
556
Forks
53
PR merge metrics
No merged PRs in 30d

Description

My API server is not accessible from my build server, so I get fetch errors when trying to render a component that has `useSuspenseQuery` at build time. It works fine during SSR and client render, just not during the static prerender.

I should note this is from within `app/` not `pages/`, which is why I suspect a lot of the stuff I've tried isn't working.

If I have a component like

```
/app/page.tsx
```

and it imports a component like

```tsx
'use client'

import { gql } from '@apollo/client';
import { useSuspenseQuery } from '@apollo/experimental-nextjs-app-support/ssr';
import { useState } from 'react';

// the docs seems to imply that these will stop server rendering
// like a dynamic slug will, but it does not work
export const dynamic = "force-dynamic";
export const revalidate = 0;

const query = gql`
query SomeQuery($substring: String!) {
/* whatever */
}
`

export default function SearchInput () {
const response: any = useSuspenseQuery(query);
const results = response?.data?.query

return (

{JSON.stringify(data)}

)
}
```

Then I get an error like this

```
$ npm run build

> client@0.1.0 build
> next build

- info Creating an optimized production build
- info Compiled successfully
- info Linting and checking validity of types
- info Collecting page data
[ ] - info Generating static pages (0/4)ApolloError: fetch failed

... stacktrace omitted ...

graphQLErrors: [],
protocolErrors: [],
clientErrors: [],
networkError: TypeError: fetch failed
at Object.fetch (node:internal/deps/undici/undici:11576:11)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
cause: Error: getaddrinfo ENOTFOUND some-internal-url
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:108:26)
at GetAddrInfoReqWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
errno: -3008,
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'some-internal-url'
}
},
extraInfo: undefined
}

Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error
ApolloError: fetch failed

... stacktrace omitted ....

- info Generating static pages (4/4)

> Export encountered errors on following paths:
/page: /
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the /app/page.tsx example and its imported client component, then run npm run build to reproduce the fetch failure during static page generation. Determine how the app/ route and useSuspenseQuery interact during prerendering; done means the page no longer attempts the inaccessible API during static generation while preserving the reported SSR and client behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, next.js, react, typescript
Domain
build-system, frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.