I have 2 separate implementations of the preview URL. One is working and the other is not.
Here is the one that is working…
Full preview URL: http://localhost:8008/landing-page/landing-page-a
Here is the one that is not working…
Full preview URL: https://localhost:4200/#/affiliate/landing-page/64/preview
The difference between the two implementations is that the localhost:8008 implementation is NextJS which is rendering pages server-side (working) and the localhost:4200 implementation is a CRA app which is rendering pages client-side.
So it seems that the preview URL will not work with client-side rendering. I realize that there is a lot of documentation concerning getting the preview URL working ( Getting the Preview URL Working ) and I went through this list carefully but I did not see any restriction regarding client-side rendering.
Here is the relevant code from the non-working example…
import React, { Dispatch, useEffect, useState } from "react";
import { useSelector, useDispatch } from "react-redux";
import { BuilderContent, BuilderComponent, useIsPreviewing } from "@builder.io/react";
import { builder } from "@builder.io/sdk";
import { RenderBuilderContent } from "./builder";
import { LandingPageModel } from "../../../model/LandingPageModel";
import { selectLandingPage } from "../../../../redux/affiliate/affiliate.selectors";
import {
getLandingPage,
getLandingPageById,
} from "../../../../redux/affiliate/affiliate.actions";
import { useParams } from "react-router-dom";
import { TemplateBindingModel } from "../../../model/TemplateBindingModel";
import { LandingPageComponentBindingModel } from "../../../model/LandingPageComponentBindingModel";
interface LandingPagePreviewProps {
templateBindings?: TemplateBindingModel;
}
const LandingPagePreview: React.FunctionComponent<LandingPagePreviewProps> = ({
templateBindings,
}) => {
const dispatch: Dispatch<any> = useDispatch();
const { id } = useParams<{ id: string }>();
const builderModelName = "page";
const isPreviewingInBuilder = useIsPreviewing();
const [notFound, setNotFound] = useState(false);
const [content, setContent] = useState<BuilderContent<any>>()
const [landingPage, setLandingPage] = useState<any>(null);
useEffect(() => {
dispatch(getLandingPageById(Number(id)));
}, [id]);
const selectedLandingPage: LandingPageModel = useSelector(selectLandingPage);
// Builder Public API Key set in .env file
builder.init(process.env.REACT_APP_BUILDER_IO_PUBLIC_API_KEY!);
useEffect(() => {
async function fetchContent() {
const content = await builder
.get(builderModelName, {
userAttributes: {
urlPath: '/' + selectedLandingPage?.name,
},
}).promise();
setContent(content);
setNotFound(!content);
}
console.log('fetching content for', selectedLandingPage?.name);
fetchContent();
console.log('fetched content:', content);
}, [selectedLandingPage]);
// if (notFound && !isPreviewingInBuilder) {
// return null;
// }
console.log('rendering preview')
console.log(builderModelName)
console.log(content)
return (
<div>
<RenderBuilderContent content={content} model={builderModelName} />
</div>
);
}
export default LandingPagePreview;
Note that if I open a new browser window and paste the preview URL it will work just fine. So nothing wrong with this implementation. Note also, that in the screenshot for the non-working preview URL, my custom components have been registered, so it is clear that the preview URL is working to some extent.
If I look at the console log for the the non-working preview, I see…
So the content cannot be pulled. On the working version the content is pulled, but server-side.
I would really like to get the non-working preview URL working. But I don’t think it is possible. Any ideas?