naren
December 28, 2024, 2:13am
1
I am using Next.js 14.0.4 and following the instructions at: builder/packages/utils at main · BuilderIO/builder · GitHub
and at
Builder content gets a tiny image pixel added to it in order for Builder to keep track of pageviews for your content. It must be included on any content that is served from Builder, but sometimes you might need to make changes to the format in order to meet your company’s internal guidelines (such as accessibility). In order to make changes, such as adding a specifc alt tag, you can use the Builder utils library to modify the content that is fetched from the Builder API.
import { setPixelProper…
My code looks like:
const builderModelName = 'page';
const content = await builder
.get(builderModelName, {
userAttributes: {
// Use the page path specified in the URL to fetch the content
urlPath: '/' + (props?.params?.page?.join('/') || ''),
},
cachebust: true,
cacheSeconds: 10,
options: { includeRefs: true },
})
.toPromise() || null;
setPixelProperties(content, { alt: 'Builder.io pixel tag' })
The alt tag still shows up as an empty string (“”). ie. there is indeed an alt tag present but I am unable to modify its value. Anything changed with the API?
Hi @naren ,
We attempted to reproduce the issue using the dependencies listed below, but everything seems to be working fine on our end.
"dependencies": {
"@builder.io/dev-tools": "^1.2.0",
"@builder.io/react": "^7.0.1",
"@builder.io/sdk": "^5.0.0",
"@builder.io/utils": "^1.1.23",
"@builder.io/widgets": "^2.0.3",
"next": "14.0.4",
"react": "^18",
"react-dom": "^18"
},
Here is the complete code that we tested
import React from "react";
import { useRouter } from "next/router";
import { BuilderComponent, builder, useIsPreviewing } from "@builder.io/react";
import { setPixelProperties } from "@builder.io/utils";
import DefaultErrorPage from "next/error";
import Head from "next/head";
import { BuilderContent } from "@builder.io/sdk";
import "../builder-registry";
builder.init(process.env.NEXT_PUBLIC_BUILDER_API_KEY!);
export async function getServerSideProps({ params }) {
const mode = "page"; // Define your model name here
const page =
(await builder
.get(mode, {
userAttributes: {
urlPath: "/" + ((params?.page as string[])?.join("/") || ""),
},
}) // Define your query parameters properly
.toPromise()) || null;
setPixelProperties(page, { alt: "pixel tag from builder" });
return {
props: {
page,
},
};
}
// 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 <DefaultErrorPage statusCode={404} />;
}
// If the page content is available, render
// the BuilderComponent with the page content
return (
<>
<Head>
<title>{page?.data?.title}</title>
</Head>
{/* Render the Builder page */}
<BuilderComponent
model="page"
content={page || undefined}
/>
</>
);
}
Visual Development for React, Vue, Svelte, Qwik, and more - BuilderIO/builder
We hope this helps! If you are still experiencing any issues, please help us by sharing reproducible steps, and we will be happy to assist you further.
Thanks,
naren
January 6, 2025, 12:06am
3
Thank you very much @manish-sharma . Updating the libraries worked.
Thanks a lot!