@prismicio/next 2.3.4-pr.175.c252006 → 2.3.4-pr.175.e026048
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/PrismicPreview.cjs +5 -3
- package/dist/PrismicPreview.cjs.map +1 -1
- package/dist/PrismicPreview.js +5 -3
- package/dist/PrismicPreview.js.map +1 -1
- package/dist/exitPreview.cjs +1 -2
- package/dist/exitPreview.cjs.map +1 -1
- package/dist/exitPreview.d.cts.map +1 -1
- package/dist/exitPreview.d.ts.map +1 -1
- package/dist/exitPreview.js +1 -2
- package/dist/exitPreview.js.map +1 -1
- package/dist/lib/getSafePath.cjs +18 -0
- package/dist/lib/getSafePath.cjs.map +1 -0
- package/dist/lib/getSafePath.js +18 -0
- package/dist/lib/getSafePath.js.map +1 -0
- package/dist/package.cjs +1 -1
- package/dist/package.js +1 -1
- package/dist/pages/PrismicPreview.cjs +3 -2
- package/dist/pages/PrismicPreview.cjs.map +1 -1
- package/dist/pages/PrismicPreview.js +3 -2
- package/dist/pages/PrismicPreview.js.map +1 -1
- package/dist/pages/exitPreview.cjs +1 -2
- package/dist/pages/exitPreview.cjs.map +1 -1
- package/dist/pages/exitPreview.d.cts.map +1 -1
- package/dist/pages/exitPreview.d.ts.map +1 -1
- package/dist/pages/exitPreview.js +1 -2
- package/dist/pages/exitPreview.js.map +1 -1
- package/dist/pages/redirectToPreviewURL.cjs +9 -4
- package/dist/pages/redirectToPreviewURL.cjs.map +1 -1
- package/dist/pages/redirectToPreviewURL.js +9 -4
- package/dist/pages/redirectToPreviewURL.js.map +1 -1
- package/dist/redirectToPreviewURL.cjs +3 -3
- package/dist/redirectToPreviewURL.cjs.map +1 -1
- package/dist/redirectToPreviewURL.js +3 -3
- package/dist/redirectToPreviewURL.js.map +1 -1
- package/package.json +5 -2
- package/src/PrismicPreview.tsx +5 -4
- package/src/exitPreview.ts +1 -3
- package/src/lib/getSafePath.ts +20 -0
- package/src/pages/PrismicPreview.tsx +6 -3
- package/src/pages/exitPreview.ts +1 -2
- package/src/pages/redirectToPreviewURL.ts +16 -6
- package/src/redirectToPreviewURL.ts +11 -8
- package/dist/lib/previewSessionCookie.cjs +0 -7
- package/dist/lib/previewSessionCookie.cjs.map +0 -1
- package/dist/lib/previewSessionCookie.js +0 -7
- package/dist/lib/previewSessionCookie.js.map +0 -1
- package/src/lib/previewSessionCookie.ts +0 -2
package/dist/PrismicPreview.cjs
CHANGED
|
@@ -16,20 +16,22 @@ const ResolvedScript = require_resolveDefaultExport.resolveDefaultExport(next_sc
|
|
|
16
16
|
* rendered on every page.
|
|
17
17
|
*/
|
|
18
18
|
const PrismicPreview = async (props) => {
|
|
19
|
-
const { repositoryName, children, ...otherProps } = props;
|
|
19
|
+
const { repositoryName, updatePreviewURL = "/api/preview", children, ...otherProps } = props;
|
|
20
20
|
const { draftMode } = await import("next/headers.js");
|
|
21
|
-
const toolbarSrc =
|
|
21
|
+
const toolbarSrc = (0, _prismicio_client.getToolbarSrc)(repositoryName, { version: 5 });
|
|
22
22
|
const isDraftMode = (await draftMode()).isEnabled;
|
|
23
23
|
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [
|
|
24
24
|
children,
|
|
25
25
|
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_PrismicPreviewClient.PrismicPreviewClient, {
|
|
26
26
|
repositoryName,
|
|
27
27
|
isDraftMode,
|
|
28
|
+
updatePreviewURL,
|
|
28
29
|
...otherProps
|
|
29
30
|
}),
|
|
30
31
|
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(ResolvedScript, {
|
|
31
32
|
src: toolbarSrc,
|
|
32
|
-
strategy: "lazyOnload"
|
|
33
|
+
strategy: "lazyOnload",
|
|
34
|
+
"data-preview-url": updatePreviewURL
|
|
33
35
|
})
|
|
34
36
|
] });
|
|
35
37
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PrismicPreview.cjs","names":["resolveDefaultExport","Script","getToolbarSrc","PrismicPreviewClient"],"sources":["../src/PrismicPreview.tsx"],"sourcesContent":["import { getToolbarSrc } from \"@prismicio/client\"\nimport Script from \"next/script\"\nimport type { FC, ReactNode } from \"react\"\n\nimport { resolveDefaultExport } from \"./lib/resolveDefaultExport\"\nimport { PrismicPreviewClient } from \"./PrismicPreviewClient\"\n\nconst ResolvedScript = resolveDefaultExport(Script)\n\n/** Props for `<PrismicPreview>`. */\nexport type PrismicPreviewProps = {\n\t/**\n\t * The name of your Prismic repository. A Prismic Toolbar will be registered using this\n\t * repository.\n\t */\n\trepositoryName: string\n\n\t/**\n\t * The URL of your app's Prismic preview endpoint (default: `/api/preview`). This URL will be\n\t * fetched on preview update events.\n\t */\n\tupdatePreviewURL?: string\n\n\t/**\n\t * The URL of your app's exit preview endpoint (default: `/api/exit-preview`). This URL will be\n\t * fetched on preview exit events.\n\t */\n\texitPreviewURL?: string\n\n\t/** Children to render adjacent to the Prismic Toolbar. */\n\tchildren?: ReactNode\n}\n\n/**\n * React component that sets up Prismic Previews using the Prismic Toolbar. When the Prismic Toolbar\n * send events to the browser, such as on preview updates and exiting, this component will\n * automatically refresh the page with the changes.\n *\n * This component can be wrapped around your app or added anywhere in your app's tree. It must be\n * rendered on every page.\n */\nexport const PrismicPreview: FC<PrismicPreviewProps> = async (props) => {\n\tconst { repositoryName, children, ...otherProps } = props\n\n\t// Need this to avoid the following Next.js build-time error:\n\t// You're importing a component that needs next/headers. That only works\n\t// in a Server Component which is not supported in the pages/ directory.\n\tconst { draftMode } = await import(\"next/headers\")\n\n\t// TODO:
|
|
1
|
+
{"version":3,"file":"PrismicPreview.cjs","names":["resolveDefaultExport","Script","getToolbarSrc","PrismicPreviewClient"],"sources":["../src/PrismicPreview.tsx"],"sourcesContent":["import { getToolbarSrc } from \"@prismicio/client\"\nimport Script from \"next/script\"\nimport type { FC, ReactNode } from \"react\"\n\nimport { resolveDefaultExport } from \"./lib/resolveDefaultExport\"\nimport { PrismicPreviewClient } from \"./PrismicPreviewClient\"\n\nconst ResolvedScript = resolveDefaultExport(Script)\n\n/** Props for `<PrismicPreview>`. */\nexport type PrismicPreviewProps = {\n\t/**\n\t * The name of your Prismic repository. A Prismic Toolbar will be registered using this\n\t * repository.\n\t */\n\trepositoryName: string\n\n\t/**\n\t * The URL of your app's Prismic preview endpoint (default: `/api/preview`). This URL will be\n\t * fetched on preview update events.\n\t */\n\tupdatePreviewURL?: string\n\n\t/**\n\t * The URL of your app's exit preview endpoint (default: `/api/exit-preview`). This URL will be\n\t * fetched on preview exit events.\n\t */\n\texitPreviewURL?: string\n\n\t/** Children to render adjacent to the Prismic Toolbar. */\n\tchildren?: ReactNode\n}\n\n/**\n * React component that sets up Prismic Previews using the Prismic Toolbar. When the Prismic Toolbar\n * send events to the browser, such as on preview updates and exiting, this component will\n * automatically refresh the page with the changes.\n *\n * This component can be wrapped around your app or added anywhere in your app's tree. It must be\n * rendered on every page.\n */\nexport const PrismicPreview: FC<PrismicPreviewProps> = async (props) => {\n\tconst { repositoryName, updatePreviewURL = \"/api/preview\", children, ...otherProps } = props\n\n\t// Need this to avoid the following Next.js build-time error:\n\t// You're importing a component that needs next/headers. That only works\n\t// in a Server Component which is not supported in the pages/ directory.\n\tconst { draftMode } = await import(\"next/headers\")\n\n\t// TODO: Bump the `@prismicio/client` peer to `^7.23.0`, which adds `version`, and drop `overrides`.\n\tconst toolbarSrc = getToolbarSrc(repositoryName, { version: 5 })\n\tconst isDraftMode = (await draftMode()).isEnabled\n\n\treturn (\n\t\t<>\n\t\t\t{children}\n\t\t\t<PrismicPreviewClient\n\t\t\t\trepositoryName={repositoryName}\n\t\t\t\tisDraftMode={isDraftMode}\n\t\t\t\tupdatePreviewURL={updatePreviewURL}\n\t\t\t\t{...otherProps}\n\t\t\t/>\n\t\t\t<ResolvedScript src={toolbarSrc} strategy=\"lazyOnload\" data-preview-url={updatePreviewURL} />\n\t\t</>\n\t)\n}\n"],"mappings":";;;;;;;;AAOA,MAAM,iBAAiBA,6BAAAA,qBAAqBC,eAAAA,OAAM;;;;;;;;;AAkClD,MAAa,iBAA0C,OAAO,UAAU;CACvE,MAAM,EAAE,gBAAgB,mBAAmB,gBAAgB,UAAU,GAAG,eAAe;CAKvF,MAAM,EAAE,cAAc,MAAM,OAAO;CAGnC,MAAM,cAAA,GAAaC,kBAAAA,cAAAA,CAAc,gBAAgB,EAAE,SAAS,EAAE,CAAC;CAC/D,MAAM,eAAe,MAAM,UAAU,EAAA,CAAG;CAExC,OACC,iBAAA,GAAA,kBAAA,KAAA,CAAA,kBAAA,UAAA,EAAA,UAAA;EACE;EACD,iBAAA,GAAA,kBAAA,IAAA,CAACC,6BAAAA,sBAAD;GACiB;GACH;GACK;GAClB,GAAI;EACJ,CAAA;EACD,iBAAA,GAAA,kBAAA,IAAA,CAAC,gBAAD;GAAgB,KAAK;GAAY,UAAS;GAAa,oBAAkB;EAAmB,CAAA;CAC3F,EAAA,CAAA;AAEJ"}
|
package/dist/PrismicPreview.js
CHANGED
|
@@ -14,20 +14,22 @@ const ResolvedScript = resolveDefaultExport(Script);
|
|
|
14
14
|
* rendered on every page.
|
|
15
15
|
*/
|
|
16
16
|
const PrismicPreview = async (props) => {
|
|
17
|
-
const { repositoryName, children, ...otherProps } = props;
|
|
17
|
+
const { repositoryName, updatePreviewURL = "/api/preview", children, ...otherProps } = props;
|
|
18
18
|
const { draftMode } = await import("next/headers.js");
|
|
19
|
-
const toolbarSrc =
|
|
19
|
+
const toolbarSrc = getToolbarSrc(repositoryName, { version: 5 });
|
|
20
20
|
const isDraftMode = (await draftMode()).isEnabled;
|
|
21
21
|
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
22
22
|
children,
|
|
23
23
|
/* @__PURE__ */ jsx(PrismicPreviewClient, {
|
|
24
24
|
repositoryName,
|
|
25
25
|
isDraftMode,
|
|
26
|
+
updatePreviewURL,
|
|
26
27
|
...otherProps
|
|
27
28
|
}),
|
|
28
29
|
/* @__PURE__ */ jsx(ResolvedScript, {
|
|
29
30
|
src: toolbarSrc,
|
|
30
|
-
strategy: "lazyOnload"
|
|
31
|
+
strategy: "lazyOnload",
|
|
32
|
+
"data-preview-url": updatePreviewURL
|
|
31
33
|
})
|
|
32
34
|
] });
|
|
33
35
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PrismicPreview.js","names":[],"sources":["../src/PrismicPreview.tsx"],"sourcesContent":["import { getToolbarSrc } from \"@prismicio/client\"\nimport Script from \"next/script\"\nimport type { FC, ReactNode } from \"react\"\n\nimport { resolveDefaultExport } from \"./lib/resolveDefaultExport\"\nimport { PrismicPreviewClient } from \"./PrismicPreviewClient\"\n\nconst ResolvedScript = resolveDefaultExport(Script)\n\n/** Props for `<PrismicPreview>`. */\nexport type PrismicPreviewProps = {\n\t/**\n\t * The name of your Prismic repository. A Prismic Toolbar will be registered using this\n\t * repository.\n\t */\n\trepositoryName: string\n\n\t/**\n\t * The URL of your app's Prismic preview endpoint (default: `/api/preview`). This URL will be\n\t * fetched on preview update events.\n\t */\n\tupdatePreviewURL?: string\n\n\t/**\n\t * The URL of your app's exit preview endpoint (default: `/api/exit-preview`). This URL will be\n\t * fetched on preview exit events.\n\t */\n\texitPreviewURL?: string\n\n\t/** Children to render adjacent to the Prismic Toolbar. */\n\tchildren?: ReactNode\n}\n\n/**\n * React component that sets up Prismic Previews using the Prismic Toolbar. When the Prismic Toolbar\n * send events to the browser, such as on preview updates and exiting, this component will\n * automatically refresh the page with the changes.\n *\n * This component can be wrapped around your app or added anywhere in your app's tree. It must be\n * rendered on every page.\n */\nexport const PrismicPreview: FC<PrismicPreviewProps> = async (props) => {\n\tconst { repositoryName, children, ...otherProps } = props\n\n\t// Need this to avoid the following Next.js build-time error:\n\t// You're importing a component that needs next/headers. That only works\n\t// in a Server Component which is not supported in the pages/ directory.\n\tconst { draftMode } = await import(\"next/headers\")\n\n\t// TODO:
|
|
1
|
+
{"version":3,"file":"PrismicPreview.js","names":[],"sources":["../src/PrismicPreview.tsx"],"sourcesContent":["import { getToolbarSrc } from \"@prismicio/client\"\nimport Script from \"next/script\"\nimport type { FC, ReactNode } from \"react\"\n\nimport { resolveDefaultExport } from \"./lib/resolveDefaultExport\"\nimport { PrismicPreviewClient } from \"./PrismicPreviewClient\"\n\nconst ResolvedScript = resolveDefaultExport(Script)\n\n/** Props for `<PrismicPreview>`. */\nexport type PrismicPreviewProps = {\n\t/**\n\t * The name of your Prismic repository. A Prismic Toolbar will be registered using this\n\t * repository.\n\t */\n\trepositoryName: string\n\n\t/**\n\t * The URL of your app's Prismic preview endpoint (default: `/api/preview`). This URL will be\n\t * fetched on preview update events.\n\t */\n\tupdatePreviewURL?: string\n\n\t/**\n\t * The URL of your app's exit preview endpoint (default: `/api/exit-preview`). This URL will be\n\t * fetched on preview exit events.\n\t */\n\texitPreviewURL?: string\n\n\t/** Children to render adjacent to the Prismic Toolbar. */\n\tchildren?: ReactNode\n}\n\n/**\n * React component that sets up Prismic Previews using the Prismic Toolbar. When the Prismic Toolbar\n * send events to the browser, such as on preview updates and exiting, this component will\n * automatically refresh the page with the changes.\n *\n * This component can be wrapped around your app or added anywhere in your app's tree. It must be\n * rendered on every page.\n */\nexport const PrismicPreview: FC<PrismicPreviewProps> = async (props) => {\n\tconst { repositoryName, updatePreviewURL = \"/api/preview\", children, ...otherProps } = props\n\n\t// Need this to avoid the following Next.js build-time error:\n\t// You're importing a component that needs next/headers. That only works\n\t// in a Server Component which is not supported in the pages/ directory.\n\tconst { draftMode } = await import(\"next/headers\")\n\n\t// TODO: Bump the `@prismicio/client` peer to `^7.23.0`, which adds `version`, and drop `overrides`.\n\tconst toolbarSrc = getToolbarSrc(repositoryName, { version: 5 })\n\tconst isDraftMode = (await draftMode()).isEnabled\n\n\treturn (\n\t\t<>\n\t\t\t{children}\n\t\t\t<PrismicPreviewClient\n\t\t\t\trepositoryName={repositoryName}\n\t\t\t\tisDraftMode={isDraftMode}\n\t\t\t\tupdatePreviewURL={updatePreviewURL}\n\t\t\t\t{...otherProps}\n\t\t\t/>\n\t\t\t<ResolvedScript src={toolbarSrc} strategy=\"lazyOnload\" data-preview-url={updatePreviewURL} />\n\t\t</>\n\t)\n}\n"],"mappings":";;;;;;AAOA,MAAM,iBAAiB,qBAAqB,MAAM;;;;;;;;;AAkClD,MAAa,iBAA0C,OAAO,UAAU;CACvE,MAAM,EAAE,gBAAgB,mBAAmB,gBAAgB,UAAU,GAAG,eAAe;CAKvF,MAAM,EAAE,cAAc,MAAM,OAAO;CAGnC,MAAM,aAAa,cAAc,gBAAgB,EAAE,SAAS,EAAE,CAAC;CAC/D,MAAM,eAAe,MAAM,UAAU,EAAA,CAAG;CAExC,OACC,qBAAA,UAAA,EAAA,UAAA;EACE;EACD,oBAAC,sBAAD;GACiB;GACH;GACK;GAClB,GAAI;EACJ,CAAA;EACD,oBAAC,gBAAD;GAAgB,KAAK;GAAY,UAAS;GAAa,oBAAkB;EAAmB,CAAA;CAC3F,EAAA,CAAA;AAEJ"}
|
package/dist/exitPreview.cjs
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
const require_previewSessionCookie = require("./lib/previewSessionCookie.cjs");
|
|
2
1
|
let _prismicio_client = require("@prismicio/client");
|
|
3
2
|
//#region src/exitPreview.ts
|
|
4
3
|
/**
|
|
@@ -30,7 +29,7 @@ async function exitPreview() {
|
|
|
30
29
|
secure: true
|
|
31
30
|
});
|
|
32
31
|
cookieJar.delete({
|
|
33
|
-
name:
|
|
32
|
+
name: _prismicio_client.cookie.previewSession,
|
|
34
33
|
sameSite: "none",
|
|
35
34
|
secure: true
|
|
36
35
|
});
|
package/dist/exitPreview.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exitPreview.cjs","names":["prismicCookie"
|
|
1
|
+
{"version":3,"file":"exitPreview.cjs","names":["prismicCookie"],"sources":["../src/exitPreview.ts"],"sourcesContent":["import { cookie as prismicCookie } from \"@prismicio/client\"\n\n/**\n * Ends a Prismic preview session within a Next.js app. This function should be used in a Router\n * Handler.\n *\n * @example\n * \t;```typescript\n * \t// src/app/api/exit-preview/route.js\n *\n * \timport { exitPreview } from \"@prismicio/next\"\n *\n * \texport async function GET() {\n * \t\treturn await exitPreview()\n * \t}\n * \t```\n */\nexport async function exitPreview(): Promise<Response> {\n\t// Need this to avoid the following Next.js build-time error:\n\t// You're importing a component that needs next/headers. That only works\n\t// in a Server Component which is not supported in the pages/ directory.\n\tconst { cookies } = await import(\"next/headers\")\n\n\tconst cookieJar = await cookies()\n\tcookieJar.delete({ name: \"__prerender_bypass\", sameSite: \"none\", secure: true })\n\tcookieJar.delete({ name: prismicCookie.preview, sameSite: \"none\", secure: true })\n\tcookieJar.delete({ name: prismicCookie.previewSession, sameSite: \"none\", secure: true })\n\n\t// `Cache-Control` header is used to prevent CDN-level caching.\n\treturn new Response(JSON.stringify({ success: true }), {\n\t\theaders: {\n\t\t\t\"Cache-Control\": \"no-store\",\n\t\t},\n\t})\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAiBA,eAAsB,cAAiC;CAItD,MAAM,EAAE,YAAY,MAAM,OAAO;CAEjC,MAAM,YAAY,MAAM,QAAQ;CAChC,UAAU,OAAO;EAAE,MAAM;EAAsB,UAAU;EAAQ,QAAQ;CAAK,CAAC;CAC/E,UAAU,OAAO;EAAE,MAAMA,kBAAAA,OAAc;EAAS,UAAU;EAAQ,QAAQ;CAAK,CAAC;CAChF,UAAU,OAAO;EAAE,MAAMA,kBAAAA,OAAc;EAAgB,UAAU;EAAQ,QAAQ;CAAK,CAAC;CAGvF,OAAO,IAAI,SAAS,KAAK,UAAU,EAAE,SAAS,KAAK,CAAC,GAAG,EACtD,SAAS,EACR,iBAAiB,WAClB,EACD,CAAC;AACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exitPreview.d.cts","names":[],"sources":["../src/exitPreview.ts"],"mappings":";;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"exitPreview.d.cts","names":[],"sources":["../src/exitPreview.ts"],"mappings":";;;;;;;;;;;;;;;;wBAiBsB,eAAe,QAAQ"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exitPreview.d.ts","names":[],"sources":["../src/exitPreview.ts"],"mappings":";;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"exitPreview.d.ts","names":[],"sources":["../src/exitPreview.ts"],"mappings":";;;;;;;;;;;;;;;;wBAiBsB,eAAe,QAAQ"}
|
package/dist/exitPreview.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { previewSessionCookie } from "./lib/previewSessionCookie.js";
|
|
2
1
|
import { cookie } from "@prismicio/client";
|
|
3
2
|
//#region src/exitPreview.ts
|
|
4
3
|
/**
|
|
@@ -30,7 +29,7 @@ async function exitPreview() {
|
|
|
30
29
|
secure: true
|
|
31
30
|
});
|
|
32
31
|
cookieJar.delete({
|
|
33
|
-
name:
|
|
32
|
+
name: cookie.previewSession,
|
|
34
33
|
sameSite: "none",
|
|
35
34
|
secure: true
|
|
36
35
|
});
|
package/dist/exitPreview.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exitPreview.js","names":["prismicCookie"],"sources":["../src/exitPreview.ts"],"sourcesContent":["import { cookie as prismicCookie } from \"@prismicio/client\"\n\
|
|
1
|
+
{"version":3,"file":"exitPreview.js","names":["prismicCookie"],"sources":["../src/exitPreview.ts"],"sourcesContent":["import { cookie as prismicCookie } from \"@prismicio/client\"\n\n/**\n * Ends a Prismic preview session within a Next.js app. This function should be used in a Router\n * Handler.\n *\n * @example\n * \t;```typescript\n * \t// src/app/api/exit-preview/route.js\n *\n * \timport { exitPreview } from \"@prismicio/next\"\n *\n * \texport async function GET() {\n * \t\treturn await exitPreview()\n * \t}\n * \t```\n */\nexport async function exitPreview(): Promise<Response> {\n\t// Need this to avoid the following Next.js build-time error:\n\t// You're importing a component that needs next/headers. That only works\n\t// in a Server Component which is not supported in the pages/ directory.\n\tconst { cookies } = await import(\"next/headers\")\n\n\tconst cookieJar = await cookies()\n\tcookieJar.delete({ name: \"__prerender_bypass\", sameSite: \"none\", secure: true })\n\tcookieJar.delete({ name: prismicCookie.preview, sameSite: \"none\", secure: true })\n\tcookieJar.delete({ name: prismicCookie.previewSession, sameSite: \"none\", secure: true })\n\n\t// `Cache-Control` header is used to prevent CDN-level caching.\n\treturn new Response(JSON.stringify({ success: true }), {\n\t\theaders: {\n\t\t\t\"Cache-Control\": \"no-store\",\n\t\t},\n\t})\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAiBA,eAAsB,cAAiC;CAItD,MAAM,EAAE,YAAY,MAAM,OAAO;CAEjC,MAAM,YAAY,MAAM,QAAQ;CAChC,UAAU,OAAO;EAAE,MAAM;EAAsB,UAAU;EAAQ,QAAQ;CAAK,CAAC;CAC/E,UAAU,OAAO;EAAE,MAAMA,OAAc;EAAS,UAAU;EAAQ,QAAQ;CAAK,CAAC;CAChF,UAAU,OAAO;EAAE,MAAMA,OAAc;EAAgB,UAAU;EAAQ,QAAQ;CAAK,CAAC;CAGvF,OAAO,IAAI,SAAS,KAAK,UAAU,EAAE,SAAS,KAAK,CAAC,GAAG,EACtD,SAAS,EACR,iBAAiB,WAClB,EACD,CAAC;AACF"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//#region src/lib/getSafePath.ts
|
|
2
|
+
/** Returns `path` if it stays on the current origin, like `/blog?page=2#top`. */
|
|
3
|
+
function getSafePath(path) {
|
|
4
|
+
if (!path?.startsWith("/") || path.startsWith("//") || path.startsWith("/\\")) return;
|
|
5
|
+
const origin = "https://placeholder.invalid";
|
|
6
|
+
let url;
|
|
7
|
+
try {
|
|
8
|
+
url = new URL(path, origin);
|
|
9
|
+
} catch {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
const safePath = url.href.slice(27);
|
|
13
|
+
if (url.origin === origin && !safePath.startsWith("//")) return safePath;
|
|
14
|
+
}
|
|
15
|
+
//#endregion
|
|
16
|
+
exports.getSafePath = getSafePath;
|
|
17
|
+
|
|
18
|
+
//# sourceMappingURL=getSafePath.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getSafePath.cjs","names":[],"sources":["../../src/lib/getSafePath.ts"],"sourcesContent":["/** Returns `path` if it stays on the current origin, like `/blog?page=2#top`. */\nexport function getSafePath(path: string | null | undefined): string | undefined {\n\tif (!path?.startsWith(\"/\") || path.startsWith(\"//\") || path.startsWith(\"/\\\\\")) {\n\t\treturn\n\t}\n\n\tconst origin = \"https://placeholder.invalid\"\n\tlet url: URL\n\ttry {\n\t\turl = new URL(path, origin)\n\t} catch {\n\t\treturn\n\t}\n\n\t// Dot segments can normalize `/a/..//evil.com` to `//evil.com`, which leaves the origin.\n\tconst safePath = url.href.slice(origin.length)\n\tif (url.origin === origin && !safePath.startsWith(\"//\")) {\n\t\treturn safePath\n\t}\n}\n"],"mappings":";;AACA,SAAgB,YAAY,MAAqD;CAChF,IAAI,CAAC,MAAM,WAAW,GAAG,KAAK,KAAK,WAAW,IAAI,KAAK,KAAK,WAAW,KAAK,GAC3E;CAGD,MAAM,SAAS;CACf,IAAI;CACJ,IAAI;EACH,MAAM,IAAI,IAAI,MAAM,MAAM;CAC3B,QAAQ;EACP;CACD;CAGA,MAAM,WAAW,IAAI,KAAK,MAAM,EAAa;CAC7C,IAAI,IAAI,WAAW,UAAU,CAAC,SAAS,WAAW,IAAI,GACrD,OAAO;AAET"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//#region src/lib/getSafePath.ts
|
|
2
|
+
/** Returns `path` if it stays on the current origin, like `/blog?page=2#top`. */
|
|
3
|
+
function getSafePath(path) {
|
|
4
|
+
if (!path?.startsWith("/") || path.startsWith("//") || path.startsWith("/\\")) return;
|
|
5
|
+
const origin = "https://placeholder.invalid";
|
|
6
|
+
let url;
|
|
7
|
+
try {
|
|
8
|
+
url = new URL(path, origin);
|
|
9
|
+
} catch {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
const safePath = url.href.slice(27);
|
|
13
|
+
if (url.origin === origin && !safePath.startsWith("//")) return safePath;
|
|
14
|
+
}
|
|
15
|
+
//#endregion
|
|
16
|
+
export { getSafePath };
|
|
17
|
+
|
|
18
|
+
//# sourceMappingURL=getSafePath.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getSafePath.js","names":[],"sources":["../../src/lib/getSafePath.ts"],"sourcesContent":["/** Returns `path` if it stays on the current origin, like `/blog?page=2#top`. */\nexport function getSafePath(path: string | null | undefined): string | undefined {\n\tif (!path?.startsWith(\"/\") || path.startsWith(\"//\") || path.startsWith(\"/\\\\\")) {\n\t\treturn\n\t}\n\n\tconst origin = \"https://placeholder.invalid\"\n\tlet url: URL\n\ttry {\n\t\turl = new URL(path, origin)\n\t} catch {\n\t\treturn\n\t}\n\n\t// Dot segments can normalize `/a/..//evil.com` to `//evil.com`, which leaves the origin.\n\tconst safePath = url.href.slice(origin.length)\n\tif (url.origin === origin && !safePath.startsWith(\"//\")) {\n\t\treturn safePath\n\t}\n}\n"],"mappings":";;AACA,SAAgB,YAAY,MAAqD;CAChF,IAAI,CAAC,MAAM,WAAW,GAAG,KAAK,KAAK,WAAW,IAAI,KAAK,KAAK,WAAW,KAAK,GAC3E;CAGD,MAAM,SAAS;CACf,IAAI;CACJ,IAAI;EACH,MAAM,IAAI,IAAI,MAAM,MAAM;CAC3B,QAAQ;EACP;CACD;CAGA,MAAM,WAAW,IAAI,KAAK,MAAM,EAAa;CAC7C,IAAI,IAAI,WAAW,UAAU,CAAC,SAAS,WAAW,IAAI,GACrD,OAAO;AAET"}
|
package/dist/package.cjs
CHANGED
package/dist/package.js
CHANGED
|
@@ -17,7 +17,7 @@ let next_router_js = require("next/router.js");
|
|
|
17
17
|
const PrismicPreview = (props) => {
|
|
18
18
|
const { repositoryName, updatePreviewURL = "/api/preview", exitPreviewURL = "/api/exit-preview", children } = props;
|
|
19
19
|
const router = (0, next_router_js.useRouter)();
|
|
20
|
-
const toolbarSrc =
|
|
20
|
+
const toolbarSrc = (0, _prismicio_client.getToolbarSrc)(repositoryName, { version: 5 });
|
|
21
21
|
(0, react.useEffect)(() => {
|
|
22
22
|
const controller = new AbortController();
|
|
23
23
|
window.addEventListener("prismicPreviewUpdate", onUpdate, { signal: controller.signal });
|
|
@@ -61,7 +61,8 @@ const PrismicPreview = (props) => {
|
|
|
61
61
|
]);
|
|
62
62
|
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [children, /* @__PURE__ */ (0, react_jsx_runtime.jsx)(next_script_js.default, {
|
|
63
63
|
src: toolbarSrc,
|
|
64
|
-
strategy: "lazyOnload"
|
|
64
|
+
strategy: "lazyOnload",
|
|
65
|
+
"data-preview-url": router.basePath + updatePreviewURL
|
|
65
66
|
})] });
|
|
66
67
|
};
|
|
67
68
|
function getPreviewCookieRepositoryName() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PrismicPreview.cjs","names":["useRouter","getToolbarSrc","Script","prismicCookie"],"sources":["../../src/pages/PrismicPreview.tsx"],"sourcesContent":["import { getToolbarSrc, cookie as prismicCookie } from \"@prismicio/client\"\nimport { useRouter } from \"next/router\"\nimport Script from \"next/script\"\nimport type { FC } from \"react\"\nimport { type ReactNode, useEffect } from \"react\"\n\n/** Props for `<PrismicPreview>`. */\nexport type PrismicPreviewProps = {\n\t/**\n\t * The name of your Prismic repository. A Prismic Toolbar will be registered using this\n\t * repository.\n\t */\n\trepositoryName: string\n\n\t/**\n\t * The URL of your app's Prismic preview endpoint (default: `/api/preview`). This URL will be\n\t * fetched on preview update events.\n\t */\n\tupdatePreviewURL?: string\n\n\t/**\n\t * The URL of your app's exit preview endpoint (default: `/api/exit-preview`). This URL will be\n\t * fetched on preview exit events.\n\t */\n\texitPreviewURL?: string\n\n\t/** Children to render adjacent to the Prismic Toolbar. */\n\tchildren?: ReactNode\n}\n\n/**\n * React component that sets up Prismic Previews using the Prismic Toolbar. When the Prismic Toolbar\n * send events to the browser, such as on preview updates and exiting, this component will\n * automatically refresh the page with the changes.\n *\n * This component can be wrapped around your app or added anywhere in your app's tree. It must be\n * rendered on every page.\n */\nexport const PrismicPreview: FC<PrismicPreviewProps> = (props) => {\n\tconst {\n\t\trepositoryName,\n\t\tupdatePreviewURL = \"/api/preview\",\n\t\texitPreviewURL = \"/api/exit-preview\",\n\t\tchildren,\n\t} = props\n\n\tconst router = useRouter()\n\n\
|
|
1
|
+
{"version":3,"file":"PrismicPreview.cjs","names":["useRouter","getToolbarSrc","Script","prismicCookie"],"sources":["../../src/pages/PrismicPreview.tsx"],"sourcesContent":["import { getToolbarSrc, cookie as prismicCookie } from \"@prismicio/client\"\nimport { useRouter } from \"next/router\"\nimport Script from \"next/script\"\nimport type { FC } from \"react\"\nimport { type ReactNode, useEffect } from \"react\"\n\n/** Props for `<PrismicPreview>`. */\nexport type PrismicPreviewProps = {\n\t/**\n\t * The name of your Prismic repository. A Prismic Toolbar will be registered using this\n\t * repository.\n\t */\n\trepositoryName: string\n\n\t/**\n\t * The URL of your app's Prismic preview endpoint (default: `/api/preview`). This URL will be\n\t * fetched on preview update events.\n\t */\n\tupdatePreviewURL?: string\n\n\t/**\n\t * The URL of your app's exit preview endpoint (default: `/api/exit-preview`). This URL will be\n\t * fetched on preview exit events.\n\t */\n\texitPreviewURL?: string\n\n\t/** Children to render adjacent to the Prismic Toolbar. */\n\tchildren?: ReactNode\n}\n\n/**\n * React component that sets up Prismic Previews using the Prismic Toolbar. When the Prismic Toolbar\n * send events to the browser, such as on preview updates and exiting, this component will\n * automatically refresh the page with the changes.\n *\n * This component can be wrapped around your app or added anywhere in your app's tree. It must be\n * rendered on every page.\n */\nexport const PrismicPreview: FC<PrismicPreviewProps> = (props) => {\n\tconst {\n\t\trepositoryName,\n\t\tupdatePreviewURL = \"/api/preview\",\n\t\texitPreviewURL = \"/api/exit-preview\",\n\t\tchildren,\n\t} = props\n\n\tconst router = useRouter()\n\n\tconst toolbarSrc = getToolbarSrc(repositoryName, { version: 5 })\n\n\tuseEffect(() => {\n\t\tconst controller = new AbortController()\n\n\t\twindow.addEventListener(\"prismicPreviewUpdate\", onUpdate, {\n\t\t\tsignal: controller.signal,\n\t\t})\n\t\twindow.addEventListener(\"prismicPreviewEnd\", onEnd, {\n\t\t\tsignal: controller.signal,\n\t\t})\n\n\t\t// Start the preview for preview share links. Previews from\n\t\t// share links do not go to the `updatePreviewURL` like a normal\n\t\t// preview.\n\t\t//\n\t\t// We check that the current URL is a descendant of the base\n\t\t// path to prevent infinite refrehes.\n\t\tif (\n\t\t\twindow.location.href.startsWith(window.location.origin + router.basePath) &&\n\t\t\tgetPreviewCookieRepositoryName() === repositoryName &&\n\t\t\t!router.isPreview\n\t\t) {\n\t\t\tstart()\n\t\t}\n\n\t\tfunction onEnd(event: Event) {\n\t\t\tevent.preventDefault()\n\t\t\tfetch(router.basePath + exitPreviewURL, { signal: controller.signal })\n\t\t\t\t.then((res) => {\n\t\t\t\t\tif (!res.ok) {\n\t\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t\t`[<PrismicPreview>] Failed to exit Preview Mode using the \"${exitPreviewURL}\" API endpoint. Does it exist?`,\n\t\t\t\t\t\t)\n\n\t\t\t\t\t\treturn\n\t\t\t\t\t}\n\n\t\t\t\t\trefresh()\n\t\t\t\t})\n\t\t\t\t.catch(() => {})\n\t\t}\n\n\t\tfunction onUpdate(event: Event) {\n\t\t\tevent.preventDefault()\n\t\t\tstart()\n\t\t}\n\n\t\tfunction start() {\n\t\t\t// We check `opaqueredirect` because we don't care if\n\t\t\t// the redirect was successful or not. As long as it\n\t\t\t// redirects, we know the endpoint exists and at least\n\t\t\t// attempted to set preview data.\n\t\t\tfetch(router.basePath + updatePreviewURL, {\n\t\t\t\tredirect: \"manual\",\n\t\t\t\tsignal: controller.signal,\n\t\t\t})\n\t\t\t\t.then((res) => {\n\t\t\t\t\tif (res.type !== \"opaqueredirect\") {\n\t\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t\t`[<PrismicPreview>] Failed to start or update the preview using \"${updatePreviewURL}\". Does it exist?`,\n\t\t\t\t\t\t)\n\n\t\t\t\t\t\treturn\n\t\t\t\t\t}\n\n\t\t\t\t\trefresh()\n\t\t\t\t})\n\t\t\t\t.catch(() => {})\n\t\t}\n\n\t\tfunction refresh() {\n\t\t\trouter.replace(router.asPath, undefined, { scroll: false })\n\t\t}\n\n\t\treturn () => controller.abort()\n\t}, [exitPreviewURL, updatePreviewURL, repositoryName, router])\n\n\treturn (\n\t\t<>\n\t\t\t{children}\n\t\t\t<Script\n\t\t\t\tsrc={toolbarSrc}\n\t\t\t\tstrategy=\"lazyOnload\"\n\t\t\t\tdata-preview-url={router.basePath + updatePreviewURL}\n\t\t\t/>\n\t\t</>\n\t)\n}\n\nfunction getPreviewCookieRepositoryName() {\n\tconst cookie = window.document.cookie\n\t\t.split(\"; \")\n\t\t.find((row) => row.startsWith(`${prismicCookie.preview}=`))\n\t\t?.split(\"=\")[1]\n\n\treturn (decodeURIComponent(cookie ?? \"\").match(/\"([^\"]+)\\.prismic\\.io\"/) || [])[1]\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAsCA,MAAa,kBAA2C,UAAU;CACjE,MAAM,EACL,gBACA,mBAAmB,gBACnB,iBAAiB,qBACjB,aACG;CAEJ,MAAM,UAAA,GAASA,eAAAA,UAAAA,CAAU;CAEzB,MAAM,cAAA,GAAaC,kBAAAA,cAAAA,CAAc,gBAAgB,EAAE,SAAS,EAAE,CAAC;CAE/D,CAAA,GAAA,MAAA,UAAA,OAAgB;EACf,MAAM,aAAa,IAAI,gBAAgB;EAEvC,OAAO,iBAAiB,wBAAwB,UAAU,EACzD,QAAQ,WAAW,OACpB,CAAC;EACD,OAAO,iBAAiB,qBAAqB,OAAO,EACnD,QAAQ,WAAW,OACpB,CAAC;EAQD,IACC,OAAO,SAAS,KAAK,WAAW,OAAO,SAAS,SAAS,OAAO,QAAQ,KACxE,+BAA+B,MAAM,kBACrC,CAAC,OAAO,WAER,MAAM;EAGP,SAAS,MAAM,OAAc;GAC5B,MAAM,eAAe;GACrB,MAAM,OAAO,WAAW,gBAAgB,EAAE,QAAQ,WAAW,OAAO,CAAC,CAAC,CACpE,MAAM,QAAQ;IACd,IAAI,CAAC,IAAI,IAAI;KACZ,QAAQ,MACP,6DAA6D,eAAe,+BAC7E;KAEA;IACD;IAEA,QAAQ;GACT,CAAC,CAAC,CACD,YAAY,CAAC,CAAC;EACjB;EAEA,SAAS,SAAS,OAAc;GAC/B,MAAM,eAAe;GACrB,MAAM;EACP;EAEA,SAAS,QAAQ;GAKhB,MAAM,OAAO,WAAW,kBAAkB;IACzC,UAAU;IACV,QAAQ,WAAW;GACpB,CAAC,CAAC,CACA,MAAM,QAAQ;IACd,IAAI,IAAI,SAAS,kBAAkB;KAClC,QAAQ,MACP,mEAAmE,iBAAiB,kBACrF;KAEA;IACD;IAEA,QAAQ;GACT,CAAC,CAAC,CACD,YAAY,CAAC,CAAC;EACjB;EAEA,SAAS,UAAU;GAClB,OAAO,QAAQ,OAAO,QAAQ,KAAA,GAAW,EAAE,QAAQ,MAAM,CAAC;EAC3D;EAEA,aAAa,WAAW,MAAM;CAC/B,GAAG;EAAC;EAAgB;EAAkB;EAAgB;CAAM,CAAC;CAE7D,OACC,iBAAA,GAAA,kBAAA,KAAA,CAAA,kBAAA,UAAA,EAAA,UAAA,CACE,UACD,iBAAA,GAAA,kBAAA,IAAA,CAACC,eAAAA,SAAD;EACC,KAAK;EACL,UAAS;EACT,oBAAkB,OAAO,WAAW;CACpC,CAAA,CACA,EAAA,CAAA;AAEJ;AAEA,SAAS,iCAAiC;CACzC,MAAM,SAAS,OAAO,SAAS,OAC7B,MAAM,IAAI,CAAC,CACX,MAAM,QAAQ,IAAI,WAAW,GAAGC,kBAAAA,OAAc,QAAQ,EAAE,CAAC,CAAC,EACzD,MAAM,GAAG,CAAC,CAAC;CAEd,QAAQ,mBAAmB,UAAU,EAAE,CAAC,CAAC,MAAM,wBAAwB,KAAK,CAAC,EAAA,CAAG;AACjF"}
|
|
@@ -15,7 +15,7 @@ import { useRouter } from "next/router.js";
|
|
|
15
15
|
const PrismicPreview = (props) => {
|
|
16
16
|
const { repositoryName, updatePreviewURL = "/api/preview", exitPreviewURL = "/api/exit-preview", children } = props;
|
|
17
17
|
const router = useRouter();
|
|
18
|
-
const toolbarSrc =
|
|
18
|
+
const toolbarSrc = getToolbarSrc(repositoryName, { version: 5 });
|
|
19
19
|
useEffect(() => {
|
|
20
20
|
const controller = new AbortController();
|
|
21
21
|
window.addEventListener("prismicPreviewUpdate", onUpdate, { signal: controller.signal });
|
|
@@ -59,7 +59,8 @@ const PrismicPreview = (props) => {
|
|
|
59
59
|
]);
|
|
60
60
|
return /* @__PURE__ */ jsxs(Fragment, { children: [children, /* @__PURE__ */ jsx(Script, {
|
|
61
61
|
src: toolbarSrc,
|
|
62
|
-
strategy: "lazyOnload"
|
|
62
|
+
strategy: "lazyOnload",
|
|
63
|
+
"data-preview-url": router.basePath + updatePreviewURL
|
|
63
64
|
})] });
|
|
64
65
|
};
|
|
65
66
|
function getPreviewCookieRepositoryName() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PrismicPreview.js","names":["cookie","prismicCookie"],"sources":["../../src/pages/PrismicPreview.tsx"],"sourcesContent":["import { getToolbarSrc, cookie as prismicCookie } from \"@prismicio/client\"\nimport { useRouter } from \"next/router\"\nimport Script from \"next/script\"\nimport type { FC } from \"react\"\nimport { type ReactNode, useEffect } from \"react\"\n\n/** Props for `<PrismicPreview>`. */\nexport type PrismicPreviewProps = {\n\t/**\n\t * The name of your Prismic repository. A Prismic Toolbar will be registered using this\n\t * repository.\n\t */\n\trepositoryName: string\n\n\t/**\n\t * The URL of your app's Prismic preview endpoint (default: `/api/preview`). This URL will be\n\t * fetched on preview update events.\n\t */\n\tupdatePreviewURL?: string\n\n\t/**\n\t * The URL of your app's exit preview endpoint (default: `/api/exit-preview`). This URL will be\n\t * fetched on preview exit events.\n\t */\n\texitPreviewURL?: string\n\n\t/** Children to render adjacent to the Prismic Toolbar. */\n\tchildren?: ReactNode\n}\n\n/**\n * React component that sets up Prismic Previews using the Prismic Toolbar. When the Prismic Toolbar\n * send events to the browser, such as on preview updates and exiting, this component will\n * automatically refresh the page with the changes.\n *\n * This component can be wrapped around your app or added anywhere in your app's tree. It must be\n * rendered on every page.\n */\nexport const PrismicPreview: FC<PrismicPreviewProps> = (props) => {\n\tconst {\n\t\trepositoryName,\n\t\tupdatePreviewURL = \"/api/preview\",\n\t\texitPreviewURL = \"/api/exit-preview\",\n\t\tchildren,\n\t} = props\n\n\tconst router = useRouter()\n\n\
|
|
1
|
+
{"version":3,"file":"PrismicPreview.js","names":["cookie","prismicCookie"],"sources":["../../src/pages/PrismicPreview.tsx"],"sourcesContent":["import { getToolbarSrc, cookie as prismicCookie } from \"@prismicio/client\"\nimport { useRouter } from \"next/router\"\nimport Script from \"next/script\"\nimport type { FC } from \"react\"\nimport { type ReactNode, useEffect } from \"react\"\n\n/** Props for `<PrismicPreview>`. */\nexport type PrismicPreviewProps = {\n\t/**\n\t * The name of your Prismic repository. A Prismic Toolbar will be registered using this\n\t * repository.\n\t */\n\trepositoryName: string\n\n\t/**\n\t * The URL of your app's Prismic preview endpoint (default: `/api/preview`). This URL will be\n\t * fetched on preview update events.\n\t */\n\tupdatePreviewURL?: string\n\n\t/**\n\t * The URL of your app's exit preview endpoint (default: `/api/exit-preview`). This URL will be\n\t * fetched on preview exit events.\n\t */\n\texitPreviewURL?: string\n\n\t/** Children to render adjacent to the Prismic Toolbar. */\n\tchildren?: ReactNode\n}\n\n/**\n * React component that sets up Prismic Previews using the Prismic Toolbar. When the Prismic Toolbar\n * send events to the browser, such as on preview updates and exiting, this component will\n * automatically refresh the page with the changes.\n *\n * This component can be wrapped around your app or added anywhere in your app's tree. It must be\n * rendered on every page.\n */\nexport const PrismicPreview: FC<PrismicPreviewProps> = (props) => {\n\tconst {\n\t\trepositoryName,\n\t\tupdatePreviewURL = \"/api/preview\",\n\t\texitPreviewURL = \"/api/exit-preview\",\n\t\tchildren,\n\t} = props\n\n\tconst router = useRouter()\n\n\tconst toolbarSrc = getToolbarSrc(repositoryName, { version: 5 })\n\n\tuseEffect(() => {\n\t\tconst controller = new AbortController()\n\n\t\twindow.addEventListener(\"prismicPreviewUpdate\", onUpdate, {\n\t\t\tsignal: controller.signal,\n\t\t})\n\t\twindow.addEventListener(\"prismicPreviewEnd\", onEnd, {\n\t\t\tsignal: controller.signal,\n\t\t})\n\n\t\t// Start the preview for preview share links. Previews from\n\t\t// share links do not go to the `updatePreviewURL` like a normal\n\t\t// preview.\n\t\t//\n\t\t// We check that the current URL is a descendant of the base\n\t\t// path to prevent infinite refrehes.\n\t\tif (\n\t\t\twindow.location.href.startsWith(window.location.origin + router.basePath) &&\n\t\t\tgetPreviewCookieRepositoryName() === repositoryName &&\n\t\t\t!router.isPreview\n\t\t) {\n\t\t\tstart()\n\t\t}\n\n\t\tfunction onEnd(event: Event) {\n\t\t\tevent.preventDefault()\n\t\t\tfetch(router.basePath + exitPreviewURL, { signal: controller.signal })\n\t\t\t\t.then((res) => {\n\t\t\t\t\tif (!res.ok) {\n\t\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t\t`[<PrismicPreview>] Failed to exit Preview Mode using the \"${exitPreviewURL}\" API endpoint. Does it exist?`,\n\t\t\t\t\t\t)\n\n\t\t\t\t\t\treturn\n\t\t\t\t\t}\n\n\t\t\t\t\trefresh()\n\t\t\t\t})\n\t\t\t\t.catch(() => {})\n\t\t}\n\n\t\tfunction onUpdate(event: Event) {\n\t\t\tevent.preventDefault()\n\t\t\tstart()\n\t\t}\n\n\t\tfunction start() {\n\t\t\t// We check `opaqueredirect` because we don't care if\n\t\t\t// the redirect was successful or not. As long as it\n\t\t\t// redirects, we know the endpoint exists and at least\n\t\t\t// attempted to set preview data.\n\t\t\tfetch(router.basePath + updatePreviewURL, {\n\t\t\t\tredirect: \"manual\",\n\t\t\t\tsignal: controller.signal,\n\t\t\t})\n\t\t\t\t.then((res) => {\n\t\t\t\t\tif (res.type !== \"opaqueredirect\") {\n\t\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t\t`[<PrismicPreview>] Failed to start or update the preview using \"${updatePreviewURL}\". Does it exist?`,\n\t\t\t\t\t\t)\n\n\t\t\t\t\t\treturn\n\t\t\t\t\t}\n\n\t\t\t\t\trefresh()\n\t\t\t\t})\n\t\t\t\t.catch(() => {})\n\t\t}\n\n\t\tfunction refresh() {\n\t\t\trouter.replace(router.asPath, undefined, { scroll: false })\n\t\t}\n\n\t\treturn () => controller.abort()\n\t}, [exitPreviewURL, updatePreviewURL, repositoryName, router])\n\n\treturn (\n\t\t<>\n\t\t\t{children}\n\t\t\t<Script\n\t\t\t\tsrc={toolbarSrc}\n\t\t\t\tstrategy=\"lazyOnload\"\n\t\t\t\tdata-preview-url={router.basePath + updatePreviewURL}\n\t\t\t/>\n\t\t</>\n\t)\n}\n\nfunction getPreviewCookieRepositoryName() {\n\tconst cookie = window.document.cookie\n\t\t.split(\"; \")\n\t\t.find((row) => row.startsWith(`${prismicCookie.preview}=`))\n\t\t?.split(\"=\")[1]\n\n\treturn (decodeURIComponent(cookie ?? \"\").match(/\"([^\"]+)\\.prismic\\.io\"/) || [])[1]\n}\n"],"mappings":";;;;;;;;;;;;;;AAsCA,MAAa,kBAA2C,UAAU;CACjE,MAAM,EACL,gBACA,mBAAmB,gBACnB,iBAAiB,qBACjB,aACG;CAEJ,MAAM,SAAS,UAAU;CAEzB,MAAM,aAAa,cAAc,gBAAgB,EAAE,SAAS,EAAE,CAAC;CAE/D,gBAAgB;EACf,MAAM,aAAa,IAAI,gBAAgB;EAEvC,OAAO,iBAAiB,wBAAwB,UAAU,EACzD,QAAQ,WAAW,OACpB,CAAC;EACD,OAAO,iBAAiB,qBAAqB,OAAO,EACnD,QAAQ,WAAW,OACpB,CAAC;EAQD,IACC,OAAO,SAAS,KAAK,WAAW,OAAO,SAAS,SAAS,OAAO,QAAQ,KACxE,+BAA+B,MAAM,kBACrC,CAAC,OAAO,WAER,MAAM;EAGP,SAAS,MAAM,OAAc;GAC5B,MAAM,eAAe;GACrB,MAAM,OAAO,WAAW,gBAAgB,EAAE,QAAQ,WAAW,OAAO,CAAC,CAAC,CACpE,MAAM,QAAQ;IACd,IAAI,CAAC,IAAI,IAAI;KACZ,QAAQ,MACP,6DAA6D,eAAe,+BAC7E;KAEA;IACD;IAEA,QAAQ;GACT,CAAC,CAAC,CACD,YAAY,CAAC,CAAC;EACjB;EAEA,SAAS,SAAS,OAAc;GAC/B,MAAM,eAAe;GACrB,MAAM;EACP;EAEA,SAAS,QAAQ;GAKhB,MAAM,OAAO,WAAW,kBAAkB;IACzC,UAAU;IACV,QAAQ,WAAW;GACpB,CAAC,CAAC,CACA,MAAM,QAAQ;IACd,IAAI,IAAI,SAAS,kBAAkB;KAClC,QAAQ,MACP,mEAAmE,iBAAiB,kBACrF;KAEA;IACD;IAEA,QAAQ;GACT,CAAC,CAAC,CACD,YAAY,CAAC,CAAC;EACjB;EAEA,SAAS,UAAU;GAClB,OAAO,QAAQ,OAAO,QAAQ,KAAA,GAAW,EAAE,QAAQ,MAAM,CAAC;EAC3D;EAEA,aAAa,WAAW,MAAM;CAC/B,GAAG;EAAC;EAAgB;EAAkB;EAAgB;CAAM,CAAC;CAE7D,OACC,qBAAA,UAAA,EAAA,UAAA,CACE,UACD,oBAAC,QAAD;EACC,KAAK;EACL,UAAS;EACT,oBAAkB,OAAO,WAAW;CACpC,CAAA,CACA,EAAA,CAAA;AAEJ;AAEA,SAAS,iCAAiC;CACzC,MAAMA,WAAS,OAAO,SAAS,OAC7B,MAAM,IAAI,CAAC,CACX,MAAM,QAAQ,IAAI,WAAW,GAAGC,OAAc,QAAQ,EAAE,CAAC,CAAC,EACzD,MAAM,GAAG,CAAC,CAAC;CAEd,QAAQ,mBAAmBD,YAAU,EAAE,CAAC,CAAC,MAAM,wBAAwB,KAAK,CAAC,EAAA,CAAG;AACjF"}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
const require_previewSessionCookie = require("../lib/previewSessionCookie.cjs");
|
|
2
1
|
const require_appendCookie = require("./appendCookie.cjs");
|
|
3
2
|
let _prismicio_client = require("@prismicio/client");
|
|
4
3
|
//#region src/pages/exitPreview.ts
|
|
@@ -19,7 +18,7 @@ let _prismicio_client = require("@prismicio/client");
|
|
|
19
18
|
*/
|
|
20
19
|
function exitPreview(config) {
|
|
21
20
|
config.res.clearPreviewData();
|
|
22
|
-
for (const name of [_prismicio_client.cookie.preview,
|
|
21
|
+
for (const name of [_prismicio_client.cookie.preview, _prismicio_client.cookie.previewSession]) require_appendCookie.appendCookie(config.res, `${name}=; Expires=${(/* @__PURE__ */ new Date(0)).toUTCString()}`);
|
|
23
22
|
config.res.setHeader("Cache-Control", "no-store");
|
|
24
23
|
config.res.json({ success: true });
|
|
25
24
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exitPreview.cjs","names":["prismicCookie"
|
|
1
|
+
{"version":3,"file":"exitPreview.cjs","names":["prismicCookie"],"sources":["../../src/pages/exitPreview.ts"],"sourcesContent":["import { cookie as prismicCookie } from \"@prismicio/client\"\n\nimport { appendCookie } from \"./appendCookie\"\nimport type { NextApiRequestLike, NextApiResponseLike } from \"./types\"\n\n/** Configuration for `exitPreview()`. */\nexport type ExitPreviewAPIRouteConfig = {\n\t/**\n\t * The `req` object from a Next.js API route.\n\t *\n\t * @see Next.js API route docs: \\<https://nextjs.org/docs/api-routes/introduction\\>\n\t */\n\treq?: NextApiRequestLike\n\n\t/**\n\t * The `res` object from a Next.js API route.\n\t *\n\t * @see Next.js API route docs: \\<https://nextjs.org/docs/api-routes/introduction\\>\n\t */\n\tres: NextApiResponseLike\n}\n\n/**\n * Ends a Prismic preview session within a Next.js app. This function should be used in an API\n * route.\n *\n * @example\n * \t;```typescript\n * \t// src/pages/api/exit-preview.js\n *\n * \timport { exitPreview } from \"@prismicio/next\"\n *\n * \texport default function handler(_req, res) {\n * \t\treturn exitPreview({ res })\n * \t}\n * \t```\n */\nexport function exitPreview(config: ExitPreviewAPIRouteConfig): void {\n\tconfig.res.clearPreviewData()\n\tfor (const name of [prismicCookie.preview, prismicCookie.previewSession]) {\n\t\tappendCookie(config.res, `${name}=; Expires=${new Date(0).toUTCString()}`)\n\t}\n\n\t// `Cache-Control` header is used to prevent CDN-level caching.\n\tconfig.res.setHeader(\"Cache-Control\", \"no-store\")\n\n\tconfig.res.json({ success: true })\n\n\treturn\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAqCA,SAAgB,YAAY,QAAyC;CACpE,OAAO,IAAI,iBAAiB;CAC5B,KAAK,MAAM,QAAQ,CAACA,kBAAAA,OAAc,SAASA,kBAAAA,OAAc,cAAc,GACtE,qBAAA,aAAa,OAAO,KAAK,GAAG,KAAK,8BAAa,IAAI,KAAK,CAAC,EAAA,CAAE,YAAY,GAAG;CAI1E,OAAO,IAAI,UAAU,iBAAiB,UAAU;CAEhD,OAAO,IAAI,KAAK,EAAE,SAAS,KAAK,CAAC;AAGlC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exitPreview.d.cts","names":[],"sources":["../../src/pages/exitPreview.ts"],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"exitPreview.d.cts","names":[],"sources":["../../src/pages/exitPreview.ts"],"mappings":";;;YAMY;;;;;;EAMX,MAAM;;;;;;EAON,KAAK;;;;;;;;;;;;;;;;;wBAkBU,YAAY,QAAQ"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exitPreview.d.ts","names":[],"sources":["../../src/pages/exitPreview.ts"],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"exitPreview.d.ts","names":[],"sources":["../../src/pages/exitPreview.ts"],"mappings":";;;YAMY;;;;;;EAMX,MAAM;;;;;;EAON,KAAK;;;;;;;;;;;;;;;;;wBAkBU,YAAY,QAAQ"}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { previewSessionCookie } from "../lib/previewSessionCookie.js";
|
|
2
1
|
import { appendCookie } from "./appendCookie.js";
|
|
3
2
|
import { cookie } from "@prismicio/client";
|
|
4
3
|
//#region src/pages/exitPreview.ts
|
|
@@ -19,7 +18,7 @@ import { cookie } from "@prismicio/client";
|
|
|
19
18
|
*/
|
|
20
19
|
function exitPreview(config) {
|
|
21
20
|
config.res.clearPreviewData();
|
|
22
|
-
for (const name of [cookie.preview,
|
|
21
|
+
for (const name of [cookie.preview, cookie.previewSession]) appendCookie(config.res, `${name}=; Expires=${(/* @__PURE__ */ new Date(0)).toUTCString()}`);
|
|
23
22
|
config.res.setHeader("Cache-Control", "no-store");
|
|
24
23
|
config.res.json({ success: true });
|
|
25
24
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exitPreview.js","names":["prismicCookie"],"sources":["../../src/pages/exitPreview.ts"],"sourcesContent":["import { cookie as prismicCookie } from \"@prismicio/client\"\n\nimport {
|
|
1
|
+
{"version":3,"file":"exitPreview.js","names":["prismicCookie"],"sources":["../../src/pages/exitPreview.ts"],"sourcesContent":["import { cookie as prismicCookie } from \"@prismicio/client\"\n\nimport { appendCookie } from \"./appendCookie\"\nimport type { NextApiRequestLike, NextApiResponseLike } from \"./types\"\n\n/** Configuration for `exitPreview()`. */\nexport type ExitPreviewAPIRouteConfig = {\n\t/**\n\t * The `req` object from a Next.js API route.\n\t *\n\t * @see Next.js API route docs: \\<https://nextjs.org/docs/api-routes/introduction\\>\n\t */\n\treq?: NextApiRequestLike\n\n\t/**\n\t * The `res` object from a Next.js API route.\n\t *\n\t * @see Next.js API route docs: \\<https://nextjs.org/docs/api-routes/introduction\\>\n\t */\n\tres: NextApiResponseLike\n}\n\n/**\n * Ends a Prismic preview session within a Next.js app. This function should be used in an API\n * route.\n *\n * @example\n * \t;```typescript\n * \t// src/pages/api/exit-preview.js\n *\n * \timport { exitPreview } from \"@prismicio/next\"\n *\n * \texport default function handler(_req, res) {\n * \t\treturn exitPreview({ res })\n * \t}\n * \t```\n */\nexport function exitPreview(config: ExitPreviewAPIRouteConfig): void {\n\tconfig.res.clearPreviewData()\n\tfor (const name of [prismicCookie.preview, prismicCookie.previewSession]) {\n\t\tappendCookie(config.res, `${name}=; Expires=${new Date(0).toUTCString()}`)\n\t}\n\n\t// `Cache-Control` header is used to prevent CDN-level caching.\n\tconfig.res.setHeader(\"Cache-Control\", \"no-store\")\n\n\tconfig.res.json({ success: true })\n\n\treturn\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAqCA,SAAgB,YAAY,QAAyC;CACpE,OAAO,IAAI,iBAAiB;CAC5B,KAAK,MAAM,QAAQ,CAACA,OAAc,SAASA,OAAc,cAAc,GACtE,aAAa,OAAO,KAAK,GAAG,KAAK,8BAAa,IAAI,KAAK,CAAC,EAAA,CAAE,YAAY,GAAG;CAI1E,OAAO,IAAI,UAAU,iBAAiB,UAAU;CAEhD,OAAO,IAAI,KAAK,EAAE,SAAS,KAAK,CAAC;AAGlC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_getSafePath = require("../lib/getSafePath.cjs");
|
|
2
2
|
const require_appendCookie = require("./appendCookie.cjs");
|
|
3
3
|
let _prismicio_client = require("@prismicio/client");
|
|
4
4
|
//#region src/pages/redirectToPreviewURL.ts
|
|
@@ -6,9 +6,14 @@ async function redirectToPreviewURL(config) {
|
|
|
6
6
|
const { client, req, res, linkResolver, defaultURL = "/", basePath = "" } = config;
|
|
7
7
|
const previewToken = req.query.token?.toString();
|
|
8
8
|
const session = req.query.session?.toString();
|
|
9
|
-
if (
|
|
10
|
-
require_appendCookie.appendCookie(res, `${_prismicio_client.cookie.
|
|
11
|
-
require_appendCookie.appendCookie(res, `${
|
|
9
|
+
if (session) {
|
|
10
|
+
require_appendCookie.appendCookie(res, `${_prismicio_client.cookie.previewSession}=${encodeURIComponent(session)}`);
|
|
11
|
+
if (previewToken) require_appendCookie.appendCookie(res, `${_prismicio_client.cookie.preview}=${encodeURIComponent(previewToken)}`);
|
|
12
|
+
}
|
|
13
|
+
const path = require_getSafePath.getSafePath(req.query.path?.toString());
|
|
14
|
+
if (path) {
|
|
15
|
+
res.redirect(path);
|
|
16
|
+
return;
|
|
12
17
|
}
|
|
13
18
|
const previewURL = await client.resolvePreviewURL({
|
|
14
19
|
linkResolver,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redirectToPreviewURL.cjs","names":["prismicCookie","
|
|
1
|
+
{"version":3,"file":"redirectToPreviewURL.cjs","names":["prismicCookie","getSafePath"],"sources":["../../src/pages/redirectToPreviewURL.ts"],"sourcesContent":["import { cookie as prismicCookie, type LinkResolverFunction, type Client } from \"@prismicio/client\"\n\nimport { getSafePath } from \"../lib/getSafePath\"\nimport { appendCookie } from \"./appendCookie\"\nimport type { NextApiRequestLike, NextApiResponseLike } from \"./types\"\n\nexport type RedirectToPreviewURLConfig = {\n\t/** The Prismic client configured for the preview session's repository. */\n\t// `Pick` is used to use the smallest possible subset of\n\t// `prismic.Client`. Doing this reduces the surface area for breaking\n\t// type changes.\n\tclient: Pick<Client, \"resolvePreviewURL\">\n\n\t/**\n\t * The `req` object from a Next.js API route.\n\t *\n\t * @see Next.js API route docs: \\<https://nextjs.org/docs/pages/building-your-application/routing/api-routes\\>\n\t */\n\treq: NextApiRequestLike\n\n\t/**\n\t * The `res` object from a Next.js API route.\n\t *\n\t * @see Next.js API route docs: \\<https://nextjs.org/docs/pages/building-your-application/routing/api-routes\\>\n\t */\n\tres: NextApiResponseLike\n\n\t/**\n\t * A Link Resolver used to resolve the previewed document's URL.\n\t *\n\t * @see To learn more about Link Resolver: {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver}\n\t */\n\tlinkResolver?: LinkResolverFunction\n\n\t/**\n\t * The default redirect URL if a URL cannot be determined for the previewed document.\n\t *\n\t * **Note**: If you `next.config.js` file contains a `basePath`, the `defaultURL` option must\n\t * _not_ include it. Instead, provide the `basePath` property using the `basePath` option.\n\t */\n\tdefaultURL?: string\n\n\t/**\n\t * The `basePath` for the Next.js app as it is defined in `next.config.js`. This option can be\n\t * omitted if the app does not have a `basePath`.\n\t *\n\t * @remarks\n\t * The Router Handler or API route is unable to detect the app's `basePath` automatically. It\n\t * must be provided to `redirectToPreviewURL()` manually.\n\t */\n\tbasePath?: string\n}\n\nexport async function redirectToPreviewURL(config: RedirectToPreviewURLConfig): Promise<void> {\n\tconst { client, req, res, linkResolver, defaultURL = \"/\", basePath = \"\" } = config\n\n\tconst previewToken = req.query.token?.toString()\n\n\t// The toolbar's v5 flow follows this session from the ref the page shows. Without that ref, its\n\t// first ping would start the preview again, racing an early exit.\n\tconst session = req.query.session?.toString()\n\tif (session) {\n\t\tappendCookie(res, `${prismicCookie.previewSession}=${encodeURIComponent(session)}`)\n\t\tif (previewToken) {\n\t\t\tappendCookie(res, `${prismicCookie.preview}=${encodeURIComponent(previewToken)}`)\n\t\t}\n\t}\n\n\t// Share links return to the page they were opened from. Its path includes `basePath`.\n\tconst path = getSafePath(req.query.path?.toString())\n\tif (path) {\n\t\tres.redirect(path)\n\n\t\treturn\n\t}\n\n\tconst previewURL = await client.resolvePreviewURL({\n\t\tlinkResolver,\n\t\tdefaultURL,\n\t\tpreviewToken,\n\t})\n\n\tres.redirect(basePath + previewURL)\n\n\treturn\n}\n"],"mappings":";;;;AAqDA,eAAsB,qBAAqB,QAAmD;CAC7F,MAAM,EAAE,QAAQ,KAAK,KAAK,cAAc,aAAa,KAAK,WAAW,OAAO;CAE5E,MAAM,eAAe,IAAI,MAAM,OAAO,SAAS;CAI/C,MAAM,UAAU,IAAI,MAAM,SAAS,SAAS;CAC5C,IAAI,SAAS;EACZ,qBAAA,aAAa,KAAK,GAAGA,kBAAAA,OAAc,eAAe,GAAG,mBAAmB,OAAO,GAAG;EAClF,IAAI,cACH,qBAAA,aAAa,KAAK,GAAGA,kBAAAA,OAAc,QAAQ,GAAG,mBAAmB,YAAY,GAAG;CAElF;CAGA,MAAM,OAAOC,oBAAAA,YAAY,IAAI,MAAM,MAAM,SAAS,CAAC;CACnD,IAAI,MAAM;EACT,IAAI,SAAS,IAAI;EAEjB;CACD;CAEA,MAAM,aAAa,MAAM,OAAO,kBAAkB;EACjD;EACA;EACA;CACD,CAAC;CAED,IAAI,SAAS,WAAW,UAAU;AAGnC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getSafePath } from "../lib/getSafePath.js";
|
|
2
2
|
import { appendCookie } from "./appendCookie.js";
|
|
3
3
|
import { cookie } from "@prismicio/client";
|
|
4
4
|
//#region src/pages/redirectToPreviewURL.ts
|
|
@@ -6,9 +6,14 @@ async function redirectToPreviewURL(config) {
|
|
|
6
6
|
const { client, req, res, linkResolver, defaultURL = "/", basePath = "" } = config;
|
|
7
7
|
const previewToken = req.query.token?.toString();
|
|
8
8
|
const session = req.query.session?.toString();
|
|
9
|
-
if (
|
|
10
|
-
appendCookie(res, `${cookie.
|
|
11
|
-
appendCookie(res, `${
|
|
9
|
+
if (session) {
|
|
10
|
+
appendCookie(res, `${cookie.previewSession}=${encodeURIComponent(session)}`);
|
|
11
|
+
if (previewToken) appendCookie(res, `${cookie.preview}=${encodeURIComponent(previewToken)}`);
|
|
12
|
+
}
|
|
13
|
+
const path = getSafePath(req.query.path?.toString());
|
|
14
|
+
if (path) {
|
|
15
|
+
res.redirect(path);
|
|
16
|
+
return;
|
|
12
17
|
}
|
|
13
18
|
const previewURL = await client.resolvePreviewURL({
|
|
14
19
|
linkResolver,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redirectToPreviewURL.js","names":["prismicCookie"],"sources":["../../src/pages/redirectToPreviewURL.ts"],"sourcesContent":["import { cookie as prismicCookie, type LinkResolverFunction, type Client } from \"@prismicio/client\"\n\nimport {
|
|
1
|
+
{"version":3,"file":"redirectToPreviewURL.js","names":["prismicCookie"],"sources":["../../src/pages/redirectToPreviewURL.ts"],"sourcesContent":["import { cookie as prismicCookie, type LinkResolverFunction, type Client } from \"@prismicio/client\"\n\nimport { getSafePath } from \"../lib/getSafePath\"\nimport { appendCookie } from \"./appendCookie\"\nimport type { NextApiRequestLike, NextApiResponseLike } from \"./types\"\n\nexport type RedirectToPreviewURLConfig = {\n\t/** The Prismic client configured for the preview session's repository. */\n\t// `Pick` is used to use the smallest possible subset of\n\t// `prismic.Client`. Doing this reduces the surface area for breaking\n\t// type changes.\n\tclient: Pick<Client, \"resolvePreviewURL\">\n\n\t/**\n\t * The `req` object from a Next.js API route.\n\t *\n\t * @see Next.js API route docs: \\<https://nextjs.org/docs/pages/building-your-application/routing/api-routes\\>\n\t */\n\treq: NextApiRequestLike\n\n\t/**\n\t * The `res` object from a Next.js API route.\n\t *\n\t * @see Next.js API route docs: \\<https://nextjs.org/docs/pages/building-your-application/routing/api-routes\\>\n\t */\n\tres: NextApiResponseLike\n\n\t/**\n\t * A Link Resolver used to resolve the previewed document's URL.\n\t *\n\t * @see To learn more about Link Resolver: {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver}\n\t */\n\tlinkResolver?: LinkResolverFunction\n\n\t/**\n\t * The default redirect URL if a URL cannot be determined for the previewed document.\n\t *\n\t * **Note**: If you `next.config.js` file contains a `basePath`, the `defaultURL` option must\n\t * _not_ include it. Instead, provide the `basePath` property using the `basePath` option.\n\t */\n\tdefaultURL?: string\n\n\t/**\n\t * The `basePath` for the Next.js app as it is defined in `next.config.js`. This option can be\n\t * omitted if the app does not have a `basePath`.\n\t *\n\t * @remarks\n\t * The Router Handler or API route is unable to detect the app's `basePath` automatically. It\n\t * must be provided to `redirectToPreviewURL()` manually.\n\t */\n\tbasePath?: string\n}\n\nexport async function redirectToPreviewURL(config: RedirectToPreviewURLConfig): Promise<void> {\n\tconst { client, req, res, linkResolver, defaultURL = \"/\", basePath = \"\" } = config\n\n\tconst previewToken = req.query.token?.toString()\n\n\t// The toolbar's v5 flow follows this session from the ref the page shows. Without that ref, its\n\t// first ping would start the preview again, racing an early exit.\n\tconst session = req.query.session?.toString()\n\tif (session) {\n\t\tappendCookie(res, `${prismicCookie.previewSession}=${encodeURIComponent(session)}`)\n\t\tif (previewToken) {\n\t\t\tappendCookie(res, `${prismicCookie.preview}=${encodeURIComponent(previewToken)}`)\n\t\t}\n\t}\n\n\t// Share links return to the page they were opened from. Its path includes `basePath`.\n\tconst path = getSafePath(req.query.path?.toString())\n\tif (path) {\n\t\tres.redirect(path)\n\n\t\treturn\n\t}\n\n\tconst previewURL = await client.resolvePreviewURL({\n\t\tlinkResolver,\n\t\tdefaultURL,\n\t\tpreviewToken,\n\t})\n\n\tres.redirect(basePath + previewURL)\n\n\treturn\n}\n"],"mappings":";;;;AAqDA,eAAsB,qBAAqB,QAAmD;CAC7F,MAAM,EAAE,QAAQ,KAAK,KAAK,cAAc,aAAa,KAAK,WAAW,OAAO;CAE5E,MAAM,eAAe,IAAI,MAAM,OAAO,SAAS;CAI/C,MAAM,UAAU,IAAI,MAAM,SAAS,SAAS;CAC5C,IAAI,SAAS;EACZ,aAAa,KAAK,GAAGA,OAAc,eAAe,GAAG,mBAAmB,OAAO,GAAG;EAClF,IAAI,cACH,aAAa,KAAK,GAAGA,OAAc,QAAQ,GAAG,mBAAmB,YAAY,GAAG;CAElF;CAGA,MAAM,OAAO,YAAY,IAAI,MAAM,MAAM,SAAS,CAAC;CACnD,IAAI,MAAM;EACT,IAAI,SAAS,IAAI;EAEjB;CACD;CAEA,MAAM,aAAa,MAAM,OAAO,kBAAkB;EACjD;EACA;EACA;CACD,CAAC;CAED,IAAI,SAAS,WAAW,UAAU;AAGnC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_getSafePath = require("./lib/getSafePath.cjs");
|
|
2
2
|
let _prismicio_client = require("@prismicio/client");
|
|
3
3
|
//#region src/redirectToPreviewURL.ts
|
|
4
4
|
async function redirectToPreviewURL(config) {
|
|
@@ -13,11 +13,11 @@ async function redirectToPreviewURL(config) {
|
|
|
13
13
|
secure: true
|
|
14
14
|
});
|
|
15
15
|
const session = request.nextUrl.searchParams.get("session");
|
|
16
|
-
if (session) cookieJar.set(
|
|
16
|
+
if (session) cookieJar.set(_prismicio_client.cookie.previewSession, session, {
|
|
17
17
|
sameSite: "none",
|
|
18
18
|
secure: true
|
|
19
19
|
});
|
|
20
|
-
const previewURL = await client.resolvePreviewURL({
|
|
20
|
+
const previewURL = require_getSafePath.getSafePath(request.nextUrl.searchParams.get("path")) ?? await client.resolvePreviewURL({
|
|
21
21
|
documentID,
|
|
22
22
|
previewToken,
|
|
23
23
|
defaultURL,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redirectToPreviewURL.cjs","names":["prismicCookie","
|
|
1
|
+
{"version":3,"file":"redirectToPreviewURL.cjs","names":["prismicCookie","getSafePath"],"sources":["../src/redirectToPreviewURL.ts"],"sourcesContent":["import { cookie as prismicCookie, type Client, type LinkResolverFunction } from \"@prismicio/client\"\n\nimport { getSafePath } from \"./lib/getSafePath\"\nimport type { NextRequestLike } from \"./types\"\n\nexport type RedirectToPreviewURLConfig = {\n\t/** The Prismic client configured for the preview session's repository. */\n\t// `Pick` is used to use the smallest possible subset of\n\t// `prismic.Client`. Doing this reduces the surface area for breaking\n\t// type changes.\n\tclient: Pick<Client, \"resolvePreviewURL\">\n\n\t/**\n\t * The `request` object from a Next.js Route Handler.\n\t *\n\t * @see Next.js Route Handler docs: \\<https://nextjs.org/docs/app/building-your-application/routing/route-handlers\\>\n\t */\n\trequest: NextRequestLike\n\n\t/**\n\t * A Link Resolver used to resolve the previewed document's URL.\n\t *\n\t * @see To learn more about Link Resolver: {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver}\n\t */\n\tlinkResolver?: LinkResolverFunction\n\n\t/**\n\t * The default redirect URL if a URL cannot be determined for the previewed document.\n\t *\n\t * **Note**: If you `next.config.js` file contains a `basePath`, the `defaultURL` option must\n\t * _not_ include it. Instead, provide the `basePath` property using the `basePath` option.\n\t */\n\tdefaultURL?: string\n}\n\nexport async function redirectToPreviewURL(config: RedirectToPreviewURLConfig): Promise<never> {\n\tconst { client, request, linkResolver, defaultURL = \"/\" } = config\n\n\t// Need this to avoid the following Next.js build-time error:\n\t// You're importing a component that needs next/headers. That only works\n\t// in a Server Component which is not supported in the pages/ directory.\n\tconst { cookies, draftMode } = await import(\"next/headers\")\n\n\t// Dynamic so Node.js can load this module: the bare specifier (kept by\n\t// tsdown.config.ts for Next.js's server alias) is not resolvable by Node.js.\n\tconst { redirect } = await import(\"next/navigation\")\n\n\tconst documentID = request.nextUrl.searchParams.get(\"documentId\") ?? undefined\n\n\t// Set the initial preview cookie. Setting the cookie here is necessary\n\t// to support unpublished previews. Without setting it here, the page\n\t// will try to render without the preview cookie, leading to a\n\t// PrismicNotFound error.\n\tconst cookieJar = await cookies()\n\tconst previewToken = request.nextUrl.searchParams.get(\"token\") ?? undefined\n\tif (previewToken) {\n\t\tcookieJar.set(prismicCookie.preview, previewToken, { sameSite: \"none\", secure: true })\n\t}\n\n\t// The toolbar's v5 flow follows this session.\n\tconst session = request.nextUrl.searchParams.get(\"session\")\n\tif (session) {\n\t\tcookieJar.set(prismicCookie.previewSession, session, { sameSite: \"none\", secure: true })\n\t}\n\n\t// Share links return to the page they were opened from.\n\tconst previewURL =\n\t\tgetSafePath(request.nextUrl.searchParams.get(\"path\")) ??\n\t\t(await client.resolvePreviewURL({\n\t\t\tdocumentID,\n\t\t\tpreviewToken,\n\t\t\tdefaultURL,\n\t\t\tlinkResolver,\n\t\t}))\n\n\t;(await draftMode()).enable()\n\n\t// Next.js uses `SameSite=Lax` in development, which cross-site iframes drop.\n\tcookieJar.set({\n\t\t...cookieJar.get(\"__prerender_bypass\")!,\n\t\thttpOnly: true,\n\t\tsameSite: \"none\",\n\t\tsecure: true,\n\t})\n\n\treturn redirect(previewURL)\n}\n"],"mappings":";;;AAmCA,eAAsB,qBAAqB,QAAoD;CAC9F,MAAM,EAAE,QAAQ,SAAS,cAAc,aAAa,QAAQ;CAK5D,MAAM,EAAE,SAAS,cAAc,MAAM,OAAO;CAI5C,MAAM,EAAE,aAAa,MAAM,OAAO;CAElC,MAAM,aAAa,QAAQ,QAAQ,aAAa,IAAI,YAAY,KAAK,KAAA;CAMrE,MAAM,YAAY,MAAM,QAAQ;CAChC,MAAM,eAAe,QAAQ,QAAQ,aAAa,IAAI,OAAO,KAAK,KAAA;CAClE,IAAI,cACH,UAAU,IAAIA,kBAAAA,OAAc,SAAS,cAAc;EAAE,UAAU;EAAQ,QAAQ;CAAK,CAAC;CAItF,MAAM,UAAU,QAAQ,QAAQ,aAAa,IAAI,SAAS;CAC1D,IAAI,SACH,UAAU,IAAIA,kBAAAA,OAAc,gBAAgB,SAAS;EAAE,UAAU;EAAQ,QAAQ;CAAK,CAAC;CAIxF,MAAM,aACLC,oBAAAA,YAAY,QAAQ,QAAQ,aAAa,IAAI,MAAM,CAAC,KACnD,MAAM,OAAO,kBAAkB;EAC/B;EACA;EACA;EACA;CACD,CAAC;CAED,CAAC,MAAM,UAAU,EAAA,CAAG,OAAO;CAG5B,UAAU,IAAI;EACb,GAAG,UAAU,IAAI,oBAAoB;EACrC,UAAU;EACV,UAAU;EACV,QAAQ;CACT,CAAC;CAED,OAAO,SAAS,UAAU;AAC3B"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getSafePath } from "./lib/getSafePath.js";
|
|
2
2
|
import { cookie } from "@prismicio/client";
|
|
3
3
|
//#region src/redirectToPreviewURL.ts
|
|
4
4
|
async function redirectToPreviewURL(config) {
|
|
@@ -13,11 +13,11 @@ async function redirectToPreviewURL(config) {
|
|
|
13
13
|
secure: true
|
|
14
14
|
});
|
|
15
15
|
const session = request.nextUrl.searchParams.get("session");
|
|
16
|
-
if (session) cookieJar.set(
|
|
16
|
+
if (session) cookieJar.set(cookie.previewSession, session, {
|
|
17
17
|
sameSite: "none",
|
|
18
18
|
secure: true
|
|
19
19
|
});
|
|
20
|
-
const previewURL = await client.resolvePreviewURL({
|
|
20
|
+
const previewURL = getSafePath(request.nextUrl.searchParams.get("path")) ?? await client.resolvePreviewURL({
|
|
21
21
|
documentID,
|
|
22
22
|
previewToken,
|
|
23
23
|
defaultURL,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redirectToPreviewURL.js","names":["prismicCookie"],"sources":["../src/redirectToPreviewURL.ts"],"sourcesContent":["import { cookie as prismicCookie, type Client, type LinkResolverFunction } from \"@prismicio/client\"\n\nimport {
|
|
1
|
+
{"version":3,"file":"redirectToPreviewURL.js","names":["prismicCookie"],"sources":["../src/redirectToPreviewURL.ts"],"sourcesContent":["import { cookie as prismicCookie, type Client, type LinkResolverFunction } from \"@prismicio/client\"\n\nimport { getSafePath } from \"./lib/getSafePath\"\nimport type { NextRequestLike } from \"./types\"\n\nexport type RedirectToPreviewURLConfig = {\n\t/** The Prismic client configured for the preview session's repository. */\n\t// `Pick` is used to use the smallest possible subset of\n\t// `prismic.Client`. Doing this reduces the surface area for breaking\n\t// type changes.\n\tclient: Pick<Client, \"resolvePreviewURL\">\n\n\t/**\n\t * The `request` object from a Next.js Route Handler.\n\t *\n\t * @see Next.js Route Handler docs: \\<https://nextjs.org/docs/app/building-your-application/routing/route-handlers\\>\n\t */\n\trequest: NextRequestLike\n\n\t/**\n\t * A Link Resolver used to resolve the previewed document's URL.\n\t *\n\t * @see To learn more about Link Resolver: {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver}\n\t */\n\tlinkResolver?: LinkResolverFunction\n\n\t/**\n\t * The default redirect URL if a URL cannot be determined for the previewed document.\n\t *\n\t * **Note**: If you `next.config.js` file contains a `basePath`, the `defaultURL` option must\n\t * _not_ include it. Instead, provide the `basePath` property using the `basePath` option.\n\t */\n\tdefaultURL?: string\n}\n\nexport async function redirectToPreviewURL(config: RedirectToPreviewURLConfig): Promise<never> {\n\tconst { client, request, linkResolver, defaultURL = \"/\" } = config\n\n\t// Need this to avoid the following Next.js build-time error:\n\t// You're importing a component that needs next/headers. That only works\n\t// in a Server Component which is not supported in the pages/ directory.\n\tconst { cookies, draftMode } = await import(\"next/headers\")\n\n\t// Dynamic so Node.js can load this module: the bare specifier (kept by\n\t// tsdown.config.ts for Next.js's server alias) is not resolvable by Node.js.\n\tconst { redirect } = await import(\"next/navigation\")\n\n\tconst documentID = request.nextUrl.searchParams.get(\"documentId\") ?? undefined\n\n\t// Set the initial preview cookie. Setting the cookie here is necessary\n\t// to support unpublished previews. Without setting it here, the page\n\t// will try to render without the preview cookie, leading to a\n\t// PrismicNotFound error.\n\tconst cookieJar = await cookies()\n\tconst previewToken = request.nextUrl.searchParams.get(\"token\") ?? undefined\n\tif (previewToken) {\n\t\tcookieJar.set(prismicCookie.preview, previewToken, { sameSite: \"none\", secure: true })\n\t}\n\n\t// The toolbar's v5 flow follows this session.\n\tconst session = request.nextUrl.searchParams.get(\"session\")\n\tif (session) {\n\t\tcookieJar.set(prismicCookie.previewSession, session, { sameSite: \"none\", secure: true })\n\t}\n\n\t// Share links return to the page they were opened from.\n\tconst previewURL =\n\t\tgetSafePath(request.nextUrl.searchParams.get(\"path\")) ??\n\t\t(await client.resolvePreviewURL({\n\t\t\tdocumentID,\n\t\t\tpreviewToken,\n\t\t\tdefaultURL,\n\t\t\tlinkResolver,\n\t\t}))\n\n\t;(await draftMode()).enable()\n\n\t// Next.js uses `SameSite=Lax` in development, which cross-site iframes drop.\n\tcookieJar.set({\n\t\t...cookieJar.get(\"__prerender_bypass\")!,\n\t\thttpOnly: true,\n\t\tsameSite: \"none\",\n\t\tsecure: true,\n\t})\n\n\treturn redirect(previewURL)\n}\n"],"mappings":";;;AAmCA,eAAsB,qBAAqB,QAAoD;CAC9F,MAAM,EAAE,QAAQ,SAAS,cAAc,aAAa,QAAQ;CAK5D,MAAM,EAAE,SAAS,cAAc,MAAM,OAAO;CAI5C,MAAM,EAAE,aAAa,MAAM,OAAO;CAElC,MAAM,aAAa,QAAQ,QAAQ,aAAa,IAAI,YAAY,KAAK,KAAA;CAMrE,MAAM,YAAY,MAAM,QAAQ;CAChC,MAAM,eAAe,QAAQ,QAAQ,aAAa,IAAI,OAAO,KAAK,KAAA;CAClE,IAAI,cACH,UAAU,IAAIA,OAAc,SAAS,cAAc;EAAE,UAAU;EAAQ,QAAQ;CAAK,CAAC;CAItF,MAAM,UAAU,QAAQ,QAAQ,aAAa,IAAI,SAAS;CAC1D,IAAI,SACH,UAAU,IAAIA,OAAc,gBAAgB,SAAS;EAAE,UAAU;EAAQ,QAAQ;CAAK,CAAC;CAIxF,MAAM,aACL,YAAY,QAAQ,QAAQ,aAAa,IAAI,MAAM,CAAC,KACnD,MAAM,OAAO,kBAAkB;EAC/B;EACA;EACA;EACA;CACD,CAAC;CAED,CAAC,MAAM,UAAU,EAAA,CAAG,OAAO;CAG5B,UAAU,IAAI;EACb,GAAG,UAAU,IAAI,oBAAoB;EACrC,UAAU;EACV,UAAU;EACV,QAAQ;CACT,CAAC;CAED,OAAO,SAAS,UAAU;AAC3B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prismicio/next",
|
|
3
|
-
"version": "2.3.4-pr.175.
|
|
3
|
+
"version": "2.3.4-pr.175.e026048",
|
|
4
4
|
"description": "Helpers to integrate Prismic into Next.js apps",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"next",
|
|
@@ -61,7 +61,7 @@
|
|
|
61
61
|
},
|
|
62
62
|
"devDependencies": {
|
|
63
63
|
"@playwright/test": "^1.64.1",
|
|
64
|
-
"@prismicio/client": "
|
|
64
|
+
"@prismicio/client": "7.22.3-pr.477.5e5ad22",
|
|
65
65
|
"@types/node": "^26.6.5",
|
|
66
66
|
"@types/react": "^19.3.0",
|
|
67
67
|
"next": "16.4.2",
|
|
@@ -73,6 +73,9 @@
|
|
|
73
73
|
"tsdown": "^0.23.1",
|
|
74
74
|
"typescript": "^7.0.2"
|
|
75
75
|
},
|
|
76
|
+
"overrides": {
|
|
77
|
+
"@prismicio/client": "$@prismicio/client"
|
|
78
|
+
},
|
|
76
79
|
"peerDependencies": {
|
|
77
80
|
"@prismicio/client": "^7",
|
|
78
81
|
"next": "^13.4.5 || ^14 || ^15 || ^16",
|
package/src/PrismicPreview.tsx
CHANGED
|
@@ -40,15 +40,15 @@ export type PrismicPreviewProps = {
|
|
|
40
40
|
* rendered on every page.
|
|
41
41
|
*/
|
|
42
42
|
export const PrismicPreview: FC<PrismicPreviewProps> = async (props) => {
|
|
43
|
-
const { repositoryName, children, ...otherProps } = props
|
|
43
|
+
const { repositoryName, updatePreviewURL = "/api/preview", children, ...otherProps } = props
|
|
44
44
|
|
|
45
45
|
// Need this to avoid the following Next.js build-time error:
|
|
46
46
|
// You're importing a component that needs next/headers. That only works
|
|
47
47
|
// in a Server Component which is not supported in the pages/ directory.
|
|
48
48
|
const { draftMode } = await import("next/headers")
|
|
49
49
|
|
|
50
|
-
// TODO:
|
|
51
|
-
const toolbarSrc =
|
|
50
|
+
// TODO: Bump the `@prismicio/client` peer to `^7.23.0`, which adds `version`, and drop `overrides`.
|
|
51
|
+
const toolbarSrc = getToolbarSrc(repositoryName, { version: 5 })
|
|
52
52
|
const isDraftMode = (await draftMode()).isEnabled
|
|
53
53
|
|
|
54
54
|
return (
|
|
@@ -57,9 +57,10 @@ export const PrismicPreview: FC<PrismicPreviewProps> = async (props) => {
|
|
|
57
57
|
<PrismicPreviewClient
|
|
58
58
|
repositoryName={repositoryName}
|
|
59
59
|
isDraftMode={isDraftMode}
|
|
60
|
+
updatePreviewURL={updatePreviewURL}
|
|
60
61
|
{...otherProps}
|
|
61
62
|
/>
|
|
62
|
-
<ResolvedScript src={toolbarSrc} strategy="lazyOnload" />
|
|
63
|
+
<ResolvedScript src={toolbarSrc} strategy="lazyOnload" data-preview-url={updatePreviewURL} />
|
|
63
64
|
</>
|
|
64
65
|
)
|
|
65
66
|
}
|
package/src/exitPreview.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { cookie as prismicCookie } from "@prismicio/client"
|
|
2
2
|
|
|
3
|
-
import { previewSessionCookie } from "./lib/previewSessionCookie"
|
|
4
|
-
|
|
5
3
|
/**
|
|
6
4
|
* Ends a Prismic preview session within a Next.js app. This function should be used in a Router
|
|
7
5
|
* Handler.
|
|
@@ -26,7 +24,7 @@ export async function exitPreview(): Promise<Response> {
|
|
|
26
24
|
const cookieJar = await cookies()
|
|
27
25
|
cookieJar.delete({ name: "__prerender_bypass", sameSite: "none", secure: true })
|
|
28
26
|
cookieJar.delete({ name: prismicCookie.preview, sameSite: "none", secure: true })
|
|
29
|
-
cookieJar.delete({ name:
|
|
27
|
+
cookieJar.delete({ name: prismicCookie.previewSession, sameSite: "none", secure: true })
|
|
30
28
|
|
|
31
29
|
// `Cache-Control` header is used to prevent CDN-level caching.
|
|
32
30
|
return new Response(JSON.stringify({ success: true }), {
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** Returns `path` if it stays on the current origin, like `/blog?page=2#top`. */
|
|
2
|
+
export function getSafePath(path: string | null | undefined): string | undefined {
|
|
3
|
+
if (!path?.startsWith("/") || path.startsWith("//") || path.startsWith("/\\")) {
|
|
4
|
+
return
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
const origin = "https://placeholder.invalid"
|
|
8
|
+
let url: URL
|
|
9
|
+
try {
|
|
10
|
+
url = new URL(path, origin)
|
|
11
|
+
} catch {
|
|
12
|
+
return
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Dot segments can normalize `/a/..//evil.com` to `//evil.com`, which leaves the origin.
|
|
16
|
+
const safePath = url.href.slice(origin.length)
|
|
17
|
+
if (url.origin === origin && !safePath.startsWith("//")) {
|
|
18
|
+
return safePath
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -46,8 +46,7 @@ export const PrismicPreview: FC<PrismicPreviewProps> = (props) => {
|
|
|
46
46
|
|
|
47
47
|
const router = useRouter()
|
|
48
48
|
|
|
49
|
-
|
|
50
|
-
const toolbarSrc = `${getToolbarSrc(repositoryName)}&v=5`
|
|
49
|
+
const toolbarSrc = getToolbarSrc(repositoryName, { version: 5 })
|
|
51
50
|
|
|
52
51
|
useEffect(() => {
|
|
53
52
|
const controller = new AbortController()
|
|
@@ -128,7 +127,11 @@ export const PrismicPreview: FC<PrismicPreviewProps> = (props) => {
|
|
|
128
127
|
return (
|
|
129
128
|
<>
|
|
130
129
|
{children}
|
|
131
|
-
<Script
|
|
130
|
+
<Script
|
|
131
|
+
src={toolbarSrc}
|
|
132
|
+
strategy="lazyOnload"
|
|
133
|
+
data-preview-url={router.basePath + updatePreviewURL}
|
|
134
|
+
/>
|
|
132
135
|
</>
|
|
133
136
|
)
|
|
134
137
|
}
|
package/src/pages/exitPreview.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { cookie as prismicCookie } from "@prismicio/client"
|
|
2
2
|
|
|
3
|
-
import { previewSessionCookie } from "../lib/previewSessionCookie"
|
|
4
3
|
import { appendCookie } from "./appendCookie"
|
|
5
4
|
import type { NextApiRequestLike, NextApiResponseLike } from "./types"
|
|
6
5
|
|
|
@@ -38,7 +37,7 @@ export type ExitPreviewAPIRouteConfig = {
|
|
|
38
37
|
*/
|
|
39
38
|
export function exitPreview(config: ExitPreviewAPIRouteConfig): void {
|
|
40
39
|
config.res.clearPreviewData()
|
|
41
|
-
for (const name of [prismicCookie.preview,
|
|
40
|
+
for (const name of [prismicCookie.preview, prismicCookie.previewSession]) {
|
|
42
41
|
appendCookie(config.res, `${name}=; Expires=${new Date(0).toUTCString()}`)
|
|
43
42
|
}
|
|
44
43
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { cookie as prismicCookie, type LinkResolverFunction, type Client } from "@prismicio/client"
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { getSafePath } from "../lib/getSafePath"
|
|
4
4
|
import { appendCookie } from "./appendCookie"
|
|
5
5
|
import type { NextApiRequestLike, NextApiResponseLike } from "./types"
|
|
6
6
|
|
|
@@ -56,12 +56,22 @@ export async function redirectToPreviewURL(config: RedirectToPreviewURLConfig):
|
|
|
56
56
|
|
|
57
57
|
const previewToken = req.query.token?.toString()
|
|
58
58
|
|
|
59
|
-
// The toolbar's v5 flow follows this session
|
|
60
|
-
//
|
|
59
|
+
// The toolbar's v5 flow follows this session from the ref the page shows. Without that ref, its
|
|
60
|
+
// first ping would start the preview again, racing an early exit.
|
|
61
61
|
const session = req.query.session?.toString()
|
|
62
|
-
if (
|
|
63
|
-
appendCookie(res, `${prismicCookie.
|
|
64
|
-
|
|
62
|
+
if (session) {
|
|
63
|
+
appendCookie(res, `${prismicCookie.previewSession}=${encodeURIComponent(session)}`)
|
|
64
|
+
if (previewToken) {
|
|
65
|
+
appendCookie(res, `${prismicCookie.preview}=${encodeURIComponent(previewToken)}`)
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Share links return to the page they were opened from. Its path includes `basePath`.
|
|
70
|
+
const path = getSafePath(req.query.path?.toString())
|
|
71
|
+
if (path) {
|
|
72
|
+
res.redirect(path)
|
|
73
|
+
|
|
74
|
+
return
|
|
65
75
|
}
|
|
66
76
|
|
|
67
77
|
const previewURL = await client.resolvePreviewURL({
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { cookie as prismicCookie, type Client, type LinkResolverFunction } from "@prismicio/client"
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { getSafePath } from "./lib/getSafePath"
|
|
4
4
|
import type { NextRequestLike } from "./types"
|
|
5
5
|
|
|
6
6
|
export type RedirectToPreviewURLConfig = {
|
|
@@ -60,15 +60,18 @@ export async function redirectToPreviewURL(config: RedirectToPreviewURLConfig):
|
|
|
60
60
|
// The toolbar's v5 flow follows this session.
|
|
61
61
|
const session = request.nextUrl.searchParams.get("session")
|
|
62
62
|
if (session) {
|
|
63
|
-
cookieJar.set(
|
|
63
|
+
cookieJar.set(prismicCookie.previewSession, session, { sameSite: "none", secure: true })
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
66
|
+
// Share links return to the page they were opened from.
|
|
67
|
+
const previewURL =
|
|
68
|
+
getSafePath(request.nextUrl.searchParams.get("path")) ??
|
|
69
|
+
(await client.resolvePreviewURL({
|
|
70
|
+
documentID,
|
|
71
|
+
previewToken,
|
|
72
|
+
defaultURL,
|
|
73
|
+
linkResolver,
|
|
74
|
+
}))
|
|
72
75
|
|
|
73
76
|
;(await draftMode()).enable()
|
|
74
77
|
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
//#region src/lib/previewSessionCookie.ts
|
|
2
|
-
/** The cookie storing the preview session ID. The toolbar's v5 flow follows this session. */
|
|
3
|
-
const previewSessionCookie = "io.prismic.previewSession";
|
|
4
|
-
//#endregion
|
|
5
|
-
exports.previewSessionCookie = previewSessionCookie;
|
|
6
|
-
|
|
7
|
-
//# sourceMappingURL=previewSessionCookie.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"previewSessionCookie.cjs","names":[],"sources":["../../src/lib/previewSessionCookie.ts"],"sourcesContent":["/** The cookie storing the preview session ID. The toolbar's v5 flow follows this session. */\nexport const previewSessionCookie = \"io.prismic.previewSession\"\n"],"mappings":";;AACA,MAAa,uBAAuB"}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
//#region src/lib/previewSessionCookie.ts
|
|
2
|
-
/** The cookie storing the preview session ID. The toolbar's v5 flow follows this session. */
|
|
3
|
-
const previewSessionCookie = "io.prismic.previewSession";
|
|
4
|
-
//#endregion
|
|
5
|
-
export { previewSessionCookie };
|
|
6
|
-
|
|
7
|
-
//# sourceMappingURL=previewSessionCookie.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"previewSessionCookie.js","names":[],"sources":["../../src/lib/previewSessionCookie.ts"],"sourcesContent":["/** The cookie storing the preview session ID. The toolbar's v5 flow follows this session. */\nexport const previewSessionCookie = \"io.prismic.previewSession\"\n"],"mappings":";;AACA,MAAa,uBAAuB"}
|