fix: set search params in page
This commit is contained in:
@@ -17,8 +17,11 @@ export default async function CurrentContentPage({
|
||||
searchParams,
|
||||
}: PageArgs<LangParams, UriParams>) {
|
||||
try {
|
||||
console.log({ searchParams });
|
||||
ContentstackLivePreview.setConfigFromParams(searchParams);
|
||||
const livePreviewHash = searchParams["live_preview"];
|
||||
if (livePreviewHash) {
|
||||
ContentstackLivePreview.setConfigFromParams(searchParams);
|
||||
}
|
||||
|
||||
if (!searchParams.uri) {
|
||||
throw new Error("Bad URI");
|
||||
}
|
||||
@@ -28,10 +31,6 @@ export default async function CurrentContentPage({
|
||||
{ locale: params.lang, url: searchParams.uri }
|
||||
);
|
||||
|
||||
if (!response) {
|
||||
return <h1>No Hash recieved! Hash: {ContentstackLivePreview.hash}</h1>;
|
||||
}
|
||||
|
||||
if (!response.data?.all_current_blocks_page?.total) {
|
||||
console.log("#### DATA ####");
|
||||
console.log(response.data);
|
||||
@@ -47,6 +46,7 @@ export default async function CurrentContentPage({
|
||||
<Header lang={params.lang} pathname={searchParams.uri} />
|
||||
{images?.totalCount ? <Hero images={images.edges} /> : null}
|
||||
<main className="main l-sections-wrapper" id="maincontent" role="main">
|
||||
<h1>{page.title}</h1>
|
||||
<Blocks blocks={page.blocks} />
|
||||
<Aside blocks={page.aside} />
|
||||
</main>
|
||||
|
||||
@@ -5,8 +5,10 @@ import { useEffect } from "react";
|
||||
|
||||
export default function InitLivePreview() {
|
||||
useEffect(() => {
|
||||
console.log("INIT");
|
||||
ContentstackLivePreview.init();
|
||||
if (!ContentstackLivePreview.livePreview) {
|
||||
console.log("INIT");
|
||||
ContentstackLivePreview.init();
|
||||
}
|
||||
}, []);
|
||||
|
||||
return null;
|
||||
|
||||
@@ -10,38 +10,34 @@ import ContentstackLivePreview from "@contentstack/live-preview-utils";
|
||||
export async function previewRequest<T>(
|
||||
query: string | DocumentNode,
|
||||
variables?: {}
|
||||
): Promise<Data<T> | null> {
|
||||
const hash = ContentstackLivePreview.hash;
|
||||
): Promise<Data<T>> {
|
||||
try {
|
||||
const hash = ContentstackLivePreview.hash;
|
||||
|
||||
if (!hash) {
|
||||
return null;
|
||||
}
|
||||
if (!hash) {
|
||||
throw new Error("No hash received");
|
||||
}
|
||||
|
||||
if (!env.CMS_PREVIEW_URL || !env.CMS_PREVIEW_TOKEN) {
|
||||
throw new Error("No preview URL");
|
||||
}
|
||||
const headers = new Headers();
|
||||
if (!env.CMS_PREVIEW_URL || !env.CMS_PREVIEW_TOKEN) {
|
||||
throw new Error("No preview URL or token");
|
||||
}
|
||||
|
||||
headers.append("access_token", env.CMS_ACCESS_TOKEN);
|
||||
headers.append("Content-Type", "application/json");
|
||||
headers.append("preview_token", env.CMS_PREVIEW_TOKEN);
|
||||
headers.append("live_preview", hash);
|
||||
const headers = new Headers({
|
||||
access_token: env.CMS_ACCESS_TOKEN,
|
||||
preview_token: env.CMS_PREVIEW_TOKEN,
|
||||
live_preview: hash,
|
||||
});
|
||||
|
||||
const response = await fetch(env.CMS_PREVIEW_URL, {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: JSON.stringify({
|
||||
query,
|
||||
const response = await graphqlRequest<T>({
|
||||
document: query,
|
||||
requestHeaders: headers,
|
||||
url: env.CMS_PREVIEW_URL,
|
||||
variables,
|
||||
}),
|
||||
});
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
console.log(response);
|
||||
throw new Error("Cound not fetch preview content");
|
||||
return { data: response };
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
throw new Error("Something went wrong");
|
||||
}
|
||||
|
||||
const { data } = await response.json();
|
||||
|
||||
return data as Data<T>;
|
||||
}
|
||||
|
||||
@@ -38,9 +38,6 @@ export async function middleware(request: NextRequest) {
|
||||
if (request.nextUrl.pathname.includes("preview")) {
|
||||
searchParams.set("uri", pathNameWithoutLocale.replace("/preview", ""));
|
||||
|
||||
|
||||
console.log("IN PREVIEW! Search params: ", request.nextUrl.searchParams);
|
||||
|
||||
return NextResponse.rewrite(
|
||||
new URL(`/${locale}/preview?${searchParams.toString()}`, request.url)
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user