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 { Metadata } from "next";
|
||||||
import type { LangParams, LayoutArgs } from "@/types/params";
|
import type { LangParams, LayoutArgs } from "@/types/params";
|
||||||
import ContentstackLivePreview from "@contentstack/live-preview-utils";
|
import InitLivePreview from "@/components/Current/LivePreview";
|
||||||
|
|
||||||
export const metadata: Metadata = {
|
export const metadata: Metadata = {
|
||||||
description: "New web",
|
description: "New web",
|
||||||
title: "Scandic Hotels New Web",
|
title: "Scandic Hotels New Web",
|
||||||
};
|
};
|
||||||
|
|
||||||
ContentstackLivePreview.init();
|
|
||||||
|
|
||||||
export default function RootLayout({
|
export default function RootLayout({
|
||||||
children,
|
children,
|
||||||
params,
|
params,
|
||||||
@@ -24,10 +22,10 @@ export default function RootLayout({
|
|||||||
return (
|
return (
|
||||||
<html lang={params.lang}>
|
<html lang={params.lang}>
|
||||||
<head>
|
<head>
|
||||||
<Script
|
{/* <Script
|
||||||
data-cookieconsent="ignore"
|
data-cookieconsent="ignore"
|
||||||
src="/Static/dist/js/head.js?85c84c9e24ae8da3e7af"
|
src="/Static/dist/js/head.js?85c84c9e24ae8da3e7af"
|
||||||
/>
|
/> */}
|
||||||
<Script
|
<Script
|
||||||
data-cookieconsent="ignore"
|
data-cookieconsent="ignore"
|
||||||
src="/Static/dist/js/inline.js?00133e5a37de35c51a5d"
|
src="/Static/dist/js/inline.js?00133e5a37de35c51a5d"
|
||||||
@@ -48,13 +46,14 @@ export default function RootLayout({
|
|||||||
data-cookieconsent="ignore"
|
data-cookieconsent="ignore"
|
||||||
src="/Static/dist/js/ng/main.js?1705409330990"
|
src="/Static/dist/js/ng/main.js?1705409330990"
|
||||||
/>
|
/>
|
||||||
<Script
|
{/* <Script
|
||||||
data-cookieconsent="ignore"
|
data-cookieconsent="ignore"
|
||||||
src="/Static/dist/js/main-ng.js?336b801d6b38eff10884"
|
src="/Static/dist/js/main-ng.js?336b801d6b38eff10884"
|
||||||
strategy="lazyOnload"
|
strategy="lazyOnload"
|
||||||
/>
|
/> */}
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
<InitLivePreview />
|
||||||
<LangPopup lang={params.lang} />
|
<LangPopup lang={params.lang} />
|
||||||
<SkipToMainContent lang={params.lang} />
|
<SkipToMainContent lang={params.lang} />
|
||||||
{children}
|
{children}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ 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";
|
||||||
|
import ContentstackLivePreview from "@contentstack/live-preview-utils";
|
||||||
|
|
||||||
export default async function CurrentContentPage({
|
export default async function CurrentContentPage({
|
||||||
params,
|
params,
|
||||||
@@ -17,6 +18,7 @@ export default async function CurrentContentPage({
|
|||||||
}: PageArgs<LangParams, UriParams>) {
|
}: PageArgs<LangParams, UriParams>) {
|
||||||
try {
|
try {
|
||||||
console.log({ searchParams });
|
console.log({ searchParams });
|
||||||
|
ContentstackLivePreview.setConfigFromParams(searchParams);
|
||||||
if (!searchParams.uri) {
|
if (!searchParams.uri) {
|
||||||
throw new Error("Bad URI");
|
throw new Error("Bad URI");
|
||||||
}
|
}
|
||||||
@@ -26,6 +28,10 @@ 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);
|
||||||
|
|||||||
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>(
|
export async function previewRequest<T>(
|
||||||
query: string | DocumentNode,
|
query: string | DocumentNode,
|
||||||
variables?: {}
|
variables?: {}
|
||||||
): Promise<Data<T>> {
|
): Promise<Data<T> | null> {
|
||||||
try {
|
const hash = ContentstackLivePreview.hash;
|
||||||
const hash = ContentstackLivePreview.hash;
|
|
||||||
|
|
||||||
if (!env.CMS_PREVIEW_URL || !env.CMS_PREVIEW_TOKEN) {
|
if (!hash) {
|
||||||
throw new Error("No preview URL");
|
return null;
|
||||||
}
|
|
||||||
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 (!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);
|
const searchParams = new URLSearchParams(request.nextUrl.searchParams);
|
||||||
searchParams.set("uri", pathNameWithoutLocale);
|
|
||||||
|
|
||||||
if (request.nextUrl.pathname.includes("preview")) {
|
if (request.nextUrl.pathname.includes("preview")) {
|
||||||
|
searchParams.set("uri", pathNameWithoutLocale.replace("/preview", ""));
|
||||||
|
|
||||||
|
|
||||||
console.log("IN PREVIEW! Search params: ", request.nextUrl.searchParams);
|
console.log("IN PREVIEW! Search params: ", request.nextUrl.searchParams);
|
||||||
|
|
||||||
ContentstackLivePreview.setConfigFromParams(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)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
searchParams.set("uri", pathNameWithoutLocale);
|
||||||
|
|
||||||
switch (contentType) {
|
switch (contentType) {
|
||||||
case "currentContentPage":
|
case "currentContentPage":
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import type { SysAsset } from "../utils/asset"
|
import type { SysAsset } from "../utils/asset"
|
||||||
import type { Edges } from "../utils/edges"
|
import type { Edges } from "../utils/edges"
|
||||||
import type { ExternalLink } from "../utils/externalLink"
|
import type { ExternalLink } from "../utils/externalLink"
|
||||||
import type { PageLink } from "../utils/pagelink";
|
import type { PageLink } from "../utils/pageLink";
|
||||||
import type { Typename } from "../utils/typename"
|
import type { Typename } from "../utils/typename"
|
||||||
|
|
||||||
export type Hero = {
|
export type Hero = {
|
||||||
|
|||||||
Reference in New Issue
Block a user