@prismicio/next 2.3.4-pr.175.9bb2125 → 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.
@@ -16,7 +16,7 @@ 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
21
  const toolbarSrc = (0, _prismicio_client.getToolbarSrc)(repositoryName, { version: 5 });
22
22
  const isDraftMode = (await draftMode()).isEnabled;
@@ -25,11 +25,13 @@ const PrismicPreview = async (props) => {
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: 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\t{...otherProps}\n\t\t\t/>\n\t\t\t<ResolvedScript src={toolbarSrc} strategy=\"lazyOnload\" />\n\t\t</>\n\t)\n}\n"],"mappings":";;;;;;;;AAOA,MAAM,iBAAiBA,6BAAAA,qBAAqBC,eAAAA,OAAM;;;;;;;;;AAkClD,MAAa,iBAA0C,OAAO,UAAU;CACvE,MAAM,EAAE,gBAAgB,UAAU,GAAG,eAAe;CAKpD,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;GACb,GAAI;EACJ,CAAA;EACD,iBAAA,GAAA,kBAAA,IAAA,CAAC,gBAAD;GAAgB,KAAK;GAAY,UAAS;EAAc,CAAA;CACvD,EAAA,CAAA;AAEJ"}
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"}
@@ -14,7 +14,7 @@ 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
19
  const toolbarSrc = getToolbarSrc(repositoryName, { version: 5 });
20
20
  const isDraftMode = (await draftMode()).isEnabled;
@@ -23,11 +23,13 @@ const PrismicPreview = async (props) => {
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: 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\t{...otherProps}\n\t\t\t/>\n\t\t\t<ResolvedScript src={toolbarSrc} strategy=\"lazyOnload\" />\n\t\t</>\n\t)\n}\n"],"mappings":";;;;;;AAOA,MAAM,iBAAiB,qBAAqB,MAAM;;;;;;;;;AAkClD,MAAa,iBAA0C,OAAO,UAAU;CACvE,MAAM,EAAE,gBAAgB,UAAU,GAAG,eAAe;CAKpD,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;GACb,GAAI;EACJ,CAAA;EACD,oBAAC,gBAAD;GAAgB,KAAK;GAAY,UAAS;EAAc,CAAA;CACvD,EAAA,CAAA;AAEJ"}
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"}
@@ -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
@@ -1,5 +1,5 @@
1
1
  //#region package.json
2
- var version = "2.3.4-pr.175.9bb2125";
2
+ var version = "2.3.4-pr.175.e026048";
3
3
  //#endregion
