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