fix: add sdk as script
This commit is contained in:
@@ -8,15 +8,13 @@ import SkipToMainContent from "@/components/SkipToMainContent";
|
||||
|
||||
import type { Metadata } from "next";
|
||||
import type { LangParams, LayoutArgs } from "@/types/params";
|
||||
import ContentstackLivePreview from "@contentstack/live-preview-utils";
|
||||
import InitLivePreview from "@/components/Current/LivePreview";
|
||||
|
||||
export const metadata: Metadata = {
|
||||
description: "New web",
|
||||
title: "Scandic Hotels New Web",
|
||||
};
|
||||
|
||||
ContentstackLivePreview.init();
|
||||
|
||||
export default function RootLayout({
|
||||
children,
|
||||
params,
|
||||
@@ -24,10 +22,10 @@ export default function RootLayout({
|
||||
return (
|
||||
<html lang={params.lang}>
|
||||
<head>
|
||||
<Script
|
||||
{/* <Script
|
||||
data-cookieconsent="ignore"
|
||||
src="/Static/dist/js/head.js?85c84c9e24ae8da3e7af"
|
||||
/>
|
||||
/> */}
|
||||
<Script
|
||||
data-cookieconsent="ignore"
|
||||
src="/Static/dist/js/inline.js?00133e5a37de35c51a5d"
|
||||
@@ -48,13 +46,14 @@ export default function RootLayout({
|
||||
data-cookieconsent="ignore"
|
||||
src="/Static/dist/js/ng/main.js?1705409330990"
|
||||
/>
|
||||
<Script
|
||||
{/* <Script
|
||||
data-cookieconsent="ignore"
|
||||
src="/Static/dist/js/main-ng.js?336b801d6b38eff10884"
|
||||
strategy="lazyOnload"
|
||||
/>
|
||||
/> */}
|
||||
</head>
|
||||
<body>
|
||||
<InitLivePreview />
|
||||
<LangPopup lang={params.lang} />
|
||||
<SkipToMainContent lang={params.lang} />
|
||||
{children}
|
||||
|
||||
@@ -10,6 +10,7 @@ import Hero from "@/components/Current/Hero";
|
||||
|
||||
import type { PageArgs, LangParams, UriParams } from "@/types/params";
|
||||
import type { GetCurrentBlockPageData } from "@/types/requests/currentBlockPage";
|
||||
import ContentstackLivePreview from "@contentstack/live-preview-utils";
|
||||
|
||||
export default async function CurrentContentPage({
|
||||
params,
|
||||
@@ -17,6 +18,7 @@ export default async function CurrentContentPage({
|
||||
}: PageArgs<LangParams, UriParams>) {
|
||||
try {
|
||||
console.log({ searchParams });
|
||||
ContentstackLivePreview.setConfigFromParams(searchParams);
|
||||
if (!searchParams.uri) {
|
||||
throw new Error("Bad URI");
|
||||
}
|
||||
@@ -26,6 +28,10 @@ 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);
|
||||
|
||||
13
components/Current/LivePreview/index.tsx
Normal file
13
components/Current/LivePreview/index.tsx
Normal file
@@ -0,0 +1,13 @@
|
||||
"use client";
|
||||
|
||||
import ContentstackLivePreview from "@contentstack/live-preview-utils";
|
||||
import { useEffect } from "react";
|
||||
|
||||
export default function InitLivePreview() {
|
||||
useEffect(() => {
|
||||
console.log("INIT");
|
||||
ContentstackLivePreview.init();
|
||||
}, []);
|
||||
|
||||
return null;
|
||||
}
|
||||
@@ -10,34 +10,38 @@ import ContentstackLivePreview from "@contentstack/live-preview-utils";
|
||||
export async function previewRequest<T>(
|
||||
query: string | DocumentNode,
|
||||
variables?: {}
|
||||
): Promise<Data<T>> {
|
||||
try {
|
||||
const hash = ContentstackLivePreview.hash;
|
||||
): Promise<Data<T> | null> {
|
||||
const hash = ContentstackLivePreview.hash;
|
||||
|
||||
if (!env.CMS_PREVIEW_URL || !env.CMS_PREVIEW_TOKEN) {
|
||||
throw new Error("No preview URL");
|
||||
}
|
||||
const headers = new Headers();
|
||||
|
||||
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 response = await fetch(env.CMS_PREVIEW_URL.toString(), {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: JSON.stringify({
|
||||
query,
|
||||
variables,
|
||||
}),
|
||||
});
|
||||
|
||||
const { data } = await response.json();
|
||||
|
||||
return data as Data<T>;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
throw new Error("Something went wrong");
|
||||
if (!hash) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!env.CMS_PREVIEW_URL || !env.CMS_PREVIEW_TOKEN) {
|
||||
throw new Error("No preview URL");
|
||||
}
|
||||
const headers = new Headers();
|
||||
|
||||
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 response = await fetch(env.CMS_PREVIEW_URL, {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: JSON.stringify({
|
||||
query,
|
||||
variables,
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
console.log(response);
|
||||
throw new Error("Cound not fetch preview content");
|
||||
}
|
||||
|
||||
const { data } = await response.json();
|
||||
|
||||
return data as Data<T>;
|
||||
}
|
||||
|
||||
@@ -34,16 +34,18 @@ export async function middleware(request: NextRequest) {
|
||||
);
|
||||
|
||||
const searchParams = new URLSearchParams(request.nextUrl.searchParams);
|
||||
searchParams.set("uri", pathNameWithoutLocale);
|
||||
|
||||
if (request.nextUrl.pathname.includes("preview")) {
|
||||
searchParams.set("uri", pathNameWithoutLocale.replace("/preview", ""));
|
||||
|
||||
|
||||
console.log("IN PREVIEW! Search params: ", request.nextUrl.searchParams);
|
||||
|
||||
ContentstackLivePreview.setConfigFromParams(request.nextUrl.searchParams);
|
||||
return NextResponse.rewrite(
|
||||
new URL(`/${locale}/preview?${searchParams.toString()}`, request.url)
|
||||
);
|
||||
}
|
||||
searchParams.set("uri", pathNameWithoutLocale);
|
||||
|
||||
switch (contentType) {
|
||||
case "currentContentPage":
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { SysAsset } from "../utils/asset"
|
||||
import type { Edges } from "../utils/edges"
|
||||
import type { ExternalLink } from "../utils/externalLink"
|
||||
import type { PageLink } from "../utils/pagelink";
|
||||
import type { PageLink } from "../utils/pageLink";
|
||||
import type { Typename } from "../utils/typename"
|
||||
|
||||
export type Hero = {
|
||||
|
||||
Reference in New Issue
Block a user