npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@commercengine/ssr-utils

v0.3.1

Published

Framework-agnostic cookie-backed SSR session utilities for Commerce Engine SDKs

Downloads

220

Readme

@commercengine/ssr-utils

Framework-agnostic cookie-backed SSR session utilities for Commerce Engine SDK integrations.

This package provides two levels of API:

  1. createSSRFactory (recommended) — a generic factory that handles singleton scoping, isomorphic token storage, and deduped client bootstrap. Framework bindings provide only the SDK construction callbacks and a server cookie adapter.

  2. Low-level primitivesServerTokenStorage, ClientTokenStorage, and CookieAdapter for manual wiring when full control is needed.

For Next.js and TanStack Start projects, prefer @commercengine/storefront — it ships ready-made wrappers at /nextjs and /tanstack-start that build on createSSRFactory.

Installation

npm install @commercengine/ssr-utils
# or
pnpm add @commercengine/ssr-utils

Quick Start — createSSRFactory

The factory centralizes the orchestration every SSR framework binding needs: isomorphic token storage selection, client/public singleton caching, and deduped client-side session bootstrap.

The framework author provides:

| Callback | Purpose | | ------------------------- | ---------------------------------------------------- | | createPublicSDK | Build the public (API-key-only) SDK — cached once | | createClientSessionSDK | Build a client session SDK with document.cookie storage | | createServerSessionSDK | Build a server session SDK with request-scoped cookie storage | | createServerCookieAdapter | Convert a framework request context into a CookieAdapter | | bootstrapSession | (optional) Client-side bootstrap callback |

SvelteKit

import { createSSRFactory } from "@commercengine/ssr-utils";
import {
  PublicStorefrontSDK,
  SessionStorefrontSDK,
  Environment,
} from "@commercengine/storefront-sdk";
import type { Cookies } from "@sveltejs/kit";

const config = {
  storeId: env.PUBLIC_STORE_ID,
  apiKey: env.PUBLIC_API_KEY,
  environment: Environment.Staging,
};

export const storefront = createSSRFactory<
  PublicStorefrontSDK,
  SessionStorefrontSDK,
  Cookies
>({
  createPublicSDK: () => new PublicStorefrontSDK(config),
  createClientSessionSDK: (tokenStorage) =>
    new SessionStorefrontSDK({ ...config, tokenStorage }),
  createServerSessionSDK: (_cookies, tokenStorage) =>
    new SessionStorefrontSDK({ ...config, tokenStorage }),
  createServerCookieAdapter: (cookies) => ({
    get: (name) => cookies.get(name) ?? null,
    set: (name, value, opts) => cookies.set(name, value, opts),
    delete: (name) => cookies.delete(name),
  }),
  bootstrapSession: (sdk) => sdk.ensureAccessToken().then(() => {}),
});

// +page.server.ts
export async function load({ cookies }) {
  const sdk = storefront.session(cookies);
  const { data } = await sdk.cart.getCart();
  return { cart: data };
}

Nuxt / h3

import { createSSRFactory } from "@commercengine/ssr-utils";
import {
  PublicStorefrontSDK,
  SessionStorefrontSDK,
  Environment,
} from "@commercengine/storefront-sdk";
import { getCookie, setCookie, deleteCookie, type H3Event } from "h3";

const config = {
  storeId: process.env.STORE_ID!,
  apiKey: process.env.API_KEY!,
  environment: Environment.Production,
};

export const storefront = createSSRFactory<
  PublicStorefrontSDK,
  SessionStorefrontSDK,
  H3Event
>({
  createPublicSDK: () => new PublicStorefrontSDK(config),
  createClientSessionSDK: (tokenStorage) =>
    new SessionStorefrontSDK({ ...config, tokenStorage }),
  createServerSessionSDK: (_event, tokenStorage) =>
    new SessionStorefrontSDK({ ...config, tokenStorage }),
  createServerCookieAdapter: (event) => ({
    get: (name) => getCookie(event, name) ?? null,
    set: (name, value, opts) => setCookie(event, name, value, opts),
    delete: (name) => deleteCookie(event, name),
  }),
  bootstrapSession: (sdk) => sdk.ensureAccessToken().then(() => {}),
});

// Server API route
export default defineEventHandler(async (event) => {
  const sdk = storefront.session(event);
  return sdk.customer.getCustomer();
});

Astro

import { createSSRFactory } from "@commercengine/ssr-utils";
import {
  PublicStorefrontSDK,
  SessionStorefrontSDK,
  Environment,
} from "@commercengine/storefront-sdk";
import type { AstroCookies } from "astro";

const config = {
  storeId: import.meta.env.STORE_ID,
  apiKey: import.meta.env.API_KEY,
  environment: Environment.Staging,
};

