Hello,
I believe that I have followed every step correctly according to the beginners’ guide tutorial but I’m still seeing issues. I can access my website through http://localhost:3000/ and it also shows in the builder but somehow it doesn’t work.
Please help, I’m lost.
Please fill out as many of the following questions as possible so we can help you promptly! If you don’t know how to answer or it does not apply, feel free to remove it or leave it blank.
Builder content link
Builder public api key
go to 945d216d92e14e288fcb30e5b308ece6 and copy your PUBLIC api key
What are you trying to accomplish
I want to integrate builder on a landing page*
Code stack you are integrating Builder with
NextJS, react, TS
Reproducible code example
import React from “react”;
import { useRouter } from “next/router”;
import { BuilderComponent, builder, useIsPreviewing } from “@builder.io/react”;
import { BuilderContent } from “@builder.io/sdk”;
import DefaultErrorPage from “next/error”;
import Head from “next/head”;
import { GetStaticProps } from “next”;
// Replace with your Public API Key
builder.init(“945d216d92e14e288fcb30e5b308ece6”);
// Define a function that fetches the Builder
// content for a given page
export const getStaticProps: GetStaticProps = async ({ params }) => {
// Fetch the builder content for the given page
const page = await builder
.get(“page”, {
userAttributes: {
urlPath: “/” + ((params?.page as string)?.join(“/”) || “”),
},
})
.toPromise();
// Return the page content as props
return {
props: {
page: page || null,
},
// Revalidate the content every 5 seconds
revalidate: 5,
};
};
// Define a function that generates the
// static paths for all pages in Builder
export async function getStaticPaths() {
// Get a list of all pages in Builder
const pages = await builder.getAll(“page”, {
// We only need the URL field
fields: “data.url”,
options: { noTargeting: true },
});
// Generate the static paths for all pages in Builder
return {
paths: pages.map((page) => ${page.data?.url}
).filter(url => url !== ‘/’),
fallback: ‘blocking’,
};
}
// Define the Page component
export default function Page({ page }: { page: BuilderContent | null }) {
const router = useRouter();
const isPreviewing = useIsPreviewing();
// If the page content is not available
// and not in preview mode, show a 404 error page
if (!page && !isPreviewing) {
return ;
}
// If the page content is available, render
// the BuilderComponent with the page content
return (
<>
{page?.data?.title}
{/* Render the Builder page */}
<BuilderComponent model=“page” content={page || undefined} />
</>
);
}