UNPKG

@prismicio/next

Version:

Helpers to integrate Prismic into Next.js apps

74 lines (73 loc) 3.06 kB
const require_runtime = require("../_virtual/_rolldown/runtime.cjs"); let _prismicio_client = require("@prismicio/client"); let next_script_js = require("next/script.js"); next_script_js = require_runtime.__toESM(next_script_js); let react = require("react"); let react_jsx_runtime = require("react/jsx-runtime"); let next_router_js = require("next/router.js"); //#region src/pages/PrismicPreview.tsx /** * React component that sets up Prismic Previews using the Prismic Toolbar. When the Prismic Toolbar * send events to the browser, such as on preview updates and exiting, this component will * automatically refresh the page with the changes. * * This component can be wrapped around your app or added anywhere in your app's tree. It must be * rendered on every page. */ const PrismicPreview = (props) => { const { repositoryName, updatePreviewURL = "/api/preview", exitPreviewURL = "/api/exit-preview", children } = props; const router = (0, next_router_js.useRouter)(); const toolbarSrc = (0, _prismicio_client.getToolbarSrc)(repositoryName); (0, react.useEffect)(() => { const controller = new AbortController(); window.addEventListener("prismicPreviewUpdate", onUpdate, { signal: controller.signal }); window.addEventListener("prismicPreviewEnd", onEnd, { signal: controller.signal }); if (window.location.href.startsWith(window.location.origin + router.basePath) && getPreviewCookieRepositoryName() === repositoryName && !router.isPreview) start(); function onEnd(event) { event.preventDefault(); fetch(router.basePath + exitPreviewURL, { signal: controller.signal }).then((res) => { if (!res.ok) { console.error(`[<PrismicPreview>] Failed to exit Preview Mode using the "${exitPreviewURL}" API endpoint. Does it exist?`); return; } refresh(); }).catch(() => {}); } function onUpdate(event) { event.preventDefault(); start(); } function start() { fetch(router.basePath + updatePreviewURL, { redirect: "manual", signal: controller.signal }).then((res) => { if (res.type !== "opaqueredirect") { console.error(`[<PrismicPreview>] Failed to start or update the preview using "${updatePreviewURL}". Does it exist?`); return; } refresh(); }).catch(() => {}); } function refresh() { router.replace(router.asPath, void 0, { scroll: false }); } return () => controller.abort(); }, [ exitPreviewURL, updatePreviewURL, repositoryName, router ]); return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [children, /* @__PURE__ */ (0, react_jsx_runtime.jsx)(next_script_js.default, { src: toolbarSrc, strategy: "lazyOnload" })] }); }; function getPreviewCookieRepositoryName() { const cookie = window.document.cookie.split("; ").find((row) => row.startsWith(`${_prismicio_client.cookie.preview}=`))?.split("=")[1]; return (decodeURIComponent(cookie ?? "").match(/"([^"]+)\.prismic\.io"/) || [])[1]; } //#endregion exports.PrismicPreview = PrismicPreview; //# sourceMappingURL=PrismicPreview.cjs.map