Skip to content

About

Next.js App Router components and server helpers for the Crossly embeddable storefront.

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

@crossly/next

npm CI Next.js App Router types license

Crossly storefront components and server helpers for Next.js.

  • @crossly/next: the @crossly/react components (<Catalog>, <BuyButton>, <PriceIndex>, <PriceMark>, <CrosslyProvider>) behind a 'use client' boundary. Render them straight from a Server Component.
  • @crossly/next/server: typed fetch helpers for the same public embed endpoints the elements call: fetchCatalog, fetchPriceIndex, fetchPriceMark, createCheckout. They are wired to the Next.js data cache (revalidate, tags).

Everything is authenticated with your publishable key (crossly_pk_live_…). That key is public by design: it can read your own active listings and start a checkout, and nothing else.

Install

npm install @crossly/next
# pnpm add @crossly/next · yarn add @crossly/next · bun add @crossly/next

Peers: next (14+) and react (18.2+ / 19). Installs @crossly/react (and through it @crossly/embed).

# .env.local
NEXT_PUBLIC_CROSSLY_PK=crossly_pk_live_…   # mint in Crossly → Settings → Embeds

App Router

// app/layout.tsx (Server Component)
import { CrosslyProvider } from '@crossly/next';

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        <CrosslyProvider pk={process.env.NEXT_PUBLIC_CROSSLY_PK}>{children}</CrosslyProvider>
      </body>
    </html>
  );
}
// app/page.tsx (Server Component)
import { BuyButton, Catalog } from '@crossly/next';
import { fetchCatalog } from '@crossly/next/server';

export default async function Page() {
  const listings = await fetchCatalog({ limit: 6 }); // in the HTML, cached 60 s

  return (
    <main>
      <ul>
        {listings.map((l) => (
          <li key={l.id}>
            {l.title} {l.available && <BuyButton listing={l.id} label="Buy" />}
          </li>
        ))}
      </ul>
      <Catalog limit={24} fallback={<p>Loading…</p>} />
    </main>
  );
}

The full example, including a server-only shop driven by a Server Action, is in examples/app-router.

Do I need next/dynamic with ssr: false?

No. The components never touch window at import, and on the server they render only their fallback. In the browser, one shared dynamic import('@crossly/embed') registers the custom elements on first mount. Nothing ships to the server bundle that needs a DOM. next/dynamic still works if you want to code-split the components out of a route:

'use client';
import dynamic from 'next/dynamic';
const Catalog = dynamic(() => import('@crossly/next').then((m) => m.Catalog));

Client components

@crossly/next re-exports @crossly/react unchanged. The full reference (props, events, theming) is in the @crossly/react README. In short:

component element key props events
<Catalog> <crossly-catalog> limit (1–48, default 12), q onLoaded({ listings }), onError({ message })
<BuyButton> <crossly-buy-button> listing (required), quantity (1), label ("Buy now"), target (_self) onCheckout({ checkoutUrl }), onError
<PriceIndex> <crossly-index> slug (required), range (1y) onLoaded({ index }), onError
<PriceMark> <crossly-mark> bookKey (required, becomes the key attribute), range (1y) onLoaded({ mark }), onError
<CrosslyProvider> pk, api

Every component also takes pk, api, className, style, id and fallback.

Server helpers: @crossly/next/server

function request returns
fetchCatalog({ limit? }) GET /embed/v1/catalog?limit= EmbedListing[] (active listings, public fields, integer cents)
fetchPriceIndex(slug, { range? }) GET /embed/v1/vault/index/:slug?range= EmbedIndexWidget
fetchPriceMark(bookKey, { range? }) GET /embed/v1/vault/mark?bookKey=&range= EmbedMarkWidget
createCheckout({ items, returnUrl? }) POST /embed/v1/checkout { checkoutUrl, mode }

Every function also takes:

option default
pk process.env.CROSSLY_PK, then process.env.NEXT_PUBLIC_CROSSLY_PK Publishable key, sent as x-crossly-key.
api https://crossly.net/api API base.
origin none Sent as Origin. Set it only if your key has an origin allowlist (see below).
revalidate 60 Next.js data cache lifetime (fetch(…, { next: { revalidate } })). It matches the API's max-age=60. Reads only.
tags none Next.js cache tags, for revalidateTag(). Reads only.
signal none AbortSignal.

createCheckout is never cached (cache: 'no-store'). A failed request throws CrosslyApiError with the API's status, code (unauthorized, forbidden, not_found…) and message.

// app/actions.ts
'use server';
import { redirect } from 'next/navigation';
import { createCheckout } from '@crossly/next/server';

export async function buy(formData: FormData) {
  const { checkoutUrl } = await createCheckout({
    items: [{ listingId: String(formData.get('listingId')), quantity: 1 }],
    returnUrl: 'https://shop.example/thanks',
  });
  redirect(checkoutUrl); // the buyer pays on Crossly; no card data touches your server
}

Origin allowlists and server requests

Browsers send an Origin header, and the API checks it against the key's allowlist. A server request has no Origin. An empty allowlist permits any origin, so it also permits server requests. If your key has an allowlist, pass one of its origins as origin. A returnUrl must be on an allowed origin either way.

Not on the server

  • q (text search). A fresh catalog search requires a proof-of-work header from the caller. These helpers don't implement it, so they don't offer q.
  • Pagination. The catalog response also carries a signed nextCursor, but the 0.1.0 elements don't use it, so these helpers return the first page only.

Known limitations (client components)

From @crossly/embed 0.1.0: crossly:checkout is dispatched as cancelable, but the element navigates anyway. A q search on <Catalog> surfaces as onError, because the element doesn't send the proof-of-work header. Details are in the @crossly/react README.

Why @crossly/react is a dependency

It's a regular dependency: this package is a thin Next.js layer over it, and the two are versioned together. react and next are peers, so your app's copies are used.

Links

License

MIT

About

Next.js App Router components and server helpers for the Crossly embeddable storefront.

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages