Frameworks and Server Rendering
Use CG Elements in a framework and on a server-rendered page.
Browser only
The SDK renders in the browser. It does not render on the server.
It is safe to import on the server. In Node, Next.js, or Nuxt, the import does not throw. registerElements() does nothing without a DOM. getElement() returns undefined.
Call the SDK from client-only code. Use optional chaining on the result.
getElement()?.render(offerResponse);Framework examples
Register and render inside an effect.
import { useEffect } from 'react';
import { registerElements, getElement } from '@covergenius/elements';
export function OfferPanel({ offerResponse, onChange }) {
useEffect(() => {
registerElements({ environment: 'production' });
const cgElement = getElement();
cgElement?.on('change', onChange);
cgElement?.render(offerResponse);
}, [offerResponse, onChange]);
return <cg-elements />;
}Calling on() on each render is safe. A new handler replaces the old one.
Register and render in onMounted.
import { onMounted } from 'vue';
import { registerElements, getElement } from '@covergenius/elements';
onMounted(() => {
registerElements({ environment: 'production' });
getElement()?.render(props.offerResponse);
});Tell Vue that cg-elements is a custom element. Add it to the isCustomElement option of your compiler.
Next.js and Nuxt
A client component still runs on the server during the first render. Do not call getElement()! with a non-null assertion. Put SDK calls in an effect or in onMounted.
Mobile apps
Use a WebView or an installed PWA. The SDK runs in any WebView that supports custom elements.
Last updated
Was this helpful?

