Skip to Content
Octopus Energy Packages

Server-Render the First View

Resolve initial overview inputs on the route and prefetch the first measurement query

Server-render the first view after Build a Shareable Overview. This guide does not move the whole overview to React Server Components. Instead, the route resolves the first overview inputs, prefetches the first measurement query, and hydrates TanStack Query for the client overview.

What you'll build

By the end of this guide, the route will resolve the initial overview inputs, prefetch the first measurementsQueryOptions result, and hydrate TanStack Query so the client overview starts with server-fetched data.

The client still owns Coral interactions, URL-backed timeframe state, preferences, and measurement refetches after the user changes timeframe.

Pages Router

This guide uses the App Router. For the Pages Router, a few adjustments are needed (parser helpers, timeframe serialization). See the Pages Router FAQ below.

Resolve supply points and timeframe

Parse the URL timeframe params on the route with the server-side nuqs helpers, resolve market supply points, then use those supply points to resolve an anchor date when the URL does not provide a date. Build the concrete initialTimeframe from those inputs.

app/accounts/[accountNumber]/properties/[propertyId]/consumption/page.tsx
import { resolveAnchorDate } from "@octopus-energy/measurements/anchor-date";
import type { UtilityMarkets } from "@octopus-energy/measurements/market-supply-points";
import { fetchMarketSupplyPoints } from "@octopus-energy/measurements/market-supply-points";
import { createTimeframeSearchParams } from "@octopus-energy/measurements/storage/nuqs/server";
import { resolveTimeframe } from "@octopus-energy/measurements/timeframe";
import { createSearchParamsCache } from "nuqs/server";

const utilityMarkets: UtilityMarkets = {
  electricity: ["GBR_ELECTRICITY"],
  gas: ["GBR_GAS"],
};
const timezone = "Europe/London";

const searchParamsCache = createSearchParamsCache(createTimeframeSearchParams());

const { date, interval, period } = await searchParamsCache.parse(searchParams);
const marketSupplyPoints = await fetchMarketSupplyPoints(client, {
  accountNumber,
  propertyId,
  utilityMarkets,
});

const initialDate =
  date ??
  (await resolveAnchorDate(client, {
    interval,
    marketSupplyPoints,
    period,
    propertyId,
    timezone,
  }));

const initialTimeframe = resolveTimeframe({
  date: initialDate,
  interval,
  period,
  timezone,
});

Prefetch measurements

Prefetch the measurementsQueryOptions query for the resolved initialTimeframe. The client overview receives the resolved supply points as props, so it does not need a client-side supply point query.

app/accounts/[accountNumber]/properties/[propertyId]/consumption/page.tsx
import { measurementsQueryOptions } from "@octopus-energy/measurements/react-query";
import { QueryClient } from "@tanstack/react-query";

const queryClient = new QueryClient();

if (marketSupplyPoints.length > 0) {
  await queryClient.prefetchQuery(
    measurementsQueryOptions(client, {
      marketSupplyPoints,
      propertyId,
      timeframe: initialTimeframe,
    }),
  );
}

Hydrate the client overview

Wrap the client overview in HydrationBoundary and pass the server-resolved initialTimeframe and marketSupplyPoints as props.

app/accounts/[accountNumber]/properties/[propertyId]/consumption/page.tsx
import { dehydrate, HydrationBoundary } from "@tanstack/react-query";

return (
  <HydrationBoundary state={dehydrate(queryClient)}>
    <ConsumptionOverview
      initialTimeframe={initialTimeframe}
      marketSupplyPoints={marketSupplyPoints}
      propertyId={propertyId}
    />
  </HydrationBoundary>
);

Use the initial timeframe in the client

Accept the server-resolved initialTimeframe in the client overview and pass it to useTimeframe as the default. The client still owns URL-backed timeframe state, so URL params or later user changes can update the timeframe after the first render.

_components/consumption-overview.tsx
"use client";

import type { MarketSupplyPoint } from "@octopus-energy/measurements/market-supply-points";
import { measurementsQueryOptions } from "@octopus-energy/measurements/react-query";
import {
  createNuqsAdapter,
  createPreferencesSearchParams,
  createTimeframeSearchParams,
} from "@octopus-energy/measurements/storage/nuqs/client";
import { type Timeframe, useTimeframe } from "@octopus-energy/measurements/timeframe";
import { useQuery } from "@tanstack/react-query";
import { useQueryStates } from "nuqs";

type Props = {
  initialTimeframe: Timeframe;
  marketSupplyPoints: MarketSupplyPoint[];
  propertyId: string;
};

export function ConsumptionOverview({ initialTimeframe, marketSupplyPoints, propertyId }: Props) {
  const client = useGraphQLClient();
  const preferenceScopes =
    marketSupplyPoints.length > 1
      ? marketSupplyPoints.map(({ marketSupplyPointId }) => marketSupplyPointId)
      : undefined;

  const [searchParams, setSearchParams] = useQueryStates({
    ...createTimeframeSearchParams(),
    ...createPreferencesSearchParams({ scopes: preferenceScopes }),
  });

  const [timeframe, timeframeActions] = useTimeframe({
    defaults: initialTimeframe,
    storage: createNuqsAdapter(searchParams, setSearchParams),
    timezone: initialTimeframe.timezone,
  });

  const measurementsQuery = useQuery(
    measurementsQueryOptions(client, {
      marketSupplyPoints,
      propertyId,
      timeframe,
    }),
  );
}

The client calls measurementsQueryOptions with the same propertyId, marketSupplyPoints, and first timeframe used on the route. When the query key matches the prefetched server query, TanStack Query serves the hydrated result immediately.

Implementation checkpoint

At this checkpoint, the route resolves market supply points, the initial URL-backed timeframe, and the first measurements query before rendering. The client receives those same inputs and continues with hydrated data plus normal URL-backed interactions.

FAQ

On this page