Using with Next.js
The instructions vary depending on the router you use.
Since the Firmhouse JS SDK relies on the DOMContentLoaded
event for initialization by default, it is important to ensure that we initialize the SDK before this event occurs. Next.js provides us with the beforeInteractive
strategy for loading scripts before this event.
If you are using Pages Router, you can use the following template to add the Firmhouse JS SDK to your Next.js app.
You need to set theNEXT_PUBLIC_STOREFRONT_API_TOKEN
environment variable to your Storefront API token. Follow this guide to find out how to get one.
import Script from "next/script";
export default function Page() {
const firmhouseConfig = {
storefrontToken: process.env.NEXT_PUBLIC_STOREFRONT_API_TOKEN,
};
return (
<main>
<Script strategy="beforeInteractive">
{`window.Firmhouse = ${JSON.stringify(firmhouseConfig)}`}
</Script>
<Script
src="https://storefrontjs.firmhouse.com/dist/storefront.js"
strategy="beforeInteractive"
/>
<button
onClick={() => {
window.Firmhouse.showCart();
}}
>
Show cart
</button>
</main>
);
}
Last updated
Was this helpful?