Skip to content

How to stop useSuspenseQuery from running during static generation? #76

@Tvrqvoise

Description

@Tvrqvoise

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

'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 (
    <div>{JSON.stringify(data)}</div>
  )
}

Then I get an error like this

$ npm run build

> [email protected] 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: /

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions