Hi there - I’m looking for instructions on how to import the Shapediver API and library correctly in a NextJS environment.
I was looking at the react setup instructions in the documentation and noticed that there were errors: react setup - CodeSandbox.
Importing at the top of my React component like so:
import { api } from "@shapediver/viewer";
This results in a next.js error “document is not defined”.
This is probably because the script is attempting to load before the page has been rendered. My current fix for this is to load the script asynchronously like so:
useEffect(() => {
loadShapeDiver(async () => {
const SDV = window.SDV;
where loadShapeDiver looks like:
export const loadShapeDiver = (callback) => {
const existingScript = document.getElementById("shapeDiver");
if (!existingScript) {
const script = document.createElement("script");
script.src = `https://viewer.shapediver.com/v3/1.13.10/bundle.js`;
script.id = "shapeDiver";
document.body.appendChild(script);
script.onload = () => {
if (callback) callback();
};
}
if (existingScript && callback) callback();
};
I’d prefer to use the library over this method. What’s the right way to do so?
Thanks so much!