Skip to Content
Octopus Energy Packages

Server-Side Rendering

Resolve summary inputs on the route and render the energy summary as a server component

Server-render the summary after the core EnergySummary tutorial. Unlike EnergyOverview, the summary does not need an interactive timeframe, so the cleanest production path is usually to fetch and derive the primitive summary on the route, then render it in a server component.

What you'll build

By the end of this guide, the route will resolve market supply points, fetch measurements for the fixed two-week summary window, derive the summary, and render it entirely on the server without client-side data fetching.

The server owns the fixed read path, Coral rendering, and keeps the authenticated GraphQL client out of the browser.

Resolve supply points and timeframe

Read route params, create your user-scoped GraphQL client, resolve market supply points, and compute the fixed summary timeframe.

app/accounts/[accountNumber]/properties/[propertyId]/summary/page.tsx
import {
  fetchMarketSupplyPoints,
  type UtilityMarkets,
} from "@octopus-energy/measurements/market-supply-points";
import { resolveSummaryTimeframe } from "@octopus-energy/measurements/summary";

const timezone = "Europe/London";

const utilityMarkets: UtilityMarkets = {
  // Replace these example market names with the values used by your territory.
  electricity: ["GBR_ELECTRICITY"],
  gas: ["GBR_GAS"],
};

export default async function SummaryPage({
  params,
}: PageProps<"/accounts/[accountNumber]/properties/[propertyId]/summary">) {
  const { accountNumber, propertyId } = await params;
  const client = getUserScopedGraphQLClient();

  const marketSupplyPoints = await fetchMarketSupplyPoints(client, {
    accountNumber,
    propertyId,
    utilityMarkets,
  });

  const timeframe = resolveSummaryTimeframe({ timezone });
}

The timeframe is fixed for the request. Because there is no user-controlled summary range, the route does not need URL storage or useTimeframe.

Fetch measurements on the route

Use the imperative fetchMeasurements helper with the same inputs used by the client tutorial.

app/accounts/[accountNumber]/properties/[propertyId]/summary/page.tsx
import { fetchMeasurements } from "@octopus-energy/measurements";

const measurements = await fetchMeasurements(client, {
  propertyId,
  marketSupplyPoints,
  timeframe,
});

Pass any product-specific measurement options, such as readingDirections, in both the server and client versions while migrating.

Create the primitive summary on the route

Group measurements with an ordered matching array and create the primitive summary model before rendering. Passing marketSupplyPoints creates one group per supply point and preserves the supply-point query order.

app/accounts/[accountNumber]/properties/[propertyId]/summary/page.tsx
import { groupMeasurements } from "@octopus-energy/measurements";
import { createMeasurementsSummary } from "@octopus-energy/measurements/summary";

const summary = createMeasurementsSummary(
  groupMeasurements(measurements, { matching: marketSupplyPoints }),
  {
    currencyFallback: "GBP",
    locale: "en-GB",
    timeframe,
  },
);

An empty summary is represented by summary.status === "empty". You can render that neutral data state with the same Coral shell.

Derive the energy type

The rendering code needs to know whether the account has electricity, gas, or both so it can format summary text accordingly. Derive this from the resolved market supply points.

app/accounts/[accountNumber]/properties/[propertyId]/summary/page.tsx
import {
  getMarketSupplyPointUtilities,
  type MarketSupplyPoint,
} from "@octopus-energy/measurements/market-supply-points";

function getEnergyType(marketSupplyPoints: MarketSupplyPoint[]) {
  const utilities = getMarketSupplyPointUtilities(marketSupplyPoints);

  if (utilities.length === 1 && utilities[0]) return utilities[0];
  return "energy";
}

const energyType = getEnergyType(marketSupplyPoints);

Render the summary inline

Inline the rendering from Render the Summary Card directly in the page. No "use client" directive is needed: Coral's summary components handle their own client boundary. The page no longer passes data through a separate component file.

app/accounts/[accountNumber]/properties/[propertyId]/summary/page.tsx
import { Typography } from "@octopus-energy/coral/atoms";
import {
  EnergySummary,
  EnergySummaryChart,
  EnergySummaryChartContainer,
  EnergySummaryChartGroup,
  EnergySummaryChartLabel,
  EnergySummaryCharts,
  EnergySummaryIndicator,
  EnergySummaryIndicators,
  EnergySummarySummary,
} from "@octopus-energy/coral/molecules";
import {
  getSummaryChartBarProps,
  getSummaryChartLabelProps,
  getSummaryIndicatorsProps,
} from "@octopus-energy/measurements/compat";
import {
  getSummaryChartBars,
  getSummaryChartLabels,
  getSummaryComparison,
  getSummaryIndicators,
} from "@octopus-energy/measurements/summary";

// ... data fetching from previous steps ...

const comparison = getSummaryComparison(summary);
const indicators = getSummaryIndicators(summary);
const chartBars = getSummaryChartBars(summary);
const chartLabels = getSummaryChartLabels(summary);

return (
  <EnergySummary error={false} loading={false}>
    <EnergySummarySummary>
      <Typography component="h4" textStyle="h4">
        At a glance
      </Typography>
      <Typography textStyle="body2">{formatComparison(comparison, energyType)}</Typography>

      {getSummaryIndicatorsProps(indicators).map((indicator) => (
        <EnergySummaryIndicators key={indicator.key}>
          <EnergySummaryIndicator direction={indicator.direction} />
          <Typography textStyle="body2">{formatIndicator(indicator)}</Typography>
        </EnergySummaryIndicators>
      ))}
    </EnergySummarySummary>

    <EnergySummaryCharts>
      <EnergySummaryChartContainer>
        <EnergySummaryChartLabel {...getSummaryChartLabelProps(chartLabels.weekBefore)} />
        <EnergySummaryChartGroup>
          {getSummaryChartBarProps(chartBars.weekBefore).map(({ key, ...chart }) => (
            <EnergySummaryChart key={key} {...chart} />
          ))}
        </EnergySummaryChartGroup>
        <EnergySummaryChartLabel value="Week before" />
      </EnergySummaryChartContainer>

      <EnergySummaryChartContainer>
        <EnergySummaryChartLabel {...getSummaryChartLabelProps(chartLabels.lastWeek)} />
        <EnergySummaryChartGroup>
          {getSummaryChartBarProps(chartBars.lastWeek).map(({ key, ...chart }) => (
            <EnergySummaryChart key={key} {...chart} />
          ))}
        </EnergySummaryChartGroup>
        <EnergySummaryChartLabel value="Last week" />
      </EnergySummaryChartContainer>
    </EnergySummaryCharts>
  </EnergySummary>
);

The formatComparison and formatIndicator helpers from the tutorial still apply. They are pure functions that work identically in a server component.

Implementation checkpoint

At this checkpoint, the route owns the complete read path and renders the summary entirely on the server in a single file.

FAQ

On this page