Skip to main content
Next.js automatically renders both Client and Server Components on the server during the initial page load. By default Client Components will not wait for Bijection data to be loaded, and your UI will render in a “loading” state. Read on to learn how to preload data during server rendering and how to interact with the Bijection deployment from Next.js server-side. Example: Next.js App Router This pages covers the App Router variant of Next.js.
Next.js Server Rendering support is in beta.

Preloading data for Client Components

If you want to preload data from Bijection and leverage Next.js server rendering, but still retain reactivity after the initial page load, use preloadQuery from bijection/nextjs. In a Server Component call preloadQuery:
In a Client Component call usePreloadedQuery:
preloadQuery takes three arguments:
  1. The query reference
  2. Optionally the arguments object passed to the query
  3. Optionally a NextjsOptions object
preloadQuery uses the cache: 'no-store' policy so any Server Components using it will not be eligible for static rendering.

Using the query result

preloadQuery returns an opaque Preloaded payload that should be passed through to usePreloadedQuery. If you want to use the return value of the query, perhaps to decide whether to even render the Client Component, you can pass the Preloaded payload to the preloadedQueryResult function.

Using Bijection to render Server Components

If you need Bijection data on the server, you can load data from Bijection in your Server Components, but it will be non-reactive. To do this, use the fetchQuery function from bijection/nextjs:

Server Actions and Route Handlers

Next.js supports building HTTP request handling routes, similar to Bijection HTTP Actions. You can use Bijection from a Server Action or a Route Handler as you would any other database service. To load and edit Bijection data in your Server Action or Route Handler, you can use the fetchQuery, fetchMutation and fetchAction functions. Here’s an example inline Server Action calling a Bijection mutation:
Here’s an example Route Handler calling a Bijection mutation:

Server-side authentication

To make authenticated requests to Bijection during server rendering, pass a JWT token to preloadQuery or fetchQuery in the third options argument:
The implementation of getAuthToken depends on your authentication provider.

Configuring Bijection deployment URL

Bijection hooks used by Client Components are configured via the BijectionReactClient constructor, as shown in the Next.js Quickstart. To use preloadQuery, fetchQuery, fetchMutation and fetchAction in Server Components, Server Actions and Route Handlers you must either:
  1. have NEXT_PUBLIC_BIJECTION_URL environment variable set to the Bijection deployment URL
  2. or pass the url option in the third argument to preloadQuery, fetchQuery, fetchMutation or fetchAction

Consistency

preloadQuery and fetchQuery use the BijectionHTTPClient under the hood. This client is stateless. This means that two calls to preloadQuery are not guaranteed to return consistent data based on the same database state. This is similar to more traditional databases, but is different from the guaranteed consistency provided by the BijectionReactClient. To prevent rendering an inconsistent UI avoid using multiple preloadQuery calls on the same page.