export const storefront = createSSRFactory<
  PublicStorefrontSDK,
  SessionStorefrontSDK,
  AstroCookies
>({
  createPublicSDK: () => new PublicStorefrontSDK(config),
  createClientSessionSDK: (tokenStorage) =>
    new SessionStorefrontSDK({ ...config, tokenStorage }),
  createServerSessionSDK: (_cookies, tokenStorage) =>
    new SessionStorefrontSDK({ ...config, tokenStorage }),
  createServerCookieAdapter: (cookies) => ({
    get: (name) => cookies.get(name)?.value ?? null,
    set: (name, value, opts) => cookies.set(name, value, opts),
    delete: (name) => cookies.delete(name),
  }),
  bootstrapSession: (sdk) => sdk.ensureAccessToken().then(() => {}),
});

SolidStart

import { createSSRFactory } from "@commercengine/ssr-utils";
import {
  PublicStorefrontSDK,
  SessionStorefrontSDK,
  Environment,
} from "@commercengine/storefront-sdk";
import { getCookie, setCookie, deleteCookie, type H3Event } from "vinxi/http";

const config = {
  storeId: import.meta.env.VITE_STORE_ID,
  apiKey: import.meta.env.VITE_API_KEY,
  environment: Environment.Staging,
};

export const storefront = createSSRFactory<
  PublicStorefrontSDK,
  SessionStorefrontSDK,
  H3Event
>({
  createPublicSDK: () => new PublicStorefrontSDK(config),
  createClientSessionSDK: (tokenStorage) =>
    new SessionStorefrontSDK({ ...config, tokenStorage }),
  createServerSessionSDK: (_event, tokenStorage) =>
    new SessionStorefrontSDK({ ...config, tokenStorage }),
  createServerCookieAdapter: (event) => ({
    get: (name) => getCookie(event, name) ?? null,
    set: (name, value, opts) => setCookie(event, name, value, opts),
    delete: (name) => deleteCookie(event, name),
  }),
  bootstrapSession: (sdk) => sdk.ensureAccessToken().then(() => {}),
});

// In a server function
import { getRequestEvent } from "solid-js/web";
const event = getRequestEvent()!;
const sdk = storefront.session(event);

Factory Behavior

| Method | Client | Server | | ---------------- | ---------------------------------------- | ----------------------------------------- | | public() | Cached singleton | Cached singleton | | session() | Cached singleton (ClientTokenStorage) | Throws — context required | | session(ctx) | Returns cached client singleton (ctx ignored) | Fresh instance (ServerTokenStorage) | | bootstrap() | Runs bootstrapSession (deduped) | No-op (resolves immediately) |

The factory does not cache server session instances — frameworks layer their own per-request caching on top (React cache(), SvelteKit event.locals, Nuxt request context, etc.).

bootstrap() only dedupes concurrent/in-flight calls. It is not permanent memoization: once a bootstrap run settles, a later call will execute the bootstrap callback again. That allows flows like logout or session clear to bootstrap a fresh anonymous session later.

Low-Level API

For manual wiring without the factory:

ServerTokenStorage

Cookie-backed token storage for server request contexts.

import { ServerTokenStorage, type CookieAdapter } from "@commercengine/ssr-utils";

const adapter: CookieAdapter = {
  get: (name) => /* framework cookie get */ null,
  set: (name, value, options) => { /* framework cookie set */ },
  delete: (name) => { /* framework cookie delete */ },
};

const storage = new ServerTokenStorage(adapter, {
  prefix: "ce_",    // cookie name prefix
  maxAge: 2592000,   // 30 days
  path: "/",
  secure: true,
  sameSite: "lax",
});

ClientTokenStorage

Browser-side token storage backed by document.cookie. Uses the same cookie key naming convention as ServerTokenStorage so cookies are shared seamlessly between server and client.

import { ClientTokenStorage } from "@commercengine/ssr-utils";

const storage = new ClientTokenStorage({ prefix: "ce_" });

createCookieAdapter

Helper for cookie stores that expose get (returning { value } objects), set, and delete methods — e.g. Next.js cookies().

import { createCookieAdapter } from "@commercengine/ssr-utils";
import { cookies } from "next/headers";

const adapter = createCookieAdapter(await cookies());

Mental Model

  • Public SDK — API-key-backed, no session. Safe for build-time, prerender, and static generation.
  • Session SDK — cookie-backed session with automatic token lifecycle. Pass a framework request context on the server; on the client the factory manages a singleton backed by document.cookie.

This package intentionally does not provide build-time token caching. Public prerender/build reads should use the public SDK directly.

Related Packages

License

All Rights Reserved