@prismicio/next 2.3.4-pr.174.890bc09 → 2.3.4-pr.175.20dc589
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 +5 -0
- package/dist/exitPreview.cjs.map +1 -1
- package/dist/exitPreview.js +5 -0
- package/dist/exitPreview.js.map +1 -1
- package/dist/lib/getSafePath.cjs +21 -0
- package/dist/lib/getSafePath.cjs.map +1 -0
- package/dist/lib/getSafePath.js +21 -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/appendCookie.cjs +10 -0
- package/dist/pages/appendCookie.cjs.map +1 -0
- package/dist/pages/appendCookie.js +10 -0
- package/dist/pages/appendCookie.js.map +1 -0
- package/dist/pages/exitPreview.cjs +3 -0
- 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 +3 -0
- package/dist/pages/exitPreview.js.map +1 -1
- package/dist/pages/redirectToPreviewURL.cjs +13 -0
- package/dist/pages/redirectToPreviewURL.cjs.map +1 -1
- package/dist/pages/redirectToPreviewURL.d.cts.map +1 -1
- package/dist/pages/redirectToPreviewURL.d.ts.map +1 -1
- package/dist/pages/redirectToPreviewURL.js +13 -0
- package/dist/pages/redirectToPreviewURL.js.map +1 -1
- package/dist/pages/types.d.cts +1 -0
- package/dist/pages/types.d.cts.map +1 -1
- package/dist/pages/types.d.ts +1 -0
- package/dist/pages/types.d.ts.map +1 -1
- package/dist/redirectToPreviewURL.cjs +7 -1
- package/dist/redirectToPreviewURL.cjs.map +1 -1
- package/dist/redirectToPreviewURL.d.cts.map +1 -1
- package/dist/redirectToPreviewURL.d.ts.map +1 -1
- package/dist/redirectToPreviewURL.js +7 -1
- package/dist/redirectToPreviewURL.js.map +1 -1
- package/package.json +10 -10
- package/src/PrismicPreview.tsx +4 -3
- package/src/exitPreview.ts +1 -0
- package/src/lib/getSafePath.ts +22 -0
- package/src/pages/PrismicPreview.tsx +6 -2
- package/src/pages/appendCookie.ts +10 -0
- package/src/pages/exitPreview.ts +6 -0
- package/src/pages/redirectToPreviewURL.ts +21 -1
- package/src/pages/types.ts +1 -0
- package/src/redirectToPreviewURL.ts +16 -6
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 = (0, _prismicio_client.getToolbarSrc)(repositoryName);
|
|
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\tconst toolbarSrc = getToolbarSrc(repositoryName)\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;
|
|
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\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;CAEnC,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 = getToolbarSrc(repositoryName);
|
|
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\tconst toolbarSrc = getToolbarSrc(repositoryName)\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;
|
|
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\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;CAEnC,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
|
@@ -28,6 +28,11 @@ async function exitPreview() {
|
|
|
28
28
|
sameSite: "none",
|
|
29
29
|
secure: true
|
|
30
30
|
});
|
|
31
|
+
cookieJar.delete({
|
|
32
|
+
name: _prismicio_client.cookie.previewSession,
|
|
33
|
+
sameSite: "none",
|
|
34
|
+
secure: true
|
|
35
|
+
});
|
|
31
36
|
return new Response(JSON.stringify({ success: true }), { headers: { "Cache-Control": "no-store" } });
|
|
32
37
|
}
|
|
33
38
|
//#endregion
|
package/dist/exitPreview.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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\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;
|
|
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"}
|
package/dist/exitPreview.js
CHANGED
|
@@ -28,6 +28,11 @@ async function exitPreview() {
|
|
|
28
28
|
sameSite: "none",
|
|
29
29
|
secure: true
|
|
30
30
|
});
|
|
31
|
+
cookieJar.delete({
|
|
32
|
+
name: cookie.previewSession,
|
|
33
|
+
sameSite: "none",
|
|
34
|
+
secure: true
|
|
35
|
+
});
|
|
31
36
|
return new Response(JSON.stringify({ success: true }), { headers: { "Cache-Control": "no-store" } });
|
|
32
37
|
}
|
|
33
38
|
//#endregion
|
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\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\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;
|
|
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,21 @@
|
|
|
1
|
+
//#region src/lib/getSafePath.ts
|
|
2
|
+
/**
|
|
3
|
+
* Returns `path` if it stays on the current origin, like `/blog?page=2#top`. It comes from
|
|
4
|
+
* `location`, so it already includes `basePath`.
|
|
5
|
+
*/
|
|
6
|
+
function getSafePath(path) {
|
|
7
|
+
if (!path?.startsWith("/") || path.startsWith("//") || path.startsWith("/\\")) return;
|
|
8
|
+
const origin = "https://placeholder.invalid";
|
|
9
|
+
let url;
|
|
10
|
+
try {
|
|
11
|
+
url = new URL(path, origin);
|
|
12
|
+
} catch {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
const safePath = url.href.slice(27);
|
|
16
|
+
if (url.origin === origin && !safePath.startsWith("//")) return safePath;
|
|
17
|
+
}
|
|
18
|
+
//#endregion
|
|
19
|
+
exports.getSafePath = getSafePath;
|
|
20
|
+
|
|
21
|
+
//# sourceMappingURL=getSafePath.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getSafePath.cjs","names":[],"sources":["../../src/lib/getSafePath.ts"],"sourcesContent":["/**\n * Returns `path` if it stays on the current origin, like `/blog?page=2#top`. It comes from\n * `location`, so it already includes `basePath`.\n */\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\tconst safePath = url.href.slice(origin.length)\n\t// Normalizing can create `//`, like with `/.//example.com`.\n\tif (url.origin === origin && !safePath.startsWith(\"//\")) {\n\t\treturn safePath\n\t}\n}\n"],"mappings":";;;;;AAIA,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;CACA,MAAM,WAAW,IAAI,KAAK,MAAM,EAAa;CAE7C,IAAI,IAAI,WAAW,UAAU,CAAC,SAAS,WAAW,IAAI,GACrD,OAAO;AAET"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//#region src/lib/getSafePath.ts
|
|
2
|
+
/**
|
|
3
|
+
* Returns `path` if it stays on the current origin, like `/blog?page=2#top`. It comes from
|
|
4
|
+
* `location`, so it already includes `basePath`.
|
|
5
|
+
*/
|
|
6
|
+
function getSafePath(path) {
|
|
7
|
+
if (!path?.startsWith("/") || path.startsWith("//") || path.startsWith("/\\")) return;
|
|
8
|
+
const origin = "https://placeholder.invalid";
|
|
9
|
+
let url;
|
|
10
|
+
try {
|
|
11
|
+
url = new URL(path, origin);
|
|
12
|
+
} catch {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
const safePath = url.href.slice(27);
|
|
16
|
+
if (url.origin === origin && !safePath.startsWith("//")) return safePath;
|
|
17
|
+
}
|
|
18
|
+
//#endregion
|
|
19
|
+
export { getSafePath };
|
|
20
|
+
|
|
21
|
+
//# sourceMappingURL=getSafePath.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getSafePath.js","names":[],"sources":["../../src/lib/getSafePath.ts"],"sourcesContent":["/**\n * Returns `path` if it stays on the current origin, like `/blog?page=2#top`. It comes from\n * `location`, so it already includes `basePath`.\n */\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\tconst safePath = url.href.slice(origin.length)\n\t// Normalizing can create `//`, like with `/.//example.com`.\n\tif (url.origin === origin && !safePath.startsWith(\"//\")) {\n\t\treturn safePath\n\t}\n}\n"],"mappings":";;;;;AAIA,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;CACA,MAAM,WAAW,IAAI,KAAK,MAAM,EAAa;CAE7C,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 = (0, _prismicio_client.getToolbarSrc)(repositoryName);
|
|
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\tconst toolbarSrc = getToolbarSrc(repositoryName)\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
|
|
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 = getToolbarSrc(repositoryName);
|
|
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\tconst toolbarSrc = getToolbarSrc(repositoryName)\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
|
|
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"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
//#region src/pages/appendCookie.ts
|
|
2
|
+
/** Adds a cookie like the App Router's preview cookie, keeping Next.js's preview data cookies. */
|
|
3
|
+
function appendCookie(res, cookie) {
|
|
4
|
+
const previous = res.getHeader("Set-Cookie") ?? [];
|
|
5
|
+
res.setHeader("Set-Cookie", [...[previous].flat().map(String), `${cookie}; Path=/; SameSite=None; Secure`]);
|
|
6
|
+
}
|
|
7
|
+
//#endregion
|
|
8
|
+
exports.appendCookie = appendCookie;
|
|
9
|
+
|
|
10
|
+
//# sourceMappingURL=appendCookie.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"appendCookie.cjs","names":[],"sources":["../../src/pages/appendCookie.ts"],"sourcesContent":["import type { NextApiResponseLike } from \"./types\"\n\n/** Adds a cookie like the App Router's preview cookie, keeping Next.js's preview data cookies. */\nexport function appendCookie(res: NextApiResponseLike, cookie: string): void {\n\tconst previous = res.getHeader(\"Set-Cookie\") ?? []\n\tres.setHeader(\"Set-Cookie\", [\n\t\t...[previous].flat().map(String),\n\t\t`${cookie}; Path=/; SameSite=None; Secure`,\n\t])\n}\n"],"mappings":";;AAGA,SAAgB,aAAa,KAA0B,QAAsB;CAC5E,MAAM,WAAW,IAAI,UAAU,YAAY,KAAK,CAAC;CACjD,IAAI,UAAU,cAAc,CAC3B,GAAG,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,MAAM,GAC/B,GAAG,OAAO,gCACX,CAAC;AACF"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
//#region src/pages/appendCookie.ts
|
|
2
|
+
/** Adds a cookie like the App Router's preview cookie, keeping Next.js's preview data cookies. */
|
|
3
|
+
function appendCookie(res, cookie) {
|
|
4
|
+
const previous = res.getHeader("Set-Cookie") ?? [];
|
|
5
|
+
res.setHeader("Set-Cookie", [...[previous].flat().map(String), `${cookie}; Path=/; SameSite=None; Secure`]);
|
|
6
|
+
}
|
|
7
|
+
//#endregion
|
|
8
|
+
export { appendCookie };
|
|
9
|
+
|
|
10
|
+
//# sourceMappingURL=appendCookie.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"appendCookie.js","names":[],"sources":["../../src/pages/appendCookie.ts"],"sourcesContent":["import type { NextApiResponseLike } from \"./types\"\n\n/** Adds a cookie like the App Router's preview cookie, keeping Next.js's preview data cookies. */\nexport function appendCookie(res: NextApiResponseLike, cookie: string): void {\n\tconst previous = res.getHeader(\"Set-Cookie\") ?? []\n\tres.setHeader(\"Set-Cookie\", [\n\t\t...[previous].flat().map(String),\n\t\t`${cookie}; Path=/; SameSite=None; Secure`,\n\t])\n}\n"],"mappings":";;AAGA,SAAgB,aAAa,KAA0B,QAAsB;CAC5E,MAAM,WAAW,IAAI,UAAU,YAAY,KAAK,CAAC;CACjD,IAAI,UAAU,cAAc,CAC3B,GAAG,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,MAAM,GAC/B,GAAG,OAAO,gCACX,CAAC;AACF"}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
const require_appendCookie = require("./appendCookie.cjs");
|
|
2
|
+
let _prismicio_client = require("@prismicio/client");
|
|
1
3
|
//#region src/pages/exitPreview.ts
|
|
2
4
|
/**
|
|
3
5
|
* Ends a Prismic preview session within a Next.js app. This function should be used in an API
|
|
@@ -16,6 +18,7 @@
|
|
|
16
18
|
*/
|
|
17
19
|
function exitPreview(config) {
|
|
18
20
|
config.res.clearPreviewData();
|
|
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()}`);
|
|
19
22
|
config.res.setHeader("Cache-Control", "no-store");
|
|
20
23
|
config.res.json({ success: true });
|
|
21
24
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exitPreview.cjs","names":[],"sources":["../../src/pages/exitPreview.ts"],"sourcesContent":["import 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\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":"
|
|
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,3 +1,5 @@
|
|
|
1
|
+
import { appendCookie } from "./appendCookie.js";
|
|
2
|
+
import { cookie } from "@prismicio/client";
|
|
1
3
|
//#region src/pages/exitPreview.ts
|
|
2
4
|
/**
|
|
3
5
|
* Ends a Prismic preview session within a Next.js app. This function should be used in an API
|
|
@@ -16,6 +18,7 @@
|
|
|
16
18
|
*/
|
|
17
19
|
function exitPreview(config) {
|
|
18
20
|
config.res.clearPreviewData();
|
|
21
|
+
for (const name of [cookie.preview, cookie.previewSession]) appendCookie(config.res, `${name}=; Expires=${(/* @__PURE__ */ new Date(0)).toUTCString()}`);
|
|
19
22
|
config.res.setHeader("Cache-Control", "no-store");
|
|
20
23
|
config.res.json({ success: true });
|
|
21
24
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exitPreview.js","names":[],"sources":["../../src/pages/exitPreview.ts"],"sourcesContent":["import 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\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":"
|
|
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,7 +1,20 @@
|
|
|
1
|
+
const require_getSafePath = require("../lib/getSafePath.cjs");
|
|
2
|
+
const require_appendCookie = require("./appendCookie.cjs");
|
|
3
|
+
let _prismicio_client = require("@prismicio/client");
|
|
1
4
|
//#region src/pages/redirectToPreviewURL.ts
|
|
2
5
|
async function redirectToPreviewURL(config) {
|
|
3
6
|
const { client, req, res, linkResolver, defaultURL = "/", basePath = "" } = config;
|
|
4
7
|
const previewToken = req.query.token?.toString();
|
|
8
|
+
const session = req.query.session?.toString();
|
|
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
|
+
}
|
|
5
18
|
const previewURL = await client.resolvePreviewURL({
|
|
6
19
|
linkResolver,
|
|
7
20
|
defaultURL,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redirectToPreviewURL.cjs","names":[],"sources":["../../src/pages/redirectToPreviewURL.ts"],"sourcesContent":["import type
|
|
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.\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":";;;
|
|
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":";;;
|
|
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,7 +1,20 @@
|
|
|
1
|
+
import { getSafePath } from "../lib/getSafePath.js";
|
|
2
|
+
import { appendCookie } from "./appendCookie.js";
|
|
3
|
+
import { cookie } from "@prismicio/client";
|
|
1
4
|
//#region src/pages/redirectToPreviewURL.ts
|
|
2
5
|
async function redirectToPreviewURL(config) {
|
|
3
6
|
const { client, req, res, linkResolver, defaultURL = "/", basePath = "" } = config;
|
|
4
7
|
const previewToken = req.query.token?.toString();
|
|
8
|
+
const session = req.query.session?.toString();
|
|
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
|
+
}
|
|
5
18
|
const previewURL = await client.resolvePreviewURL({
|
|
6
19
|
linkResolver,
|
|
7
20
|
defaultURL,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redirectToPreviewURL.js","names":[],"sources":["../../src/pages/redirectToPreviewURL.ts"],"sourcesContent":["import type
|
|
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.\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"}
|
package/dist/pages/types.d.cts
CHANGED
|
@@ -20,6 +20,7 @@ export type NextApiRequestLike = {
|
|
|
20
20
|
export type NextApiResponseLike = {
|
|
21
21
|
redirect(url: string): NextApiResponseLike;
|
|
22
22
|
clearPreviewData(): NextApiResponseLike;
|
|
23
|
+
getHeader(name: string): number | string | string[] | undefined;
|
|
23
24
|
setHeader(name: string, value: string | string[]): NextApiResponseLike;
|
|
24
25
|
json(body: any): void;
|
|
25
26
|
setPreviewData(data: object | string): NextApiResponseLike;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.cts","names":[],"sources":["../../src/pages/types.ts"],"mappings":";;;;YAKY,qBAAqB,eAChC,KAAK;;;;;;YAOM;EACX,OAAO,QAAQ;EACf,SAAS,QAAQ;;;;;;;YAQN;EACX,SAAS,cAAc;EACvB,oBAAoB;EACpB,UAAU,cAAc,2BAA2B;EAEnD,KAAK;EACL,eAAe,wBAAwB"}
|
|
1
|
+
{"version":3,"file":"types.d.cts","names":[],"sources":["../../src/pages/types.ts"],"mappings":";;;;YAKY,qBAAqB,eAChC,KAAK;;;;;;YAOM;EACX,OAAO,QAAQ;EACf,SAAS,QAAQ;;;;;;;YAQN;EACX,SAAS,cAAc;EACvB,oBAAoB;EACpB,UAAU;EACV,UAAU,cAAc,2BAA2B;EAEnD,KAAK;EACL,eAAe,wBAAwB"}
|
package/dist/pages/types.d.ts
CHANGED
|
@@ -20,6 +20,7 @@ export type NextApiRequestLike = {
|
|
|
20
20
|
export type NextApiResponseLike = {
|
|
21
21
|
redirect(url: string): NextApiResponseLike;
|
|
22
22
|
clearPreviewData(): NextApiResponseLike;
|
|
23
|
+
getHeader(name: string): number | string | string[] | undefined;
|
|
23
24
|
setHeader(name: string, value: string | string[]): NextApiResponseLike;
|
|
24
25
|
json(body: any): void;
|
|
25
26
|
setPreviewData(data: object | string): NextApiResponseLike;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","names":[],"sources":["../../src/pages/types.ts"],"mappings":";;;;YAKY,qBAAqB,eAChC,KAAK;;;;;;YAOM;EACX,OAAO,QAAQ;EACf,SAAS,QAAQ;;;;;;;YAQN;EACX,SAAS,cAAc;EACvB,oBAAoB;EACpB,UAAU,cAAc,2BAA2B;EAEnD,KAAK;EACL,eAAe,wBAAwB"}
|
|
1
|
+
{"version":3,"file":"types.d.ts","names":[],"sources":["../../src/pages/types.ts"],"mappings":";;;;YAKY,qBAAqB,eAChC,KAAK;;;;;;YAOM;EACX,OAAO,QAAQ;EACf,SAAS,QAAQ;;;;;;;YAQN;EACX,SAAS,cAAc;EACvB,oBAAoB;EACpB,UAAU;EACV,UAAU,cAAc,2BAA2B;EAEnD,KAAK;EACL,eAAe,wBAAwB"}
|
|
@@ -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) {
|
|
@@ -11,7 +12,12 @@ async function redirectToPreviewURL(config) {
|
|
|
11
12
|
sameSite: "none",
|
|
12
13
|
secure: true
|
|
13
14
|
});
|
|
14
|
-
const
|
|
15
|
+
const session = request.nextUrl.searchParams.get("session");
|
|
16
|
+
if (session) cookieJar.set(_prismicio_client.cookie.previewSession, session, {
|
|
17
|
+
sameSite: "none",
|
|
18
|
+
secure: true
|
|
19
|
+
});
|
|
20
|
+
const previewURL = require_getSafePath.getSafePath(request.nextUrl.searchParams.get("path")) ?? await client.resolvePreviewURL({
|
|
15
21
|
documentID,
|
|
16
22
|
previewToken,
|
|
17
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\tconst
|
|
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":";;;
|
|
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":";;;
|
|
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) {
|
|
@@ -11,7 +12,12 @@ async function redirectToPreviewURL(config) {
|
|
|
11
12
|
sameSite: "none",
|
|
12
13
|
secure: true
|
|
13
14
|
});
|
|
14
|
-
const
|
|
15
|
+
const session = request.nextUrl.searchParams.get("session");
|
|
16
|
+
if (session) cookieJar.set(cookie.previewSession, session, {
|
|
17
|
+
sameSite: "none",
|
|
18
|
+
secure: true
|
|
19
|
+
});
|
|
20
|
+
const previewURL = getSafePath(request.nextUrl.searchParams.get("path")) ?? await client.resolvePreviewURL({
|
|
15
21
|
documentID,
|
|
16
22
|
previewToken,
|
|
17
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\tconst
|
|
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.
|
|
3
|
+
"version": "2.3.4-pr.175.20dc589",
|
|
4
4
|
"description": "Helpers to integrate Prismic into Next.js apps",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"next",
|
|
@@ -60,21 +60,21 @@
|
|
|
60
60
|
"lz-string": "^1.5.0"
|
|
61
61
|
},
|
|
62
62
|
"devDependencies": {
|
|
63
|
-
"@playwright/test": "^1.64.
|
|
64
|
-
"@prismicio/client": "^7.
|
|
63
|
+
"@playwright/test": "^1.64.1",
|
|
64
|
+
"@prismicio/client": "^7.23.0",
|
|
65
65
|
"@types/node": "^26.6.5",
|
|
66
66
|
"@types/react": "^19.3.0",
|
|
67
|
-
"next": "16.4.
|
|
68
|
-
"oxfmt": "^0.
|
|
69
|
-
"oxlint": "^1.
|
|
70
|
-
"playwright": "^1.64.
|
|
71
|
-
"react": "^19.3.
|
|
72
|
-
"react-dom": "^19.3.
|
|
67
|
+
"next": "16.4.2",
|
|
68
|
+
"oxfmt": "^0.73.0",
|
|
69
|
+
"oxlint": "^1.88.0",
|
|
70
|
+
"playwright": "^1.64.1",
|
|
71
|
+
"react": "^19.3.1",
|
|
72
|
+
"react-dom": "^19.3.1",
|
|
73
73
|
"tsdown": "^0.23.1",
|
|
74
74
|
"typescript": "^7.0.2"
|
|
75
75
|
},
|
|
76
76
|
"peerDependencies": {
|
|
77
|
-
"@prismicio/client": "^7",
|
|
77
|
+
"@prismicio/client": "^7.23.0",
|
|
78
78
|
"next": "^13.4.5 || ^14 || ^15 || ^16",
|
|
79
79
|
"react": "^18 || ^19"
|
|
80
80
|
},
|
package/src/PrismicPreview.tsx
CHANGED
|
@@ -40,14 +40,14 @@ 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
|
-
const toolbarSrc = getToolbarSrc(repositoryName)
|
|
50
|
+
const toolbarSrc = getToolbarSrc(repositoryName, { version: 5 })
|
|
51
51
|
const isDraftMode = (await draftMode()).isEnabled
|
|
52
52
|
|
|
53
53
|
return (
|
|
@@ -56,9 +56,10 @@ export const PrismicPreview: FC<PrismicPreviewProps> = async (props) => {
|
|
|
56
56
|
<PrismicPreviewClient
|
|
57
57
|
repositoryName={repositoryName}
|
|
58
58
|
isDraftMode={isDraftMode}
|
|
59
|
+
updatePreviewURL={updatePreviewURL}
|
|
59
60
|
{...otherProps}
|
|
60
61
|
/>
|
|
61
|
-
<ResolvedScript src={toolbarSrc} strategy="lazyOnload" />
|
|
62
|
+
<ResolvedScript src={toolbarSrc} strategy="lazyOnload" data-preview-url={updatePreviewURL} />
|
|
62
63
|
</>
|
|
63
64
|
)
|
|
64
65
|
}
|
package/src/exitPreview.ts
CHANGED
|
@@ -24,6 +24,7 @@ export async function exitPreview(): Promise<Response> {
|
|
|
24
24
|
const cookieJar = await cookies()
|
|
25
25
|
cookieJar.delete({ name: "__prerender_bypass", sameSite: "none", secure: true })
|
|
26
26
|
cookieJar.delete({ name: prismicCookie.preview, sameSite: "none", secure: true })
|
|
27
|
+
cookieJar.delete({ name: prismicCookie.previewSession, sameSite: "none", secure: true })
|
|
27
28
|
|
|
28
29
|
// `Cache-Control` header is used to prevent CDN-level caching.
|
|
29
30
|
return new Response(JSON.stringify({ success: true }), {
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns `path` if it stays on the current origin, like `/blog?page=2#top`. It comes from
|
|
3
|
+
* `location`, so it already includes `basePath`.
|
|
4
|
+
*/
|
|
5
|
+
export function getSafePath(path: string | null | undefined): string | undefined {
|
|
6
|
+
if (!path?.startsWith("/") || path.startsWith("//") || path.startsWith("/\\")) {
|
|
7
|
+
return
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const origin = "https://placeholder.invalid"
|
|
11
|
+
let url: URL
|
|
12
|
+
try {
|
|
13
|
+
url = new URL(path, origin)
|
|
14
|
+
} catch {
|
|
15
|
+
return
|
|
16
|
+
}
|
|
17
|
+
const safePath = url.href.slice(origin.length)
|
|
18
|
+
// Normalizing can create `//`, like with `/.//example.com`.
|
|
19
|
+
if (url.origin === origin && !safePath.startsWith("//")) {
|
|
20
|
+
return safePath
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -46,7 +46,7 @@ export const PrismicPreview: FC<PrismicPreviewProps> = (props) => {
|
|
|
46
46
|
|
|
47
47
|
const router = useRouter()
|
|
48
48
|
|
|
49
|
-
const toolbarSrc = getToolbarSrc(repositoryName)
|
|
49
|
+
const toolbarSrc = getToolbarSrc(repositoryName, { version: 5 })
|
|
50
50
|
|
|
51
51
|
useEffect(() => {
|
|
52
52
|
const controller = new AbortController()
|
|
@@ -127,7 +127,11 @@ export const PrismicPreview: FC<PrismicPreviewProps> = (props) => {
|
|
|
127
127
|
return (
|
|
128
128
|
<>
|
|
129
129
|
{children}
|
|
130
|
-
<Script
|
|
130
|
+
<Script
|
|
131
|
+
src={toolbarSrc}
|
|
132
|
+
strategy="lazyOnload"
|
|
133
|
+
data-preview-url={router.basePath + updatePreviewURL}
|
|
134
|
+
/>
|
|
131
135
|
</>
|
|
132
136
|
)
|
|
133
137
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { NextApiResponseLike } from "./types"
|
|
2
|
+
|
|
3
|
+
/** Adds a cookie like the App Router's preview cookie, keeping Next.js's preview data cookies. */
|
|
4
|
+
export function appendCookie(res: NextApiResponseLike, cookie: string): void {
|
|
5
|
+
const previous = res.getHeader("Set-Cookie") ?? []
|
|
6
|
+
res.setHeader("Set-Cookie", [
|
|
7
|
+
...[previous].flat().map(String),
|
|
8
|
+
`${cookie}; Path=/; SameSite=None; Secure`,
|
|
9
|
+
])
|
|
10
|
+
}
|
package/src/pages/exitPreview.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { cookie as prismicCookie } from "@prismicio/client"
|
|
2
|
+
|
|
3
|
+
import { appendCookie } from "./appendCookie"
|
|
1
4
|
import type { NextApiRequestLike, NextApiResponseLike } from "./types"
|
|
2
5
|
|
|
3
6
|
/** Configuration for `exitPreview()`. */
|
|
@@ -34,6 +37,9 @@ export type ExitPreviewAPIRouteConfig = {
|
|
|
34
37
|
*/
|
|
35
38
|
export function exitPreview(config: ExitPreviewAPIRouteConfig): void {
|
|
36
39
|
config.res.clearPreviewData()
|
|
40
|
+
for (const name of [prismicCookie.preview, prismicCookie.previewSession]) {
|
|
41
|
+
appendCookie(config.res, `${name}=; Expires=${new Date(0).toUTCString()}`)
|
|
42
|
+
}
|
|
37
43
|
|
|
38
44
|
// `Cache-Control` header is used to prevent CDN-level caching.
|
|
39
45
|
config.res.setHeader("Cache-Control", "no-store")
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { cookie as prismicCookie, type LinkResolverFunction, type Client } from "@prismicio/client"
|
|
2
2
|
|
|
3
|
+
import { getSafePath } from "../lib/getSafePath"
|
|
4
|
+
import { appendCookie } from "./appendCookie"
|
|
3
5
|
import type { NextApiRequestLike, NextApiResponseLike } from "./types"
|
|
4
6
|
|
|
5
7
|
export type RedirectToPreviewURLConfig = {
|
|
@@ -54,6 +56,24 @@ export async function redirectToPreviewURL(config: RedirectToPreviewURLConfig):
|
|
|
54
56
|
|
|
55
57
|
const previewToken = req.query.token?.toString()
|
|
56
58
|
|
|
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
|
+
const session = req.query.session?.toString()
|
|
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.
|
|
70
|
+
const path = getSafePath(req.query.path?.toString())
|
|
71
|
+
if (path) {
|
|
72
|
+
res.redirect(path)
|
|
73
|
+
|
|
74
|
+
return
|
|
75
|
+
}
|
|
76
|
+
|
|
57
77
|
const previewURL = await client.resolvePreviewURL({
|
|
58
78
|
linkResolver,
|
|
59
79
|
defaultURL,
|
package/src/pages/types.ts
CHANGED
|
@@ -24,6 +24,7 @@ export type NextApiRequestLike = {
|
|
|
24
24
|
export type NextApiResponseLike = {
|
|
25
25
|
redirect(url: string): NextApiResponseLike
|
|
26
26
|
clearPreviewData(): NextApiResponseLike
|
|
27
|
+
getHeader(name: string): number | string | string[] | undefined
|
|
27
28
|
setHeader(name: string, value: string | string[]): NextApiResponseLike
|
|
28
29
|
// oxlint-disable-next-line no-explicit-any explicit-module-boundary-types
|
|
29
30
|
json(body: any): void
|
|
@@ -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 = {
|
|
@@ -56,12 +57,21 @@ export async function redirectToPreviewURL(config: RedirectToPreviewURLConfig):
|
|
|
56
57
|
cookieJar.set(prismicCookie.preview, previewToken, { sameSite: "none", secure: true })
|
|
57
58
|
}
|
|
58
59
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
60
|
+
// The toolbar's v5 flow follows this session.
|
|
61
|
+
const session = request.nextUrl.searchParams.get("session")
|
|
62
|
+
if (session) {
|
|
63
|
+
cookieJar.set(prismicCookie.previewSession, session, { sameSite: "none", secure: true })
|
|
64
|
+
}
|
|
65
|
+
|
|
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
|
+
}))
|
|
65
75
|
|
|
66
76
|
;(await draftMode()).enable()
|
|
67
77
|
|