4
4
  Object.defineProperty(exports, "version", {
5
5
  enumerable: true,
package/dist/package.js CHANGED
@@ -1,5 +1,5 @@
1
1
  //#region package.json
2
- var version = "2.3.4-pr.175.9bb2125";
2
+ var version = "2.3.4-pr.175.e026048";
3
3
  //#endregion
4
4
  export { version };
5
5
 
@@ -20,7 +20,6 @@ const PrismicPreview = (props) => {
20
20
  const toolbarSrc = (0, _prismicio_client.getToolbarSrc)(repositoryName, { version: 5 });
21
21
  (0, react.useEffect)(() => {
22
22
  const controller = new AbortController();
23
- window.addEventListener("prismicPreviewStart", onStart, { signal: controller.signal });
24
23
  window.addEventListener("prismicPreviewUpdate", onUpdate, { signal: controller.signal });
25
24
  window.addEventListener("prismicPreviewEnd", onEnd, { signal: controller.signal });
26
25
  if (window.location.href.startsWith(window.location.origin + router.basePath) && getPreviewCookieRepositoryName() === repositoryName && !router.isPreview) start();
@@ -34,15 +33,11 @@ const PrismicPreview = (props) => {
34
33
  refresh();
35
34
  }).catch(() => {});
36
35
  }
37
- function onStart(event) {
38
- event.preventDefault();
39
- start(() => window.location.reload());
40
- }
41
36
  function onUpdate(event) {
42
37
  event.preventDefault();
43
38
  start();
44
39
  }
45
- function start(onStarted = refresh) {
40
+ function start() {
46
41
  fetch(router.basePath + updatePreviewURL, {
47
42
  redirect: "manual",
48
43
  signal: controller.signal
@@ -51,7 +46,7 @@ const PrismicPreview = (props) => {
51
46
  console.error(`[<PrismicPreview>] Failed to start or update the preview using "${updatePreviewURL}". Does it exist?`);
52
47
  return;
53
48
  }
54
- onStarted();
49
+ refresh();
55
50
  }).catch(() => {});
56
51
  }
57
52
  function refresh() {
@@ -66,7 +61,8 @@ const PrismicPreview = (props) => {
66
61
  ]);
67
62
  return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [children, /* @__PURE__ */ (0, react_jsx_runtime.jsx)(next_script_js.default, {
68
63
  src: toolbarSrc,
69
- strategy: "lazyOnload"
64
+ strategy: "lazyOnload",
65
+ "data-preview-url": router.basePath + updatePreviewURL
70
66
  })] });
71
67
  };
72
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\tconst toolbarSrc = getToolbarSrc(repositoryName, { version: 5 })\n\n\tuseEffect(() => {\n\t\tconst controller = new AbortController()\n\n\t\twindow.addEventListener(\"prismicPreviewStart\", onStart, {\n\t\t\tsignal: controller.signal,\n\t\t})\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 onStart(event: Event) {\n\t\t\tevent.preventDefault()\n\t\t\t// `router.replace()` would restore the share link's hash and, as a\n\t\t\t// hash-only change, skip `getServerSideProps`.\n\t\t\tstart(() => window.location.reload())\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(onStarted = refresh) {\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\tonStarted()\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 src={toolbarSrc} strategy=\"lazyOnload\" />\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,uBAAuB,SAAS,EACvD,QAAQ,WAAW,OACpB,CAAC;EACD,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,QAAQ,OAAc;GAC9B,MAAM,eAAe;GAGrB,YAAY,OAAO,SAAS,OAAO,CAAC;EACrC;EAEA,SAAS,SAAS,OAAc;GAC/B,MAAM,eAAe;GACrB,MAAM;EACP;EAEA,SAAS,MAAM,YAAY,SAAS;GAKnC,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,UAAU;GACX,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;EAAQ,KAAK;EAAY,UAAS;CAAc,CAAA,CAC/C,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"}
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"}
@@ -18,7 +18,6 @@ const PrismicPreview = (props) => {
18
18
  const toolbarSrc = getToolbarSrc(repositoryName, { version: 5 });
19
19
  useEffect(() => {
20
20
  const controller = new AbortController();
21
- window.addEventListener("prismicPreviewStart", onStart, { signal: controller.signal });
22
21
  window.addEventListener("prismicPreviewUpdate", onUpdate, { signal: controller.signal });
23
22
  window.addEventListener("prismicPreviewEnd", onEnd, { signal: controller.signal });
24
23
  if (window.location.href.startsWith(window.location.origin + router.basePath) && getPreviewCookieRepositoryName() === repositoryName && !router.isPreview) start();
@@ -32,15 +31,11 @@ const PrismicPreview = (props) => {
32
31
  refresh();
33
32
  }).catch(() => {});
34
33
  }
35
- function onStart(event) {
36
- event.preventDefault();
37
- start(() => window.location.reload());
38
- }
39
34
  function onUpdate(event) {
40
35
  event.preventDefault();
41
36
  start();
42
37
  }
43
- function start(onStarted = refresh) {
38
+ function start() {
44
39
  fetch(router.basePath + updatePreviewURL, {
45
40
  redirect: "manual",
46
41
  signal: controller.signal
@@ -49,7 +44,7 @@ const PrismicPreview = (props) => {
49
44
  console.error(`[<PrismicPreview>] Failed to start or update the preview using "${updatePreviewURL}". Does it exist?`);
50
45
  return;
51
46
  }
52
- onStarted();
47
+ refresh();
53
48
  }).catch(() => {});
54
49
  }
55
50
  function refresh() {
@@ -64,7 +59,8 @@ const PrismicPreview = (props) => {
64
59
  ]);
65
60
  return /* @__PURE__ */ jsxs(Fragment, { children: [children, /* @__PURE__ */ jsx(Script, {
66
61
  src: toolbarSrc,
67
- strategy: "lazyOnload"
62
+ strategy: "lazyOnload",
63
+ "data-preview-url": router.basePath + updatePreviewURL
68
64
  })] });
69
65
  };
70
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\tconst toolbarSrc = getToolbarSrc(repositoryName, { version: 5 })\n\n\tuseEffect(() => {\n\t\tconst controller = new AbortController()\n\n\t\twindow.addEventListener(\"prismicPreviewStart\", onStart, {\n\t\t\tsignal: controller.signal,\n\t\t})\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 onStart(event: Event) {\n\t\t\tevent.preventDefault()\n\t\t\t// `router.replace()` would restore the share link's hash and, as a\n\t\t\t// hash-only change, skip `getServerSideProps`.\n\t\t\tstart(() => window.location.reload())\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(onStarted = refresh) {\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\tonStarted()\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 src={toolbarSrc} strategy=\"lazyOnload\" />\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,uBAAuB,SAAS,EACvD,QAAQ,WAAW,OACpB,CAAC;EACD,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,QAAQ,OAAc;GAC9B,MAAM,eAAe;GAGrB,YAAY,OAAO,SAAS,OAAO,CAAC;EACrC;EAEA,SAAS,SAAS,OAAc;GAC/B,MAAM,eAAe;GACrB,MAAM;EACP;EAEA,SAAS,MAAM,YAAY,SAAS;GAKnC,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,UAAU;GACX,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;EAAQ,KAAK;EAAY,UAAS;CAAc,CAAA,CAC/C,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
+ {"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,3 +1,4 @@
1
+ const require_getSafePath = require("../lib/getSafePath.cjs");
1
2
  const require_appendCookie = require("./appendCookie.cjs");
2
3
  let _prismicio_client = require("@prismicio/client");
3
4
  //#region src/pages/redirectToPreviewURL.ts
@@ -5,7 +6,15 @@ async function redirectToPreviewURL(config) {
5
6
  const { client, req, res, linkResolver, defaultURL = "/", basePath = "" } = config;
6
7
  const previewToken = req.query.token?.toString();
7
8
  const session = req.query.session?.toString();
8
- if (session) require_appendCookie.appendCookie(res, `${_prismicio_client.cookie.previewSession}=${encodeURIComponent(session)}`);
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;
17
+ }
9
18
  const previewURL = await client.resolvePreviewURL({
10
19
  linkResolver,
11
20
  defaultURL,
@@ -1 +1 @@
1
- {"version":3,"file":"redirectToPreviewURL.cjs","names":["prismicCookie"],"sources":["../../src/pages/redirectToPreviewURL.ts"],"sourcesContent":["import { cookie as prismicCookie, type LinkResolverFunction, type Client } from \"@prismicio/client\"\n\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.\n\tconst session = req.query.session?.toString()\n\tif (session) {\n\t\tappendCookie(res, `${prismicCookie.previewSession}=${encodeURIComponent(session)}`)\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":";;;AAoDA,eAAsB,qBAAqB,QAAmD;CAC7F,MAAM,EAAE,QAAQ,KAAK,KAAK,cAAc,aAAa,KAAK,WAAW,OAAO;CAE5E,MAAM,eAAe,IAAI,MAAM,OAAO,SAAS;CAG/C,MAAM,UAAU,IAAI,MAAM,SAAS,SAAS;CAC5C,IAAI,SACH,qBAAA,aAAa,KAAK,GAAGA,kBAAAA,OAAc,eAAe,GAAG,mBAAmB,OAAO,GAAG;CAGnF,MAAM,aAAa,MAAM,OAAO,kBAAkB;EACjD;EACA;EACA;CACD,CAAC;CAED,IAAI,SAAS,WAAW,UAAU;AAGnC"}
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 +1 @@
1
- {"version":3,"file":"redirectToPreviewURL.d.cts","names":[],"sources":["../../src/pages/redirectToPreviewURL.ts"],"mappings":";;;YAKY;;EAKX,QAAQ,KAAK;;;;;;EAOb,KAAK;;;;;;EAOL,KAAK;;;;;;EAOL,eAAe;;;;;;;EAQf;;;;;;;;;EAUA;;wBAGqB,qBAAqB,QAAQ,6BAA6B"}
1
+ {"version":3,"file":"redirectToPreviewURL.d.cts","names":[],"sources":["../../src/pages/redirectToPreviewURL.ts"],"mappings":";;;YAMY;;EAKX,QAAQ,KAAK;;;;;;EAOb,KAAK;;;;;;EAOL,KAAK;;;;;;EAOL,eAAe;;;;;;;EAQf;;;;;;;;;EAUA;;wBAGqB,qBAAqB,QAAQ,6BAA6B"}
@@ -1 +1 @@
1
- {"version":3,"file":"redirectToPreviewURL.d.ts","names":[],"sources":["../../src/pages/redirectToPreviewURL.ts"],"mappings":";;;YAKY;;EAKX,QAAQ,KAAK;;;;;;EAOb,KAAK;;;;;;EAOL,KAAK;;;;;;EAOL,eAAe;;;;;;;EAQf;;;;;;;;;EAUA;;wBAGqB,qBAAqB,QAAQ,6BAA6B"}
1
+ {"version":3,"file":"redirectToPreviewURL.d.ts","names":[],"sources":["../../src/pages/redirectToPreviewURL.ts"],"mappings":";;;YAMY;;EAKX,QAAQ,KAAK;;;;;;EAOb,KAAK;;;;;;EAOL,KAAK;;;;;;EAOL,eAAe;;;;;;;EAQf;;;;;;;;;EAUA;;wBAGqB,qBAAqB,QAAQ,6BAA6B"}
@@ -1,3 +1,4 @@
1
+ import { getSafePath } from "../lib/getSafePath.js";
1
2
  import { appendCookie } from "./appendCookie.js";
2
3
  import { cookie } from "@prismicio/client";
3
4
  //#region src/pages/redirectToPreviewURL.ts
@@ -5,7 +6,15 @@ async function redirectToPreviewURL(config) {
5
6
  const { client, req, res, linkResolver, defaultURL = "/", basePath = "" } = config;
6
7
  const previewToken = req.query.token?.toString();
7
8
  const session = req.query.session?.toString();
8
- if (session) appendCookie(res, `${cookie.previewSession}=${encodeURIComponent(session)}`);
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;
17
+ }
9
18
  const previewURL = await client.resolvePreviewURL({
10
19
  linkResolver,
11
20
  defaultURL,
@@ -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 { 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.\n\tconst session = req.query.session?.toString()\n\tif (session) {\n\t\tappendCookie(res, `${prismicCookie.previewSession}=${encodeURIComponent(session)}`)\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":";;;AAoDA,eAAsB,qBAAqB,QAAmD;CAC7F,MAAM,EAAE,QAAQ,KAAK,KAAK,cAAc,aAAa,KAAK,WAAW,OAAO;CAE5E,MAAM,eAAe,IAAI,MAAM,OAAO,SAAS;CAG/C,MAAM,UAAU,IAAI,MAAM,SAAS,SAAS;CAC5C,IAAI,SACH,aAAa,KAAK,GAAGA,OAAc,eAAe,GAAG,mBAAmB,OAAO,GAAG;CAGnF,MAAM,aAAa,MAAM,OAAO,kBAAkB;EACjD;EACA;EACA;CACD,CAAC;CAED,IAAI,SAAS,WAAW,UAAU;AAGnC"}
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,3 +1,4 @@
1
+ const require_getSafePath = require("./lib/getSafePath.cjs");
1
2
  let _prismicio_client = require("@prismicio/client");
2
3
  //#region src/redirectToPreviewURL.ts
3
4
  async function redirectToPreviewURL(config) {
@@ -16,7 +17,7 @@ async function redirectToPreviewURL(config) {
16
17
  sameSite: "none",
17
18
  secure: true
18
19
  });
19
- const previewURL = await client.resolvePreviewURL({
20
+ const previewURL = require_getSafePath.getSafePath(request.nextUrl.searchParams.get("path")) ?? await client.resolvePreviewURL({
20
21
  documentID,
21
22
  previewToken,
22
23
  defaultURL,
@@ -1 +1 @@
1
- {"version":3,"file":"redirectToPreviewURL.cjs","names":["prismicCookie"],"sources":["../src/redirectToPreviewURL.ts"],"sourcesContent":["import { cookie as prismicCookie, type Client, type LinkResolverFunction } from \"@prismicio/client\"\n\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\tconst previewURL = await client.resolvePreviewURL({\n\t\tdocumentID,\n\t\tpreviewToken,\n\t\tdefaultURL,\n\t\tlinkResolver,\n\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":";;AAkCA,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;CAGxF,MAAM,aAAa,MAAM,OAAO,kBAAkB;EACjD;EACA;EACA;EACA;CACD,CAAC;CAEA,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
+ {"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 +1 @@
1
- {"version":3,"file":"redirectToPreviewURL.d.cts","names":[],"sources":["../src/redirectToPreviewURL.ts"],"mappings":";;;YAIY;;EAKX,QAAQ,KAAK;;;;;;EAOb,SAAS;;;;;;EAOT,eAAe;;;;;;;EAQf;;wBAGqB,qBAAqB,QAAQ,6BAA6B"}
1
+ {"version":3,"file":"redirectToPreviewURL.d.cts","names":[],"sources":["../src/redirectToPreviewURL.ts"],"mappings":";;;YAKY;;EAKX,QAAQ,KAAK;;;;;;EAOb,SAAS;;;;;;EAOT,eAAe;;;;;;;EAQf;;wBAGqB,qBAAqB,QAAQ,6BAA6B"}
@@ -1 +1 @@
1
- {"version":3,"file":"redirectToPreviewURL.d.ts","names":[],"sources":["../src/redirectToPreviewURL.ts"],"mappings":";;;YAIY;;EAKX,QAAQ,KAAK;;;;;;EAOb,SAAS;;;;;;EAOT,eAAe;;;;;;;EAQf;;wBAGqB,qBAAqB,QAAQ,6BAA6B"}
1
+ {"version":3,"file":"redirectToPreviewURL.d.ts","names":[],"sources":["../src/redirectToPreviewURL.ts"],"mappings":";;;YAKY;;EAKX,QAAQ,KAAK;;;;;;EAOb,SAAS;;;;;;EAOT,eAAe;;;;;;;EAQf;;wBAGqB,qBAAqB,QAAQ,6BAA6B"}
@@ -1,3 +1,4 @@
1
+ import { getSafePath } from "./lib/getSafePath.js";
1
2
  import { cookie } from "@prismicio/client";
2
3
  //#region src/redirectToPreviewURL.ts
3
4
  async function redirectToPreviewURL(config) {
@@ -16,7 +17,7 @@ async function redirectToPreviewURL(config) {
16
17
  sameSite: "none",
17
18
  secure: true
18
19
  });
19
- const previewURL = await client.resolvePreviewURL({
20
+ const previewURL = getSafePath(request.nextUrl.searchParams.get("path")) ?? await client.resolvePreviewURL({
20
21
  documentID,
21
22
  previewToken,
22
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 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\tconst previewURL = await client.resolvePreviewURL({\n\t\tdocumentID,\n\t\tpreviewToken,\n\t\tdefaultURL,\n\t\tlinkResolver,\n\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":";;AAkCA,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;CAGxF,MAAM,aAAa,MAAM,OAAO,kBAAkB;EACjD;EACA;EACA;EACA;CACD,CAAC;CAEA,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
+ {"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.9bb2125",
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",
@@ -40,7 +40,7 @@ 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
@@ -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
  }
@@ -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
+ }
@@ -51,9 +51,6 @@ export const PrismicPreview: FC<PrismicPreviewProps> = (props) => {
51
51
  useEffect(() => {
52
52
  const controller = new AbortController()
53
53
 
54
- window.addEventListener("prismicPreviewStart", onStart, {
55
- signal: controller.signal,
56
- })
57
54
  window.addEventListener("prismicPreviewUpdate", onUpdate, {
58
55
  signal: controller.signal,
59
56
  })
@@ -92,19 +89,12 @@ export const PrismicPreview: FC<PrismicPreviewProps> = (props) => {
92
89
  .catch(() => {})
93
90
  }
94
91
 
95
- function onStart(event: Event) {
96
- event.preventDefault()
97
- // `router.replace()` would restore the share link's hash and, as a
98
- // hash-only change, skip `getServerSideProps`.
99
- start(() => window.location.reload())
100
- }
101
-
102
92
  function onUpdate(event: Event) {
103
93
  event.preventDefault()
104
94
  start()
105
95
  }
106
96
 
107
- function start(onStarted = refresh) {
97
+ function start() {
108
98
  // We check `opaqueredirect` because we don't care if
109
99
  // the redirect was successful or not. As long as it
110
100
  // redirects, we know the endpoint exists and at least
@@ -122,7 +112,7 @@ export const PrismicPreview: FC<PrismicPreviewProps> = (props) => {
122
112
  return
123
113
  }
124
114
 
125
- onStarted()
115
+ refresh()
126
116
  })
127
117
  .catch(() => {})
128
118
  }
@@ -137,7 +127,11 @@ export const PrismicPreview: FC<PrismicPreviewProps> = (props) => {
137
127
  return (
138
128
  <>
139
129
  {children}
140
- <Script src={toolbarSrc} strategy="lazyOnload" />
130
+ <Script
131
+ src={toolbarSrc}
132
+ strategy="lazyOnload"
133
+ data-preview-url={router.basePath + updatePreviewURL}
134
+ />
141
135
  </>
142
136
  )
143
137
  }
@@ -1,5 +1,6 @@
1
1
  import { cookie as prismicCookie, type LinkResolverFunction, type Client } from "@prismicio/client"
2
2
 
3
+ import { getSafePath } from "../lib/getSafePath"
3
4
  import { appendCookie } from "./appendCookie"
4
5
  import type { NextApiRequestLike, NextApiResponseLike } from "./types"
5
6
 
@@ -55,10 +56,22 @@ export async function redirectToPreviewURL(config: RedirectToPreviewURLConfig):
55
56
 
56
57
  const previewToken = req.query.token?.toString()
57
58
 
58
- // The toolbar's v5 flow follows this session.
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.
59
61
  const session = req.query.session?.toString()
60
62
  if (session) {
61
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
62
75
  }
63
76
 
64
77
  const previewURL = await client.resolvePreviewURL({
@@ -1,5 +1,6 @@
1
1
  import { cookie as prismicCookie, type Client, type LinkResolverFunction } from "@prismicio/client"
2
2
 
3
+ import { getSafePath } from "./lib/getSafePath"
3
4
  import type { NextRequestLike } from "./types"
4
5
 
5
6
  export type RedirectToPreviewURLConfig = {
@@ -62,12 +63,15 @@ export async function redirectToPreviewURL(config: RedirectToPreviewURLConfig):
62
63
  cookieJar.set(prismicCookie.previewSession, session, { sameSite: "none", secure: true })
63
64
  }
64
65
 
65
- const previewURL = await client.resolvePreviewURL({
66
- documentID,
67
- previewToken,
68
- defaultURL,
69
- linkResolver,
70
- })
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
+ }))
71
75
 
72
76
  ;(await draftMode()).enable()
73
77