Skip to content

Server-side rendering

The elements run in the browser only, and every package is safe to import and render on the server. Nothing loads there, and nothing throws.

  • loadPesepay() resolves null when there is no DOM, whatever options you pass. Guard with if (!pesepay) return; rather than checking typeof window. An invalid option is reported in the browser, never as an error in a server render.
  • React. <Elements> loads nothing on the server and renders its children; each element component renders an empty <div>. The components are marked "use client", so the Next.js App Router can import them from a server component.
  • Vue. <Elements> loads nothing before it is mounted, and each element component renders an empty <div> on the server, in Nuxt too.
  • Angular. <pesepay-elements> loads nothing before the first render in the browser, and each element component renders as an empty host element.

The elements load once the page hydrates. A container you give the element can hold a loading placeholder: mount() appends rather than clearing.

With the App Router, the page stays a server component and the checkout is a client component: its event handlers are functions, which a server component can’t pass down. This example is built and prerendered with next build against the published packages.

app/pay/page.tsx
import { Checkout } from "./checkout";
// A server component. It renders on the server; the checkout inside it loads
// in the browser once the page hydrates.
export default function PayPage() {
// Your server created the transaction for this order and has its reference
// number.
const referenceNumber = "REF-1";
return (
<main>
<h1>Checkout</h1>
<Checkout referenceNumber={referenceNumber} />
</main>
);
}
app/pay/checkout.tsx
"use client";
import { useRouter } from "next/navigation";
import { CheckoutElement, Elements } from "@pesepay/react";
// A client component, because its event handlers run in the browser. On the
// server <Elements> loads nothing and the element renders an empty <div>.
export function Checkout({ referenceNumber }: { referenceNumber: string }) {
const router = useRouter();
return (
<Elements>
<CheckoutElement
options={{ referenceNumber }}
// The payment has ended and the element shows the result. Fulfil the
// order on your server, which Pesepay notifies too.
onFinished={(event) => {
if (event.outcome === "succeeded") router.push("/order-complete");
}}
// The methods could not load: Pesepay's hosted checkout takes the payment.
onLoadError={(event) => {
if (event.fallbackUrl) window.location.assign(event.fallbackUrl);
}}
/>
</Elements>
);
}