@prismicio/next 2.3.4-pr.175.5f9614e → 2.3.4-pr.175.9bb2125
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 +19 -5
- package/dist/PrismicPreview.cjs.map +1 -1
- package/dist/PrismicPreview.d.cts.map +1 -1
- package/dist/PrismicPreview.d.ts.map +1 -1
- package/dist/PrismicPreview.js +17 -5
- package/dist/PrismicPreview.js.map +1 -1
- package/dist/PrismicPreviewClient.cjs +1 -11
- package/dist/PrismicPreviewClient.cjs.map +1 -1
- package/dist/PrismicPreviewClient.js +1 -9
- package/dist/PrismicPreviewClient.js.map +1 -1
- package/dist/exitPreview.cjs +1 -2
- package/dist/exitPreview.cjs.map +1 -1
- package/dist/exitPreview.d.cts.map +1 -1
- package/dist/exitPreview.d.ts.map +1 -1
- package/dist/exitPreview.js +1 -2
- package/dist/exitPreview.js.map +1 -1
- package/dist/package.cjs +1 -1
- package/dist/package.js +1 -1
- package/dist/pages/PrismicPreview.cjs +9 -4
- package/dist/pages/PrismicPreview.cjs.map +1 -1
- package/dist/pages/PrismicPreview.d.cts.map +1 -1
- package/dist/pages/PrismicPreview.d.ts.map +1 -1
- package/dist/pages/PrismicPreview.js +10 -5
- package/dist/pages/PrismicPreview.js.map +1 -1
- package/dist/pages/appendCookie.cjs +2 -4
- package/dist/pages/appendCookie.cjs.map +1 -1
- package/dist/pages/appendCookie.js +2 -4
- package/dist/pages/appendCookie.js.map +1 -1
- package/dist/pages/exitPreview.cjs +2 -2
- package/dist/pages/exitPreview.cjs.map +1 -1
- package/dist/pages/exitPreview.d.cts.map +1 -1
- package/dist/pages/exitPreview.d.ts.map +1 -1
- package/dist/pages/exitPreview.js +2 -2
- package/dist/pages/exitPreview.js.map +1 -1
- package/dist/pages/redirectToPreviewURL.cjs +2 -15
- package/dist/pages/redirectToPreviewURL.cjs.map +1 -1
- package/dist/pages/redirectToPreviewURL.d.cts +1 -1
- package/dist/pages/redirectToPreviewURL.d.cts.map +1 -1
- package/dist/pages/redirectToPreviewURL.d.ts +1 -1
- package/dist/pages/redirectToPreviewURL.d.ts.map +1 -1
- package/dist/pages/redirectToPreviewURL.js +2 -15
- package/dist/pages/redirectToPreviewURL.js.map +1 -1
- package/dist/redirectToPreviewURL.cjs +8 -13
- package/dist/redirectToPreviewURL.cjs.map +1 -1
- package/dist/redirectToPreviewURL.d.cts +1 -1
- package/dist/redirectToPreviewURL.d.cts.map +1 -1
- package/dist/redirectToPreviewURL.d.ts +1 -1
- package/dist/redirectToPreviewURL.d.ts.map +1 -1
- package/dist/redirectToPreviewURL.js +8 -13
- package/dist/redirectToPreviewURL.js.map +1 -1
- package/package.json +5 -2
- package/src/PrismicPreview.tsx +8 -0
- package/src/PrismicPreviewClient.tsx +1 -12
- package/src/exitPreview.ts +1 -3
- package/src/pages/PrismicPreview.tsx +16 -9
- package/src/pages/appendCookie.ts +5 -11
- package/src/pages/exitPreview.ts +5 -2
- package/src/pages/redirectToPreviewURL.ts +4 -18
- package/src/redirectToPreviewURL.ts +16 -16
- package/dist/lib/toolbarV5.cjs +0 -37
- package/dist/lib/toolbarV5.cjs.map +0 -1
- package/dist/lib/toolbarV5.js +0 -34
- package/dist/lib/toolbarV5.js.map +0 -1
- package/src/lib/toolbarV5.ts +0 -54
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { previewSessionCookie } from "../lib/toolbarV5.js";
|
|
2
1
|
import { appendCookie } from "./appendCookie.js";
|
|
2
|
+
import { cookie } from "@prismicio/client";
|
|
3
3
|
//#region src/pages/exitPreview.ts
|
|
4
4
|
/**
|
|
5
5
|
* Ends a Prismic preview session within a Next.js app. This function should be used in an API
|
|
@@ -18,7 +18,7 @@ import { appendCookie } from "./appendCookie.js";
|
|
|
18
18
|
*/
|
|
19
19
|
function exitPreview(config) {
|
|
20
20
|
config.res.clearPreviewData();
|
|
21
|
-
appendCookie(config.res,
|
|
21
|
+
for (const name of [cookie.preview, cookie.previewSession]) appendCookie(config.res, `${name}=; Expires=${(/* @__PURE__ */ new Date(0)).toUTCString()}`);
|
|
22
22
|
config.res.setHeader("Cache-Control", "no-store");
|
|
23
23
|
config.res.json({ success: true });
|
|
24
24
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exitPreview.js","names":[],"sources":["../../src/pages/exitPreview.ts"],"sourcesContent":["import {
|
|
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,24 +1,11 @@
|
|
|
1
|
-
const require_toolbarV5 = require("../lib/toolbarV5.cjs");
|
|
2
1
|
const require_appendCookie = require("./appendCookie.cjs");
|
|
3
2
|
let _prismicio_client = require("@prismicio/client");
|
|
4
3
|
//#region src/pages/redirectToPreviewURL.ts
|
|
5
4
|
async function redirectToPreviewURL(config) {
|
|
6
5
|
const { client, req, res, linkResolver, defaultURL = "/", basePath = "" } = config;
|
|
7
|
-
|
|
6
|
+
const previewToken = req.query.token?.toString();
|
|
8
7
|
const session = req.query.session?.toString();
|
|
9
|
-
if (session) {
|
|
10
|
-
if (!previewToken) {
|
|
11
|
-
previewToken = await require_toolbarV5.getPreviewSessionRef(client, session);
|
|
12
|
-
res.setPreviewData({ ref: previewToken });
|
|
13
|
-
}
|
|
14
|
-
require_appendCookie.appendCookie(res, _prismicio_client.cookie.preview, previewToken);
|
|
15
|
-
require_appendCookie.appendCookie(res, require_toolbarV5.previewSessionCookie, session);
|
|
16
|
-
const path = require_toolbarV5.getSafePath(req.query.path?.toString());
|
|
17
|
-
if (path) {
|
|
18
|
-
res.redirect(path);
|
|
19
|
-
return;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
8
|
+
if (session) require_appendCookie.appendCookie(res, `${_prismicio_client.cookie.previewSession}=${encodeURIComponent(session)}`);
|
|
22
9
|
const previewURL = await client.resolvePreviewURL({
|
|
23
10
|
linkResolver,
|
|
24
11
|
defaultURL,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redirectToPreviewURL.cjs","names":["
|
|
1
|
+
{"version":3,"file":"redirectToPreviewURL.cjs","names":["prismicCookie"],"sources":["../../src/pages/redirectToPreviewURL.ts"],"sourcesContent":["import { cookie as prismicCookie, type LinkResolverFunction, type Client } from \"@prismicio/client\"\n\nimport { appendCookie } from \"./appendCookie\"\nimport type { NextApiRequestLike, NextApiResponseLike } from \"./types\"\n\nexport type RedirectToPreviewURLConfig = {\n\t/** The Prismic client configured for the preview session's repository. */\n\t// `Pick` is used to use the smallest possible subset of\n\t// `prismic.Client`. Doing this reduces the surface area for breaking\n\t// type changes.\n\tclient: Pick<Client, \"resolvePreviewURL\">\n\n\t/**\n\t * The `req` object from a Next.js API route.\n\t *\n\t * @see Next.js API route docs: \\<https://nextjs.org/docs/pages/building-your-application/routing/api-routes\\>\n\t */\n\treq: NextApiRequestLike\n\n\t/**\n\t * The `res` object from a Next.js API route.\n\t *\n\t * @see Next.js API route docs: \\<https://nextjs.org/docs/pages/building-your-application/routing/api-routes\\>\n\t */\n\tres: NextApiResponseLike\n\n\t/**\n\t * A Link Resolver used to resolve the previewed document's URL.\n\t *\n\t * @see To learn more about Link Resolver: {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver}\n\t */\n\tlinkResolver?: LinkResolverFunction\n\n\t/**\n\t * The default redirect URL if a URL cannot be determined for the previewed document.\n\t *\n\t * **Note**: If you `next.config.js` file contains a `basePath`, the `defaultURL` option must\n\t * _not_ include it. Instead, provide the `basePath` property using the `basePath` option.\n\t */\n\tdefaultURL?: string\n\n\t/**\n\t * The `basePath` for the Next.js app as it is defined in `next.config.js`. This option can be\n\t * omitted if the app does not have a `basePath`.\n\t *\n\t * @remarks\n\t * The Router Handler or API route is unable to detect the app's `basePath` automatically. It\n\t * must be provided to `redirectToPreviewURL()` manually.\n\t */\n\tbasePath?: string\n}\n\nexport async function redirectToPreviewURL(config: RedirectToPreviewURLConfig): Promise<void> {\n\tconst { client, req, res, linkResolver, defaultURL = \"/\", basePath = \"\" } = config\n\n\tconst previewToken = req.query.token?.toString()\n\n\t// The toolbar's v5 flow follows this session.\n\tconst session = req.query.session?.toString()\n\tif (session) {\n\t\tappendCookie(res, `${prismicCookie.previewSession}=${encodeURIComponent(session)}`)\n\t}\n\n\tconst previewURL = await client.resolvePreviewURL({\n\t\tlinkResolver,\n\t\tdefaultURL,\n\t\tpreviewToken,\n\t})\n\n\tres.redirect(basePath + previewURL)\n\n\treturn\n}\n"],"mappings":";;;AAoDA,eAAsB,qBAAqB,QAAmD;CAC7F,MAAM,EAAE,QAAQ,KAAK,KAAK,cAAc,aAAa,KAAK,WAAW,OAAO;CAE5E,MAAM,eAAe,IAAI,MAAM,OAAO,SAAS;CAG/C,MAAM,UAAU,IAAI,MAAM,SAAS,SAAS;CAC5C,IAAI,SACH,qBAAA,aAAa,KAAK,GAAGA,kBAAAA,OAAc,eAAe,GAAG,mBAAmB,OAAO,GAAG;CAGnF,MAAM,aAAa,MAAM,OAAO,kBAAkB;EACjD;EACA;EACA;CACD,CAAC;CAED,IAAI,SAAS,WAAW,UAAU;AAGnC"}
|
|
@@ -3,7 +3,7 @@ import { Client, LinkResolverFunction } from "@prismicio/client";
|
|
|
3
3
|
//#region src/pages/redirectToPreviewURL.d.ts
|
|
4
4
|
export type RedirectToPreviewURLConfig = {
|
|
5
5
|
/** The Prismic client configured for the preview session's repository. */
|
|
6
|
-
client: Pick<Client, "resolvePreviewURL"
|
|
6
|
+
client: Pick<Client, "resolvePreviewURL">;
|
|
7
7
|
/**
|
|
8
8
|
* The `req` object from a Next.js API route.
|
|
9
9
|
*
|
|
@@ -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":";;;YAKY;;EAKX,QAAQ,KAAK;;;;;;EAOb,KAAK;;;;;;EAOL,KAAK;;;;;;EAOL,eAAe;;;;;;;EAQf;;;;;;;;;EAUA;;wBAGqB,qBAAqB,QAAQ,6BAA6B"}
|
|
@@ -3,7 +3,7 @@ import { Client, LinkResolverFunction } from "@prismicio/client";
|
|
|
3
3
|
//#region src/pages/redirectToPreviewURL.d.ts
|
|
4
4
|
export type RedirectToPreviewURLConfig = {
|
|
5
5
|
/** The Prismic client configured for the preview session's repository. */
|
|
6
|
-
client: Pick<Client, "resolvePreviewURL"
|
|
6
|
+
client: Pick<Client, "resolvePreviewURL">;
|
|
7
7
|
/**
|
|
8
8
|
* The `req` object from a Next.js API route.
|
|
9
9
|
*
|
|
@@ -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":";;;YAKY;;EAKX,QAAQ,KAAK;;;;;;EAOb,KAAK;;;;;;EAOL,KAAK;;;;;;EAOL,eAAe;;;;;;;EAQf;;;;;;;;;EAUA;;wBAGqB,qBAAqB,QAAQ,6BAA6B"}
|
|
@@ -1,24 +1,11 @@
|
|
|
1
|
-
import { getPreviewSessionRef, getSafePath, previewSessionCookie } from "../lib/toolbarV5.js";
|
|
2
1
|
import { appendCookie } from "./appendCookie.js";
|
|
3
2
|
import { cookie } from "@prismicio/client";
|
|
4
3
|
//#region src/pages/redirectToPreviewURL.ts
|
|
5
4
|
async function redirectToPreviewURL(config) {
|
|
6
5
|
const { client, req, res, linkResolver, defaultURL = "/", basePath = "" } = config;
|
|
7
|
-
|
|
6
|
+
const previewToken = req.query.token?.toString();
|
|
8
7
|
const session = req.query.session?.toString();
|
|
9
|
-
if (session) {
|
|
10
|
-
if (!previewToken) {
|
|
11
|
-
previewToken = await getPreviewSessionRef(client, session);
|
|
12
|
-
res.setPreviewData({ ref: previewToken });
|
|
13
|
-
}
|
|
14
|
-
appendCookie(res, cookie.preview, previewToken);
|
|
15
|
-
appendCookie(res, previewSessionCookie, session);
|
|
16
|
-
const path = getSafePath(req.query.path?.toString());
|
|
17
|
-
if (path) {
|
|
18
|
-
res.redirect(path);
|
|
19
|
-
return;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
8
|
+
if (session) appendCookie(res, `${cookie.previewSession}=${encodeURIComponent(session)}`);
|
|
22
9
|
const previewURL = await client.resolvePreviewURL({
|
|
23
10
|
linkResolver,
|
|
24
11
|
defaultURL,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redirectToPreviewURL.js","names":["prismicCookie"],"sources":["../../src/pages/redirectToPreviewURL.ts"],"sourcesContent":["import { cookie as prismicCookie, type LinkResolverFunction, type Client } from \"@prismicio/client\"\n\nimport {
|
|
1
|
+
{"version":3,"file":"redirectToPreviewURL.js","names":["prismicCookie"],"sources":["../../src/pages/redirectToPreviewURL.ts"],"sourcesContent":["import { cookie as prismicCookie, type LinkResolverFunction, type Client } from \"@prismicio/client\"\n\nimport { appendCookie } from \"./appendCookie\"\nimport type { NextApiRequestLike, NextApiResponseLike } from \"./types\"\n\nexport type RedirectToPreviewURLConfig = {\n\t/** The Prismic client configured for the preview session's repository. */\n\t// `Pick` is used to use the smallest possible subset of\n\t// `prismic.Client`. Doing this reduces the surface area for breaking\n\t// type changes.\n\tclient: Pick<Client, \"resolvePreviewURL\">\n\n\t/**\n\t * The `req` object from a Next.js API route.\n\t *\n\t * @see Next.js API route docs: \\<https://nextjs.org/docs/pages/building-your-application/routing/api-routes\\>\n\t */\n\treq: NextApiRequestLike\n\n\t/**\n\t * The `res` object from a Next.js API route.\n\t *\n\t * @see Next.js API route docs: \\<https://nextjs.org/docs/pages/building-your-application/routing/api-routes\\>\n\t */\n\tres: NextApiResponseLike\n\n\t/**\n\t * A Link Resolver used to resolve the previewed document's URL.\n\t *\n\t * @see To learn more about Link Resolver: {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver}\n\t */\n\tlinkResolver?: LinkResolverFunction\n\n\t/**\n\t * The default redirect URL if a URL cannot be determined for the previewed document.\n\t *\n\t * **Note**: If you `next.config.js` file contains a `basePath`, the `defaultURL` option must\n\t * _not_ include it. Instead, provide the `basePath` property using the `basePath` option.\n\t */\n\tdefaultURL?: string\n\n\t/**\n\t * The `basePath` for the Next.js app as it is defined in `next.config.js`. This option can be\n\t * omitted if the app does not have a `basePath`.\n\t *\n\t * @remarks\n\t * The Router Handler or API route is unable to detect the app's `basePath` automatically. It\n\t * must be provided to `redirectToPreviewURL()` manually.\n\t */\n\tbasePath?: string\n}\n\nexport async function redirectToPreviewURL(config: RedirectToPreviewURLConfig): Promise<void> {\n\tconst { client, req, res, linkResolver, defaultURL = \"/\", basePath = \"\" } = config\n\n\tconst previewToken = req.query.token?.toString()\n\n\t// The toolbar's v5 flow follows this session.\n\tconst session = req.query.session?.toString()\n\tif (session) {\n\t\tappendCookie(res, `${prismicCookie.previewSession}=${encodeURIComponent(session)}`)\n\t}\n\n\tconst previewURL = await client.resolvePreviewURL({\n\t\tlinkResolver,\n\t\tdefaultURL,\n\t\tpreviewToken,\n\t})\n\n\tres.redirect(basePath + previewURL)\n\n\treturn\n}\n"],"mappings":";;;AAoDA,eAAsB,qBAAqB,QAAmD;CAC7F,MAAM,EAAE,QAAQ,KAAK,KAAK,cAAc,aAAa,KAAK,WAAW,OAAO;CAE5E,MAAM,eAAe,IAAI,MAAM,OAAO,SAAS;CAG/C,MAAM,UAAU,IAAI,MAAM,SAAS,SAAS;CAC5C,IAAI,SACH,aAAa,KAAK,GAAGA,OAAc,eAAe,GAAG,mBAAmB,OAAO,GAAG;CAGnF,MAAM,aAAa,MAAM,OAAO,kBAAkB;EACjD;EACA;EACA;CACD,CAAC;CAED,IAAI,SAAS,WAAW,UAAU;AAGnC"}
|
|
@@ -1,27 +1,22 @@
|
|
|
1
|
-
const require_toolbarV5 = require("./lib/toolbarV5.cjs");
|
|
2
1
|
let _prismicio_client = require("@prismicio/client");
|
|
3
2
|
//#region src/redirectToPreviewURL.ts
|
|
4
3
|
async function redirectToPreviewURL(config) {
|
|
5
4
|
const { client, request, linkResolver, defaultURL = "/" } = config;
|
|
6
5
|
const { cookies, draftMode } = await import("next/headers.js");
|
|
7
6
|
const { redirect } = await import("next/navigation");
|
|
8
|
-
const
|
|
9
|
-
const documentID = searchParams.get("documentId") ?? void 0;
|
|
10
|
-
let previewToken = searchParams.get("token") ?? void 0;
|
|
7
|
+
const documentID = request.nextUrl.searchParams.get("documentId") ?? void 0;
|
|
11
8
|
const cookieJar = await cookies();
|
|
12
|
-
const
|
|
13
|
-
if (session) {
|
|
14
|
-
previewToken ??= await require_toolbarV5.getPreviewSessionRef(client, session);
|
|
15
|
-
cookieJar.set(require_toolbarV5.previewSessionCookie, session, {
|
|
16
|
-
sameSite: "none",
|
|
17
|
-
secure: true
|
|
18
|
-
});
|
|
19
|
-
}
|
|
9
|
+
const previewToken = request.nextUrl.searchParams.get("token") ?? void 0;
|
|
20
10
|
if (previewToken) cookieJar.set(_prismicio_client.cookie.preview, previewToken, {
|
|
21
11
|
sameSite: "none",
|
|
22
12
|
secure: true
|
|
23
13
|
});
|
|
24
|
-
const
|
|
14
|
+
const session = request.nextUrl.searchParams.get("session");
|
|
15
|
+
if (session) cookieJar.set(_prismicio_client.cookie.previewSession, session, {
|
|
16
|
+
sameSite: "none",
|
|
17
|
+
secure: true
|
|
18
|
+
});
|
|
19
|
+
const previewURL = await client.resolvePreviewURL({
|
|
25
20
|
documentID,
|
|
26
21
|
previewToken,
|
|
27
22
|
defaultURL,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redirectToPreviewURL.cjs","names":["
|
|
1
|
+
{"version":3,"file":"redirectToPreviewURL.cjs","names":["prismicCookie"],"sources":["../src/redirectToPreviewURL.ts"],"sourcesContent":["import { cookie as prismicCookie, type Client, type LinkResolverFunction } from \"@prismicio/client\"\n\nimport type { NextRequestLike } from \"./types\"\n\nexport type RedirectToPreviewURLConfig = {\n\t/** The Prismic client configured for the preview session's repository. */\n\t// `Pick` is used to use the smallest possible subset of\n\t// `prismic.Client`. Doing this reduces the surface area for breaking\n\t// type changes.\n\tclient: Pick<Client, \"resolvePreviewURL\">\n\n\t/**\n\t * The `request` object from a Next.js Route Handler.\n\t *\n\t * @see Next.js Route Handler docs: \\<https://nextjs.org/docs/app/building-your-application/routing/route-handlers\\>\n\t */\n\trequest: NextRequestLike\n\n\t/**\n\t * A Link Resolver used to resolve the previewed document's URL.\n\t *\n\t * @see To learn more about Link Resolver: {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver}\n\t */\n\tlinkResolver?: LinkResolverFunction\n\n\t/**\n\t * The default redirect URL if a URL cannot be determined for the previewed document.\n\t *\n\t * **Note**: If you `next.config.js` file contains a `basePath`, the `defaultURL` option must\n\t * _not_ include it. Instead, provide the `basePath` property using the `basePath` option.\n\t */\n\tdefaultURL?: string\n}\n\nexport async function redirectToPreviewURL(config: RedirectToPreviewURLConfig): Promise<never> {\n\tconst { client, request, linkResolver, defaultURL = \"/\" } = config\n\n\t// Need this to avoid the following Next.js build-time error:\n\t// You're importing a component that needs next/headers. That only works\n\t// in a Server Component which is not supported in the pages/ directory.\n\tconst { cookies, draftMode } = await import(\"next/headers\")\n\n\t// Dynamic so Node.js can load this module: the bare specifier (kept by\n\t// tsdown.config.ts for Next.js's server alias) is not resolvable by Node.js.\n\tconst { redirect } = await import(\"next/navigation\")\n\n\tconst documentID = request.nextUrl.searchParams.get(\"documentId\") ?? undefined\n\n\t// Set the initial preview cookie. Setting the cookie here is necessary\n\t// to support unpublished previews. Without setting it here, the page\n\t// will try to render without the preview cookie, leading to a\n\t// PrismicNotFound error.\n\tconst cookieJar = await cookies()\n\tconst previewToken = request.nextUrl.searchParams.get(\"token\") ?? undefined\n\tif (previewToken) {\n\t\tcookieJar.set(prismicCookie.preview, previewToken, { sameSite: \"none\", secure: true })\n\t}\n\n\t// The toolbar's v5 flow follows this session.\n\tconst session = request.nextUrl.searchParams.get(\"session\")\n\tif (session) {\n\t\tcookieJar.set(prismicCookie.previewSession, session, { sameSite: \"none\", secure: true })\n\t}\n\n\tconst previewURL = await client.resolvePreviewURL({\n\t\tdocumentID,\n\t\tpreviewToken,\n\t\tdefaultURL,\n\t\tlinkResolver,\n\t})\n\n\t;(await draftMode()).enable()\n\n\t// Next.js uses `SameSite=Lax` in development, which cross-site iframes drop.\n\tcookieJar.set({\n\t\t...cookieJar.get(\"__prerender_bypass\")!,\n\t\thttpOnly: true,\n\t\tsameSite: \"none\",\n\t\tsecure: true,\n\t})\n\n\treturn redirect(previewURL)\n}\n"],"mappings":";;AAkCA,eAAsB,qBAAqB,QAAoD;CAC9F,MAAM,EAAE,QAAQ,SAAS,cAAc,aAAa,QAAQ;CAK5D,MAAM,EAAE,SAAS,cAAc,MAAM,OAAO;CAI5C,MAAM,EAAE,aAAa,MAAM,OAAO;CAElC,MAAM,aAAa,QAAQ,QAAQ,aAAa,IAAI,YAAY,KAAK,KAAA;CAMrE,MAAM,YAAY,MAAM,QAAQ;CAChC,MAAM,eAAe,QAAQ,QAAQ,aAAa,IAAI,OAAO,KAAK,KAAA;CAClE,IAAI,cACH,UAAU,IAAIA,kBAAAA,OAAc,SAAS,cAAc;EAAE,UAAU;EAAQ,QAAQ;CAAK,CAAC;CAItF,MAAM,UAAU,QAAQ,QAAQ,aAAa,IAAI,SAAS;CAC1D,IAAI,SACH,UAAU,IAAIA,kBAAAA,OAAc,gBAAgB,SAAS;EAAE,UAAU;EAAQ,QAAQ;CAAK,CAAC;CAGxF,MAAM,aAAa,MAAM,OAAO,kBAAkB;EACjD;EACA;EACA;EACA;CACD,CAAC;CAEA,CAAC,MAAM,UAAU,EAAA,CAAG,OAAO;CAG5B,UAAU,IAAI;EACb,GAAG,UAAU,IAAI,oBAAoB;EACrC,UAAU;EACV,UAAU;EACV,QAAQ;CACT,CAAC;CAED,OAAO,SAAS,UAAU;AAC3B"}
|
|
@@ -3,7 +3,7 @@ import { Client, LinkResolverFunction } from "@prismicio/client";
|
|
|
3
3
|
//#region src/redirectToPreviewURL.d.ts
|
|
4
4
|
export type RedirectToPreviewURLConfig = {
|
|
5
5
|
/** The Prismic client configured for the preview session's repository. */
|
|
6
|
-
client: Pick<Client, "resolvePreviewURL"
|
|
6
|
+
client: Pick<Client, "resolvePreviewURL">;
|
|
7
7
|
/**
|
|
8
8
|
* The `request` object from a Next.js Route Handler.
|
|
9
9
|
*
|
|
@@ -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":";;;YAIY;;EAKX,QAAQ,KAAK;;;;;;EAOb,SAAS;;;;;;EAOT,eAAe;;;;;;;EAQf;;wBAGqB,qBAAqB,QAAQ,6BAA6B"}
|
|
@@ -3,7 +3,7 @@ import { Client, LinkResolverFunction } from "@prismicio/client";
|
|
|
3
3
|
//#region src/redirectToPreviewURL.d.ts
|
|
4
4
|
export type RedirectToPreviewURLConfig = {
|
|
5
5
|
/** The Prismic client configured for the preview session's repository. */
|
|
6
|
-
client: Pick<Client, "resolvePreviewURL"
|
|
6
|
+
client: Pick<Client, "resolvePreviewURL">;
|
|
7
7
|
/**
|
|
8
8
|
* The `request` object from a Next.js Route Handler.
|
|
9
9
|
*
|
|
@@ -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":";;;YAIY;;EAKX,QAAQ,KAAK;;;;;;EAOb,SAAS;;;;;;EAOT,eAAe;;;;;;;EAQf;;wBAGqB,qBAAqB,QAAQ,6BAA6B"}
|
|
@@ -1,27 +1,22 @@
|
|
|
1
|
-
import { getPreviewSessionRef, getSafePath, previewSessionCookie } from "./lib/toolbarV5.js";
|
|
2
1
|
import { cookie } from "@prismicio/client";
|
|
3
2
|
//#region src/redirectToPreviewURL.ts
|
|
4
3
|
async function redirectToPreviewURL(config) {
|
|
5
4
|
const { client, request, linkResolver, defaultURL = "/" } = config;
|
|
6
5
|
const { cookies, draftMode } = await import("next/headers.js");
|
|
7
6
|
const { redirect } = await import("next/navigation");
|
|
8
|
-
const
|
|
9
|
-
const documentID = searchParams.get("documentId") ?? void 0;
|
|
10
|
-
let previewToken = searchParams.get("token") ?? void 0;
|
|
7
|
+
const documentID = request.nextUrl.searchParams.get("documentId") ?? void 0;
|
|
11
8
|
const cookieJar = await cookies();
|
|
12
|
-
const
|
|
13
|
-
if (session) {
|
|
14
|
-
previewToken ??= await getPreviewSessionRef(client, session);
|
|
15
|
-
cookieJar.set(previewSessionCookie, session, {
|
|
16
|
-
sameSite: "none",
|
|
17
|
-
secure: true
|
|
18
|
-
});
|
|
19
|
-
}
|
|
9
|
+
const previewToken = request.nextUrl.searchParams.get("token") ?? void 0;
|
|
20
10
|
if (previewToken) cookieJar.set(cookie.preview, previewToken, {
|
|
21
11
|
sameSite: "none",
|
|
22
12
|
secure: true
|
|
23
13
|
});
|
|
24
|
-
const
|
|
14
|
+
const session = request.nextUrl.searchParams.get("session");
|
|
15
|
+
if (session) cookieJar.set(cookie.previewSession, session, {
|
|
16
|
+
sameSite: "none",
|
|
17
|
+
secure: true
|
|
18
|
+
});
|
|
19
|
+
const previewURL = await client.resolvePreviewURL({
|
|
25
20
|
documentID,
|
|
26
21
|
previewToken,
|
|
27
22
|
defaultURL,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redirectToPreviewURL.js","names":["prismicCookie"],"sources":["../src/redirectToPreviewURL.ts"],"sourcesContent":["import { cookie as prismicCookie, type Client, type LinkResolverFunction } from \"@prismicio/client\"\n\nimport
|
|
1
|
+
{"version":3,"file":"redirectToPreviewURL.js","names":["prismicCookie"],"sources":["../src/redirectToPreviewURL.ts"],"sourcesContent":["import { cookie as prismicCookie, type Client, type LinkResolverFunction } from \"@prismicio/client\"\n\nimport type { NextRequestLike } from \"./types\"\n\nexport type RedirectToPreviewURLConfig = {\n\t/** The Prismic client configured for the preview session's repository. */\n\t// `Pick` is used to use the smallest possible subset of\n\t// `prismic.Client`. Doing this reduces the surface area for breaking\n\t// type changes.\n\tclient: Pick<Client, \"resolvePreviewURL\">\n\n\t/**\n\t * The `request` object from a Next.js Route Handler.\n\t *\n\t * @see Next.js Route Handler docs: \\<https://nextjs.org/docs/app/building-your-application/routing/route-handlers\\>\n\t */\n\trequest: NextRequestLike\n\n\t/**\n\t * A Link Resolver used to resolve the previewed document's URL.\n\t *\n\t * @see To learn more about Link Resolver: {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver}\n\t */\n\tlinkResolver?: LinkResolverFunction\n\n\t/**\n\t * The default redirect URL if a URL cannot be determined for the previewed document.\n\t *\n\t * **Note**: If you `next.config.js` file contains a `basePath`, the `defaultURL` option must\n\t * _not_ include it. Instead, provide the `basePath` property using the `basePath` option.\n\t */\n\tdefaultURL?: string\n}\n\nexport async function redirectToPreviewURL(config: RedirectToPreviewURLConfig): Promise<never> {\n\tconst { client, request, linkResolver, defaultURL = \"/\" } = config\n\n\t// Need this to avoid the following Next.js build-time error:\n\t// You're importing a component that needs next/headers. That only works\n\t// in a Server Component which is not supported in the pages/ directory.\n\tconst { cookies, draftMode } = await import(\"next/headers\")\n\n\t// Dynamic so Node.js can load this module: the bare specifier (kept by\n\t// tsdown.config.ts for Next.js's server alias) is not resolvable by Node.js.\n\tconst { redirect } = await import(\"next/navigation\")\n\n\tconst documentID = request.nextUrl.searchParams.get(\"documentId\") ?? undefined\n\n\t// Set the initial preview cookie. Setting the cookie here is necessary\n\t// to support unpublished previews. Without setting it here, the page\n\t// will try to render without the preview cookie, leading to a\n\t// PrismicNotFound error.\n\tconst cookieJar = await cookies()\n\tconst previewToken = request.nextUrl.searchParams.get(\"token\") ?? undefined\n\tif (previewToken) {\n\t\tcookieJar.set(prismicCookie.preview, previewToken, { sameSite: \"none\", secure: true })\n\t}\n\n\t// The toolbar's v5 flow follows this session.\n\tconst session = request.nextUrl.searchParams.get(\"session\")\n\tif (session) {\n\t\tcookieJar.set(prismicCookie.previewSession, session, { sameSite: \"none\", secure: true })\n\t}\n\n\tconst previewURL = await client.resolvePreviewURL({\n\t\tdocumentID,\n\t\tpreviewToken,\n\t\tdefaultURL,\n\t\tlinkResolver,\n\t})\n\n\t;(await draftMode()).enable()\n\n\t// Next.js uses `SameSite=Lax` in development, which cross-site iframes drop.\n\tcookieJar.set({\n\t\t...cookieJar.get(\"__prerender_bypass\")!,\n\t\thttpOnly: true,\n\t\tsameSite: \"none\",\n\t\tsecure: true,\n\t})\n\n\treturn redirect(previewURL)\n}\n"],"mappings":";;AAkCA,eAAsB,qBAAqB,QAAoD;CAC9F,MAAM,EAAE,QAAQ,SAAS,cAAc,aAAa,QAAQ;CAK5D,MAAM,EAAE,SAAS,cAAc,MAAM,OAAO;CAI5C,MAAM,EAAE,aAAa,MAAM,OAAO;CAElC,MAAM,aAAa,QAAQ,QAAQ,aAAa,IAAI,YAAY,KAAK,KAAA;CAMrE,MAAM,YAAY,MAAM,QAAQ;CAChC,MAAM,eAAe,QAAQ,QAAQ,aAAa,IAAI,OAAO,KAAK,KAAA;CAClE,IAAI,cACH,UAAU,IAAIA,OAAc,SAAS,cAAc;EAAE,UAAU;EAAQ,QAAQ;CAAK,CAAC;CAItF,MAAM,UAAU,QAAQ,QAAQ,aAAa,IAAI,SAAS;CAC1D,IAAI,SACH,UAAU,IAAIA,OAAc,gBAAgB,SAAS;EAAE,UAAU;EAAQ,QAAQ;CAAK,CAAC;CAGxF,MAAM,aAAa,MAAM,OAAO,kBAAkB;EACjD;EACA;EACA;EACA;CACD,CAAC;CAEA,CAAC,MAAM,UAAU,EAAA,CAAG,OAAO;CAG5B,UAAU,IAAI;EACb,GAAG,UAAU,IAAI,oBAAoB;EACrC,UAAU;EACV,UAAU;EACV,QAAQ;CACT,CAAC;CAED,OAAO,SAAS,UAAU;AAC3B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prismicio/next",
|
|
3
|
-
"version": "2.3.4-pr.175.
|
|
3
|
+
"version": "2.3.4-pr.175.9bb2125",
|
|
4
4
|
"description": "Helpers to integrate Prismic into Next.js apps",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"next",
|
|
@@ -61,7 +61,7 @@
|
|
|
61
61
|
},
|
|
62
62
|
"devDependencies": {
|
|
63
63
|
"@playwright/test": "^1.64.1",
|
|
64
|
-
"@prismicio/client": "
|
|
64
|
+
"@prismicio/client": "7.22.3-pr.477.5e5ad22",
|
|
65
65
|
"@types/node": "^26.6.5",
|
|
66
66
|
"@types/react": "^19.3.0",
|
|
67
67
|
"next": "16.4.2",
|
|
@@ -73,6 +73,9 @@
|
|
|
73
73
|
"tsdown": "^0.23.1",
|
|
74
74
|
"typescript": "^7.0.2"
|
|
75
75
|
},
|
|
76
|
+
"overrides": {
|
|
77
|
+
"@prismicio/client": "$@prismicio/client"
|
|
78
|
+
},
|
|
76
79
|
"peerDependencies": {
|
|
77
80
|
"@prismicio/client": "^7",
|
|
78
81
|
"next": "^13.4.5 || ^14 || ^15 || ^16",
|
package/src/PrismicPreview.tsx
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
|
+
import { getToolbarSrc } from "@prismicio/client"
|
|
2
|
+
import Script from "next/script"
|
|
1
3
|
import type { FC, ReactNode } from "react"
|
|
2
4
|
|
|
5
|
+
import { resolveDefaultExport } from "./lib/resolveDefaultExport"
|
|
3
6
|
import { PrismicPreviewClient } from "./PrismicPreviewClient"
|
|
4
7
|
|
|
8
|
+
const ResolvedScript = resolveDefaultExport(Script)
|
|
9
|
+
|
|
5
10
|
/** Props for `<PrismicPreview>`. */
|
|
6
11
|
export type PrismicPreviewProps = {
|
|
7
12
|
/**
|
|
@@ -42,6 +47,8 @@ export const PrismicPreview: FC<PrismicPreviewProps> = async (props) => {
|
|
|
42
47
|
// in a Server Component which is not supported in the pages/ directory.
|
|
43
48
|
const { draftMode } = await import("next/headers")
|
|
44
49
|
|
|
50
|
+
// TODO: Bump the `@prismicio/client` peer to `^7.23.0`, which adds `version`, and drop `overrides`.
|
|
51
|
+
const toolbarSrc = getToolbarSrc(repositoryName, { version: 5 })
|
|
45
52
|
const isDraftMode = (await draftMode()).isEnabled
|
|
46
53
|
|
|
47
54
|
return (
|
|
@@ -52,6 +59,7 @@ export const PrismicPreview: FC<PrismicPreviewProps> = async (props) => {
|
|
|
52
59
|
isDraftMode={isDraftMode}
|
|
53
60
|
{...otherProps}
|
|
54
61
|
/>
|
|
62
|
+
<ResolvedScript src={toolbarSrc} strategy="lazyOnload" />
|
|
55
63
|
</>
|
|
56
64
|
)
|
|
57
65
|
}
|
|
@@ -2,15 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
import { cookie as prismicCookie } from "@prismicio/client"
|
|
4
4
|
import { useRouter } from "next/navigation"
|
|
5
|
-
import Script from "next/script"
|
|
6
5
|
import type { FC } from "react"
|
|
7
6
|
import { useEffect } from "react"
|
|
8
7
|
|
|
9
|
-
import { resolveDefaultExport } from "./lib/resolveDefaultExport"
|
|
10
|
-
import { getToolbarScriptProps } from "./lib/toolbarV5"
|
|
11
|
-
|
|
12
|
-
const ResolvedScript = resolveDefaultExport(Script)
|
|
13
|
-
|
|
14
8
|
type PrismicPreviewClientProps = {
|
|
15
9
|
repositoryName: string
|
|
16
10
|
isDraftMode: boolean
|
|
@@ -116,12 +110,7 @@ export const PrismicPreviewClient: FC<PrismicPreviewClientProps> = (props) => {
|
|
|
116
110
|
return () => controller.abort()
|
|
117
111
|
}, [repositoryName, isDraftMode, updatePreviewURL, exitPreviewURL, refresh])
|
|
118
112
|
|
|
119
|
-
return
|
|
120
|
-
<ResolvedScript
|
|
121
|
-
{...getToolbarScriptProps(repositoryName, updatePreviewURL)}
|
|
122
|
-
strategy="lazyOnload"
|
|
123
|
-
/>
|
|
124
|
-
)
|
|
113
|
+
return null
|
|
125
114
|
}
|
|
126
115
|
|
|
127
116
|
/**
|
package/src/exitPreview.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { cookie as prismicCookie } from "@prismicio/client"
|
|
2
2
|
|
|
3
|
-
import { previewSessionCookie } from "./lib/toolbarV5"
|
|
4
|
-
|
|
5
3
|
/**
|
|
6
4
|
* Ends a Prismic preview session within a Next.js app. This function should be used in a Router
|
|
7
5
|
* Handler.
|
|
@@ -26,7 +24,7 @@ export async function exitPreview(): Promise<Response> {
|
|
|
26
24
|
const cookieJar = await cookies()
|
|
27
25
|
cookieJar.delete({ name: "__prerender_bypass", sameSite: "none", secure: true })
|
|
28
26
|
cookieJar.delete({ name: prismicCookie.preview, sameSite: "none", secure: true })
|
|
29
|
-
cookieJar.delete({ name:
|
|
27
|
+
cookieJar.delete({ name: prismicCookie.previewSession, sameSite: "none", secure: true })
|
|
30
28
|
|
|
31
29
|
// `Cache-Control` header is used to prevent CDN-level caching.
|
|
32
30
|
return new Response(JSON.stringify({ success: true }), {
|
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
import { cookie as prismicCookie } from "@prismicio/client"
|
|
1
|
+
import { getToolbarSrc, cookie as prismicCookie } from "@prismicio/client"
|
|
2
2
|
import { useRouter } from "next/router"
|
|
3
3
|
import Script from "next/script"
|
|
4
4
|
import type { FC } from "react"
|
|
5
5
|
import { type ReactNode, useEffect } from "react"
|
|
6
6
|
|
|
7
|
-
import { getToolbarScriptProps } from "../lib/toolbarV5"
|
|
8
|
-
|
|
9
7
|
/** Props for `<PrismicPreview>`. */
|
|
10
8
|
export type PrismicPreviewProps = {
|
|
11
9
|
/**
|
|
@@ -48,9 +46,14 @@ export const PrismicPreview: FC<PrismicPreviewProps> = (props) => {
|
|
|
48
46
|
|
|
49
47
|
const router = useRouter()
|
|
50
48
|
|
|
49
|
+
const toolbarSrc = getToolbarSrc(repositoryName, { version: 5 })
|
|
50
|
+
|
|
51
51
|
useEffect(() => {
|
|
52
52
|
const controller = new AbortController()
|
|
53
53
|
|
|
54
|
+
window.addEventListener("prismicPreviewStart", onStart, {
|
|
55
|
+
signal: controller.signal,
|
|
56
|
+
})
|
|
54
57
|
window.addEventListener("prismicPreviewUpdate", onUpdate, {
|
|
55
58
|
signal: controller.signal,
|
|
56
59
|
})
|
|
@@ -89,12 +92,19 @@ export const PrismicPreview: FC<PrismicPreviewProps> = (props) => {
|
|
|
89
92
|
.catch(() => {})
|
|
90
93
|
}
|
|
91
94
|
|
|
95
|
+
function onStart(event: Event) {
|
|
96
|
+
event.preventDefault()
|
|
97
|
+
// `router.replace()` would restore the share link's hash and, as a
|
|
98
|
+
// hash-only change, skip `getServerSideProps`.
|
|
99
|
+
start(() => window.location.reload())
|
|
100
|
+
}
|
|
101
|
+
|
|
92
102
|
function onUpdate(event: Event) {
|
|
93
103
|
event.preventDefault()
|
|
94
104
|
start()
|
|
95
105
|
}
|
|
96
106
|
|
|
97
|
-
function start() {
|
|
107
|
+
function start(onStarted = refresh) {
|
|
98
108
|
// We check `opaqueredirect` because we don't care if
|
|
99
109
|
// the redirect was successful or not. As long as it
|
|
100
110
|
// redirects, we know the endpoint exists and at least
|
|
@@ -112,7 +122,7 @@ export const PrismicPreview: FC<PrismicPreviewProps> = (props) => {
|
|
|
112
122
|
return
|
|
113
123
|
}
|
|
114
124
|
|
|
115
|
-
|
|
125
|
+
onStarted()
|
|
116
126
|
})
|
|
117
127
|
.catch(() => {})
|
|
118
128
|
}
|
|
@@ -127,10 +137,7 @@ export const PrismicPreview: FC<PrismicPreviewProps> = (props) => {
|
|
|
127
137
|
return (
|
|
128
138
|
<>
|
|
129
139
|
{children}
|
|
130
|
-
<Script
|
|
131
|
-
{...getToolbarScriptProps(repositoryName, router.basePath + updatePreviewURL)}
|
|
132
|
-
strategy="lazyOnload"
|
|
133
|
-
/>
|
|
140
|
+
<Script src={toolbarSrc} strategy="lazyOnload" />
|
|
134
141
|
</>
|
|
135
142
|
)
|
|
136
143
|
}
|
|
@@ -1,16 +1,10 @@
|
|
|
1
1
|
import type { NextApiResponseLike } from "./types"
|
|
2
2
|
|
|
3
3
|
/** Adds a cookie like the App Router's preview cookie, keeping Next.js's preview data cookies. */
|
|
4
|
-
export function appendCookie(
|
|
5
|
-
res: NextApiResponseLike,
|
|
6
|
-
name: string,
|
|
7
|
-
value: string,
|
|
8
|
-
expires?: Date,
|
|
9
|
-
): void {
|
|
4
|
+
export function appendCookie(res: NextApiResponseLike, cookie: string): void {
|
|
10
5
|
const previous = res.getHeader("Set-Cookie") ?? []
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
cookie
|
|
14
|
-
|
|
15
|
-
res.setHeader("Set-Cookie", [...[previous].flat().map(String), cookie])
|
|
6
|
+
res.setHeader("Set-Cookie", [
|
|
7
|
+
...[previous].flat().map(String),
|
|
8
|
+
`${cookie}; Path=/; SameSite=None; Secure`,
|
|
9
|
+
])
|
|
16
10
|
}
|
package/src/pages/exitPreview.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { cookie as prismicCookie } from "@prismicio/client"
|
|
2
|
+
|
|
2
3
|
import { appendCookie } from "./appendCookie"
|
|
3
4
|
import type { NextApiRequestLike, NextApiResponseLike } from "./types"
|
|
4
5
|
|
|
@@ -36,7 +37,9 @@ export type ExitPreviewAPIRouteConfig = {
|
|
|
36
37
|
*/
|
|
37
38
|
export function exitPreview(config: ExitPreviewAPIRouteConfig): void {
|
|
38
39
|
config.res.clearPreviewData()
|
|
39
|
-
|
|
40
|
+
for (const name of [prismicCookie.preview, prismicCookie.previewSession]) {
|
|
41
|
+
appendCookie(config.res, `${name}=; Expires=${new Date(0).toUTCString()}`)
|
|
42
|
+
}
|
|
40
43
|
|
|
41
44
|
// `Cache-Control` header is used to prevent CDN-level caching.
|
|
42
45
|
config.res.setHeader("Cache-Control", "no-store")
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { cookie as prismicCookie, type LinkResolverFunction, type Client } from "@prismicio/client"
|
|
2
2
|
|
|
3
|
-
import { getPreviewSessionRef, getSafePath, previewSessionCookie } from "../lib/toolbarV5"
|
|
4
3
|
import { appendCookie } from "./appendCookie"
|
|
5
4
|
import type { NextApiRequestLike, NextApiResponseLike } from "./types"
|
|
6
5
|
|
|
@@ -9,7 +8,7 @@ export type RedirectToPreviewURLConfig = {
|
|
|
9
8
|
// `Pick` is used to use the smallest possible subset of
|
|
10
9
|
// `prismic.Client`. Doing this reduces the surface area for breaking
|
|
11
10
|
// type changes.
|
|
12
|
-
client: Pick<Client, "resolvePreviewURL"
|
|
11
|
+
client: Pick<Client, "resolvePreviewURL">
|
|
13
12
|
|
|
14
13
|
/**
|
|
15
14
|
* The `req` object from a Next.js API route.
|
|
@@ -54,25 +53,12 @@ export type RedirectToPreviewURLConfig = {
|
|
|
54
53
|
export async function redirectToPreviewURL(config: RedirectToPreviewURLConfig): Promise<void> {
|
|
55
54
|
const { client, req, res, linkResolver, defaultURL = "/", basePath = "" } = config
|
|
56
55
|
|
|
57
|
-
|
|
56
|
+
const previewToken = req.query.token?.toString()
|
|
58
57
|
|
|
59
|
-
// The toolbar's v5 flow follows
|
|
60
|
-
// links only carry the session, so `setPreviewData()` has no ref for them.
|
|
58
|
+
// The toolbar's v5 flow follows this session.
|
|
61
59
|
const session = req.query.session?.toString()
|
|
62
60
|
if (session) {
|
|
63
|
-
|
|
64
|
-
previewToken = await getPreviewSessionRef(client, session)
|
|
65
|
-
res.setPreviewData({ ref: previewToken })
|
|
66
|
-
}
|
|
67
|
-
appendCookie(res, prismicCookie.preview, previewToken)
|
|
68
|
-
appendCookie(res, previewSessionCookie, session)
|
|
69
|
-
|
|
70
|
-
const path = getSafePath(req.query.path?.toString())
|
|
71
|
-
if (path) {
|
|
72
|
-
res.redirect(path)
|
|
73
|
-
|
|
74
|
-
return
|
|
75
|
-
}
|
|
61
|
+
appendCookie(res, `${prismicCookie.previewSession}=${encodeURIComponent(session)}`)
|
|
76
62
|
}
|
|
77
63
|
|
|
78
64
|
const previewURL = await client.resolvePreviewURL({
|