import { NextPageContext, NextPage, NextComponentType } from 'next'; import NextApp, { AppContext } from 'next/app'; import { Client, SSRExchange, ClientOptions, SSRData } from '@urql/core'; /** The Next.js {@link NextPageContext}, as modified by `next-urql`. */ interface NextUrqlPageContext extends NextPageContext { urqlClient: Client; } /** The Next.js {@link AppContext}, as modified by `next-urql`. */ interface NextUrqlAppContext extends AppContext { urqlClient: Client; } type NextUrqlContext = NextUrqlPageContext | NextUrqlAppContext; /** Passed to {@link withUrqlClient} returning the options a {@link Client} is created with. * * @param ssrExchange - the `ssrExchange` you must use in your `exchanges` array. * @param ctx - Passed when `getInitialProps` is used and set to Next.js’ {@link NextPageContext}. * @returns a {@link ClientOptions} configuration object to create a {@link Client} with. * * @remarks * You must define a `getClientConfig` function and pass it to {@link withUrqlClient}. * * This function defines the options passed to {@link initUrqlClient}. * It passes you an `ssrExchange` that you must use in your `exchanges` array. * * @example * ```ts * import { cacheExchange, fetchExchange } from '@urql/core'; * import { withUrqlClient } from 'next-urql'; * * const WrappedPage = withUrqlClient( * (ssrExchange) => ({ * url: 'https://YOUR_API', * exchanges: [cacheExchange, ssrExchange, fetchExchange], * }) * )(Page); * ``` */ type NextUrqlClientConfig = (ssrExchange: SSRExchange, ctx?: NextPageContext) => ClientOptions; /** Props that {@link withUrqlClient} components pass on to your component. */ interface WithUrqlProps { /** The {@link Client} that {@link withUrqlClient} created for your component. */ urqlClient?: Client; /** Next.js’ `pageProps` prop, as passed to it by Next.js. */ pageProps: any; /** The SSR data that {@link withUrqlClient} created for your component. */ urqlState?: SSRData; /** Resets the `Client` that on the client-side. * * @remarks * `resetUrqlClient` will force a new {@link Client} to be created * on the client-side, rather than the same `Client` with the same * server-side data to be reused. * * This may be used to force the cache and any state in the `Client` * to be cleared and reset. */ resetUrqlClient?(): void; [key: string]: any; } /** Options that may be passed to the {@link withUrqlClient} wrapper function. */ interface WithUrqlClientOptions { /** Enables automatic server-side rendering mode. * * @remarks * When enabled, {@link withUrqlClient} will add a `getInitialProps` * function to the resulting component, even if you haven't defined * one. * * This function will automatically capture `urql`'s SSR state on the * server-side and rehydrate it on the client-side, unless * {@link WithUrqlClientOptions.neverSuspend} is `true`. */ ssr?: boolean; /** Disables automatic server-side rendering, even if a `getInitialProps` function is defined. * * @remarks * When enabled, {@link withUrqlClient} will never execute queries * on the server-side automatically, and will instead rely on you * to do so manually. */ neverSuspend?: boolean; /** Enables reexecuting operations on the client-side after rehydration. * * @remarks * When enabled, `staleWhileRevalidate` will reexecute GraphQL queries on * the client-side, if they’ve been rehydrated from SSR state. * * This is useful if you, for instance, cache your server-side rendered * pages, or if you use `getStaticProps` and wish to get this data * updated. */ staleWhileRevalidate?: boolean; } /** Creates a wrapper for Next.js Page, App, or Document components that rehydrates `urql` state. * * @param getClientConfig - function used to create the {@link Client} * @param options - optional {@link WithUrqlClientOptions} configuration options * @returns a higher-order component function, which you can pass a Next.js page or app component. * * @remarks * Used to wrap a Next.js page or app component. It will create a {@link Client} and passes * it on to the child component and adds a React Provider for it. * * It will restore any page’s `pageProps.urqlState` with the {@link SSRExchange} and also * supports doing this automatically when the {@link WithUrqlClientOptions.ssr} option * is enabled. * * If you don’t define the above option, you will have to write `getServerSideProps` or * `getStaticProps` methods on your component manually. * * @see {@link https://urql.dev/goto/docs/advanced/server-side-rendering/#nextjs} for more documentation. * * @example * ```ts * import { cacheExchange, fetchExchange } from '@urql/core'; * import { withUrqlClient } from 'next-urql'; * * const WrappedPage = withUrqlClient( * (ssrExchange) => ({ * url: 'https://YOUR_API', * exchanges: [cacheExchange, ssrExchange, fetchExchange], * }), * { ssr: true }, * )(Page); * ``` */ declare function withUrqlClient(getClientConfig: NextUrqlClientConfig, options?: WithUrqlClientOptions): | typeof NextApp>(AppOrPage: C) => NextComponentType; /** Creates a {@link Client} the given options. * * @param clientOptions - {@link ClientOptions} to create the `Client` with. * @param canEnableSuspense - Enables React Suspense on the server-side for `react-ssr-prepass`. * @returns the created {@link Client} * * @remarks * `initUrqlClient` creates a {@link Client} with the given options, * like {@link createClient} does, but reuses the same client when * run on the client-side. * * As long as `canEnableSuspense` is set to `true`, it enables React Suspense * mode on the server-side for `react-ssr-prepass`. */ declare function initUrqlClient(clientOptions: ClientOptions, canEnableSuspense: boolean): Client; export { NextUrqlAppContext, NextUrqlClientConfig, NextUrqlContext, NextUrqlPageContext, WithUrqlClientOptions, WithUrqlProps, initUrqlClient, withUrqlClient };