fix: fetch preview content
This commit is contained in:
@@ -1,10 +1,13 @@
|
|||||||
import { notFound } from "next/navigation";
|
import { notFound } from "next/navigation";
|
||||||
import fs from "node:fs/promises";
|
|
||||||
import path from "node:path";
|
|
||||||
|
|
||||||
import { request } from "@/lib/request";
|
import { previewRequest } from "@/lib/previewRequest";
|
||||||
import { GetCurrentBlockPage } from "@/lib/graphql/Query/CurrentBlockPage.graphql";
|
import { GetCurrentBlockPage } from "@/lib/graphql/Query/CurrentBlockPage.graphql";
|
||||||
|
|
||||||
|
import Aside from "@/components/Current/Aside";
|
||||||
|
import Blocks from "@/components/Current/Blocks";
|
||||||
|
import Header from "@/components/Current/Header";
|
||||||
|
import Hero from "@/components/Current/Hero";
|
||||||
|
|
||||||
import type { PageArgs, LangParams, UriParams } from "@/types/params";
|
import type { PageArgs, LangParams, UriParams } from "@/types/params";
|
||||||
import type { GetCurrentBlockPageData } from "@/types/requests/currentBlockPage";
|
import type { GetCurrentBlockPageData } from "@/types/requests/currentBlockPage";
|
||||||
|
|
||||||
@@ -18,42 +21,29 @@ export default async function CurrentContentPage({
|
|||||||
throw new Error("Bad URI");
|
throw new Error("Bad URI");
|
||||||
}
|
}
|
||||||
|
|
||||||
// const response = await request<GetCurrentBlockPageData>(GetCurrentBlockPage, { locale: params.lang, url: searchParams.uri })
|
const response = await previewRequest<GetCurrentBlockPageData>(
|
||||||
|
GetCurrentBlockPage,
|
||||||
// console.log(response.data.all_current_blocks_page.items[0].blocks)
|
{ locale: params.lang, url: searchParams.uri }
|
||||||
|
|
||||||
const filePath = path.join(
|
|
||||||
process.cwd(),
|
|
||||||
"mockCms",
|
|
||||||
params.lang,
|
|
||||||
searchParams.uri,
|
|
||||||
"data.json"
|
|
||||||
);
|
);
|
||||||
|
|
||||||
const data = await fs.readFile(filePath, { encoding: "utf-8" });
|
if (!response.data?.all_current_blocks_page?.total) {
|
||||||
|
console.log("#### DATA ####");
|
||||||
if (!data) {
|
console.log(response.data);
|
||||||
throw new Error("No data");
|
console.log("SearchParams URI: ", searchParams.uri);
|
||||||
|
throw new Error("Not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
const json = JSON.parse(data);
|
const page = response.data.all_current_blocks_page.items[0];
|
||||||
|
const images = page.hero?.imagesConnection;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{json.hero ? (
|
<Header lang={params.lang} pathname={searchParams.uri} />
|
||||||
<div
|
{images?.totalCount ? <Hero images={images.edges} /> : null}
|
||||||
dangerouslySetInnerHTML={{ __html: json.hero }}
|
<main className="main l-sections-wrapper" id="maincontent" role="main">
|
||||||
className="hero-content-overlay hero-content-widget"
|
<Blocks blocks={page.blocks} />
|
||||||
/>
|
<Aside blocks={page.aside} />
|
||||||
) : null}
|
</main>
|
||||||
{json.content ? (
|
|
||||||
<main
|
|
||||||
className="main l-sections-wrapper"
|
|
||||||
role="main"
|
|
||||||
id="maincontent"
|
|
||||||
dangerouslySetInnerHTML={{ __html: json.content }}
|
|
||||||
/>
|
|
||||||
) : null}
|
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -7,26 +7,35 @@ import type { Data } from "@/types/request";
|
|||||||
import type { DocumentNode } from "graphql";
|
import type { DocumentNode } from "graphql";
|
||||||
import ContentstackLivePreview from "@contentstack/live-preview-utils";
|
import ContentstackLivePreview from "@contentstack/live-preview-utils";
|
||||||
|
|
||||||
export async function request<T>(
|
export async function previewRequest<T>(
|
||||||
query: string | DocumentNode,
|
query: string | DocumentNode,
|
||||||
variables?: {}
|
variables?: {}
|
||||||
): Promise<Data<T>> {
|
): Promise<Data<T>> {
|
||||||
try {
|
try {
|
||||||
const hash = ContentstackLivePreview.hash;
|
const hash = ContentstackLivePreview.hash;
|
||||||
|
|
||||||
const response = await graphqlRequest<T>({
|
if (!env.CMS_PREVIEW_URL || !env.CMS_PREVIEW_TOKEN) {
|
||||||
document: query,
|
throw new Error("No preview URL");
|
||||||
requestHeaders: {
|
}
|
||||||
access_token: env.CMS_ACCESS_TOKEN,
|
const headers = new Headers();
|
||||||
"Content-Type": "application/json",
|
|
||||||
preview_token: env.CMS_PREVIEW_TOKEN,
|
headers.append("access_token", env.CMS_ACCESS_TOKEN);
|
||||||
live_preview: hash,
|
headers.append("Content-Type", "application/json");
|
||||||
},
|
headers.append("preview_token", env.CMS_PREVIEW_TOKEN);
|
||||||
url: env.CMS_PREVIEW_URL,
|
headers.append("live_preview", hash);
|
||||||
variables,
|
|
||||||
|
const response = await fetch(env.CMS_PREVIEW_URL.toString(), {
|
||||||
|
method: "POST",
|
||||||
|
headers,
|
||||||
|
body: JSON.stringify({
|
||||||
|
query,
|
||||||
|
variables,
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
return { data: response };
|
const { data } = await response.json();
|
||||||
|
|
||||||
|
return data as Data<T>;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
throw new Error("Something went wrong");
|
throw new Error("Something went wrong");
|
||||||
|
|||||||
@@ -38,18 +38,11 @@ export async function middleware(request: NextRequest) {
|
|||||||
|
|
||||||
if (request.nextUrl.pathname.includes("preview")) {
|
if (request.nextUrl.pathname.includes("preview")) {
|
||||||
console.log("IN PREVIEW! Search params: ", request.nextUrl.searchParams);
|
console.log("IN PREVIEW! Search params: ", request.nextUrl.searchParams);
|
||||||
if (request.nextUrl.searchParams.get("hash")) {
|
|
||||||
ContentstackLivePreview.setConfigFromParams(request.nextUrl.searchParams);
|
|
||||||
|
|
||||||
return NextResponse.rewrite(
|
ContentstackLivePreview.setConfigFromParams(request.nextUrl.searchParams);
|
||||||
new URL(
|
return NextResponse.rewrite(
|
||||||
`/${locale}/current-content-page?${searchParams.toString()}`,
|
new URL(`/${locale}/preview?${searchParams.toString()}`, request.url)
|
||||||
request.url
|
);
|
||||||
)
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
return Response.json("Not found!!!", { status: 404 });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (contentType) {
|
switch (contentType) {
|
||||||
|
|||||||
Reference in New Issue
Block a user