@prismicio/next 2.3.4-pr.175.5f9614e → 2.3.4-pr.176.2b93539
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 +0 -6
- 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 +0 -6
- package/dist/exitPreview.js.map +1 -1
- package/dist/package.cjs +1 -1
- package/dist/package.js +1 -1
- package/dist/pages/PrismicPreview.cjs +2 -2
- 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 +3 -3
- package/dist/pages/PrismicPreview.js.map +1 -1
- package/dist/pages/exitPreview.cjs +0 -3
- 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 +0 -3
- package/dist/pages/exitPreview.js.map +1 -1
- package/dist/pages/redirectToPreviewURL.cjs +1 -18
- 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 +1 -18
- package/dist/pages/redirectToPreviewURL.js.map +1 -1
- package/dist/pages/types.d.cts +0 -1
- package/dist/pages/types.d.cts.map +1 -1
- package/dist/pages/types.d.ts +0 -1
- package/dist/pages/types.d.ts.map +1 -1
- package/dist/redirectToPreviewURL.cjs +3 -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 +3 -13
- package/dist/redirectToPreviewURL.js.map +1 -1
- package/package.json +1 -1
- package/src/PrismicPreview.tsx +7 -0
- package/src/PrismicPreviewClient.tsx +1 -12
- package/src/exitPreview.ts +0 -3
- package/src/pages/PrismicPreview.tsx +4 -7
- package/src/pages/exitPreview.ts +0 -3
- package/src/pages/redirectToPreviewURL.ts +3 -24
- package/src/pages/types.ts +0 -1
- package/src/redirectToPreviewURL.ts +10 -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/dist/pages/appendCookie.cjs +0 -12
- package/dist/pages/appendCookie.cjs.map +0 -1
- package/dist/pages/appendCookie.js +0 -12
- package/dist/pages/appendCookie.js.map +0 -1
- package/src/lib/toolbarV5.ts +0 -54
- package/src/pages/appendCookie.ts +0 -16
|
@@ -1,24 +1,7 @@
|
|
|
1
|
-
import { getPreviewSessionRef, getSafePath, previewSessionCookie } from "../lib/toolbarV5.js";
|
|
2
|
-
import { appendCookie } from "./appendCookie.js";
|
|
3
|
-
import { cookie } from "@prismicio/client";
|
|
4
1
|
//#region src/pages/redirectToPreviewURL.ts
|
|
5
2
|
async function redirectToPreviewURL(config) {
|
|
6
3
|
const { client, req, res, linkResolver, defaultURL = "/", basePath = "" } = config;
|
|
7
|
-
|
|
8
|
-
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
|
-
}
|
|
4
|
+
const previewToken = req.query.token?.toString();
|
|
22
5
|
const previewURL = await client.resolvePreviewURL({
|
|
23
6
|
linkResolver,
|
|
24
7
|
defaultURL,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redirectToPreviewURL.js","names":[
|
|
1
|
+
{"version":3,"file":"redirectToPreviewURL.js","names":[],"sources":["../../src/pages/redirectToPreviewURL.ts"],"sourcesContent":["import type { LinkResolverFunction, Client } from \"@prismicio/client\"\n\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\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":";AAmDA,eAAsB,qBAAqB,QAAmD;CAC7F,MAAM,EAAE,QAAQ,KAAK,KAAK,cAAc,aAAa,KAAK,WAAW,OAAO;CAE5E,MAAM,eAAe,IAAI,MAAM,OAAO,SAAS;CAE/C,MAAM,aAAa,MAAM,OAAO,kBAAkB;EACjD;EACA;EACA;CACD,CAAC;CAED,IAAI,SAAS,WAAW,UAAU;AAGnC"}
|
package/dist/pages/types.d.cts
CHANGED
|
@@ -20,7 +20,6 @@ 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;
|
|
24
23
|
setHeader(name: string, value: string | string[]): NextApiResponseLike;
|
|
25
24
|
json(body: any): void;
|
|
26
25
|
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
|
|
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"}
|
package/dist/pages/types.d.ts
CHANGED
|
@@ -20,7 +20,6 @@ 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;
|
|
24
23
|
setHeader(name: string, value: string | string[]): NextApiResponseLike;
|
|
25
24
|
json(body: any): void;
|
|
26
25
|
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
|
|
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,27 +1,17 @@
|
|
|
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 previewURL =
|
|
14
|
+
const previewURL = await client.resolvePreviewURL({
|
|
25
15
|
documentID,
|
|
26
16
|
previewToken,
|
|
27
17
|
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\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;CAGtF,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,17 @@
|
|
|
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 previewURL =
|
|
14
|
+
const previewURL = await client.resolvePreviewURL({
|
|
25
15
|
documentID,
|
|
26
16
|
previewToken,
|
|
27
17
|
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\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;CAGtF,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
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,7 @@ 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
|
+
const toolbarSrc = getToolbarSrc(repositoryName)
|
|
45
51
|
const isDraftMode = (await draftMode()).isEnabled
|
|
46
52
|
|
|
47
53
|
return (
|
|
@@ -52,6 +58,7 @@ export const PrismicPreview: FC<PrismicPreviewProps> = async (props) => {
|
|
|
52
58
|
isDraftMode={isDraftMode}
|
|
53
59
|
{...otherProps}
|
|
54
60
|
/>
|
|
61
|
+
<ResolvedScript src={toolbarSrc} strategy="lazyOnload" />
|
|
55
62
|
</>
|
|
56
63
|
)
|
|
57
64
|
}
|
|
@@ -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,6 @@ 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: previewSessionCookie, sameSite: "none", secure: true })
|
|
30
27
|
|
|
31
28
|
// `Cache-Control` header is used to prevent CDN-level caching.
|
|
32
29
|
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,6 +46,8 @@ export const PrismicPreview: FC<PrismicPreviewProps> = (props) => {
|
|
|
48
46
|
|
|
49
47
|
const router = useRouter()
|
|
50
48
|
|
|
49
|
+
const toolbarSrc = getToolbarSrc(repositoryName)
|
|
50
|
+
|
|
51
51
|
useEffect(() => {
|
|
52
52
|
const controller = new AbortController()
|
|
53
53
|
|
|
@@ -127,10 +127,7 @@ export const PrismicPreview: FC<PrismicPreviewProps> = (props) => {
|
|
|
127
127
|
return (
|
|
128
128
|
<>
|
|
129
129
|
{children}
|
|
130
|
-
<Script
|
|
131
|
-
{...getToolbarScriptProps(repositoryName, router.basePath + updatePreviewURL)}
|
|
132
|
-
strategy="lazyOnload"
|
|
133
|
-
/>
|
|
130
|
+
<Script src={toolbarSrc} strategy="lazyOnload" />
|
|
134
131
|
</>
|
|
135
132
|
)
|
|
136
133
|
}
|
package/src/pages/exitPreview.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { previewSessionCookie } from "../lib/toolbarV5"
|
|
2
|
-
import { appendCookie } from "./appendCookie"
|
|
3
1
|
import type { NextApiRequestLike, NextApiResponseLike } from "./types"
|
|
4
2
|
|
|
5
3
|
/** Configuration for `exitPreview()`. */
|
|
@@ -36,7 +34,6 @@ export type ExitPreviewAPIRouteConfig = {
|
|
|
36
34
|
*/
|
|
37
35
|
export function exitPreview(config: ExitPreviewAPIRouteConfig): void {
|
|
38
36
|
config.res.clearPreviewData()
|
|
39
|
-
appendCookie(config.res, previewSessionCookie, "", new Date(0))
|
|
40
37
|
|
|
41
38
|
// `Cache-Control` header is used to prevent CDN-level caching.
|
|
42
39
|
config.res.setHeader("Cache-Control", "no-store")
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { LinkResolverFunction, Client } from "@prismicio/client"
|
|
2
2
|
|
|
3
|
-
import { getPreviewSessionRef, getSafePath, previewSessionCookie } from "../lib/toolbarV5"
|
|
4
|
-
import { appendCookie } from "./appendCookie"
|
|
5
3
|
import type { NextApiRequestLike, NextApiResponseLike } from "./types"
|
|
6
4
|
|
|
7
5
|
export type RedirectToPreviewURLConfig = {
|
|
@@ -9,7 +7,7 @@ export type RedirectToPreviewURLConfig = {
|
|
|
9
7
|
// `Pick` is used to use the smallest possible subset of
|
|
10
8
|
// `prismic.Client`. Doing this reduces the surface area for breaking
|
|
11
9
|
// type changes.
|
|
12
|
-
client: Pick<Client, "resolvePreviewURL"
|
|
10
|
+
client: Pick<Client, "resolvePreviewURL">
|
|
13
11
|
|
|
14
12
|
/**
|
|
15
13
|
* The `req` object from a Next.js API route.
|
|
@@ -54,26 +52,7 @@ export type RedirectToPreviewURLConfig = {
|
|
|
54
52
|
export async function redirectToPreviewURL(config: RedirectToPreviewURLConfig): Promise<void> {
|
|
55
53
|
const { client, req, res, linkResolver, defaultURL = "/", basePath = "" } = config
|
|
56
54
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
// The toolbar's v5 flow follows the session in the preview cookies. Share
|
|
60
|
-
// links only carry the session, so `setPreviewData()` has no ref for them.
|
|
61
|
-
const session = req.query.session?.toString()
|
|
62
|
-
if (session) {
|
|
63
|
-
if (!previewToken) {
|
|
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
|
-
}
|
|
76
|
-
}
|
|
55
|
+
const previewToken = req.query.token?.toString()
|
|
77
56
|
|
|
78
57
|
const previewURL = await client.resolvePreviewURL({
|
|
79
58
|
linkResolver,
|
package/src/pages/types.ts
CHANGED
|
@@ -24,7 +24,6 @@ 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
|
|
28
27
|
setHeader(name: string, value: string | string[]): NextApiResponseLike
|
|
29
28
|
// oxlint-disable-next-line no-explicit-any explicit-module-boundary-types
|
|
30
29
|
json(body: any): void
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { cookie as prismicCookie, type Client, type LinkResolverFunction } from "@prismicio/client"
|
|
2
2
|
|
|
3
|
-
import { getPreviewSessionRef, getSafePath, previewSessionCookie } from "./lib/toolbarV5"
|
|
4
3
|
import type { NextRequestLike } from "./types"
|
|
5
4
|
|
|
6
5
|
export type RedirectToPreviewURLConfig = {
|
|
@@ -8,7 +7,7 @@ export type RedirectToPreviewURLConfig = {
|
|
|
8
7
|
// `Pick` is used to use the smallest possible subset of
|
|
9
8
|
// `prismic.Client`. Doing this reduces the surface area for breaking
|
|
10
9
|
// type changes.
|
|
11
|
-
client: Pick<Client, "resolvePreviewURL"
|
|
10
|
+
client: Pick<Client, "resolvePreviewURL">
|
|
12
11
|
|
|
13
12
|
/**
|
|
14
13
|
* The `request` object from a Next.js Route Handler.
|
|
@@ -45,29 +44,24 @@ export async function redirectToPreviewURL(config: RedirectToPreviewURLConfig):
|
|
|
45
44
|
// tsdown.config.ts for Next.js's server alias) is not resolvable by Node.js.
|
|
46
45
|
const { redirect } = await import("next/navigation")
|
|
47
46
|
|
|
48
|
-
const
|
|
49
|
-
const documentID = searchParams.get("documentId") ?? undefined
|
|
50
|
-
let previewToken = searchParams.get("token") ?? undefined
|
|
51
|
-
const cookieJar = await cookies()
|
|
52
|
-
|
|
53
|
-
// The toolbar's v5 flow follows the session. Share links only carry the session.
|
|
54
|
-
const session = searchParams.get("session")
|
|
55
|
-
if (session) {
|
|
56
|
-
previewToken ??= await getPreviewSessionRef(client, session)
|
|
57
|
-
cookieJar.set(previewSessionCookie, session, { sameSite: "none", secure: true })
|
|
58
|
-
}
|
|
47
|
+
const documentID = request.nextUrl.searchParams.get("documentId") ?? undefined
|
|
59
48
|
|
|
60
49
|
// Set the initial preview cookie. Setting the cookie here is necessary
|
|
61
50
|
// to support unpublished previews. Without setting it here, the page
|
|
62
51
|
// will try to render without the preview cookie, leading to a
|
|
63
52
|
// PrismicNotFound error.
|
|
53
|
+
const cookieJar = await cookies()
|
|
54
|
+
const previewToken = request.nextUrl.searchParams.get("token") ?? undefined
|
|
64
55
|
if (previewToken) {
|
|
65
56
|
cookieJar.set(prismicCookie.preview, previewToken, { sameSite: "none", secure: true })
|
|
66
57
|
}
|
|
67
58
|
|
|
68
|
-
const previewURL =
|
|
69
|
-
|
|
70
|
-
|
|
59
|
+
const previewURL = await client.resolvePreviewURL({
|
|
60
|
+
documentID,
|
|
61
|
+
previewToken,
|
|
62
|
+
defaultURL,
|
|
63
|
+
linkResolver,
|
|
64
|
+
})
|
|
71
65
|
|
|
72
66
|
;(await draftMode()).enable()
|
|
73
67
|
|
package/dist/lib/toolbarV5.cjs
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
let _prismicio_client = require("@prismicio/client");
|
|
2
|
-
//#region src/lib/toolbarV5.ts
|
|
3
|
-
/** The cookie storing the preview session ID. The toolbar's v5 flow follows this session. */
|
|
4
|
-
const previewSessionCookie = "io.prismic.previewSession";
|
|
5
|
-
/**
|
|
6
|
-
* Props for the toolbar's `<script>`, opting into its v5 flow.
|
|
7
|
-
*
|
|
8
|
-
* @param previewURL - The app's preview route, as passed to `fetch()`.
|
|
9
|
-
*/
|
|
10
|
-
function getToolbarScriptProps(repositoryName, previewURL) {
|
|
11
|
-
return {
|
|
12
|
-
src: `${(0, _prismicio_client.getToolbarSrc)(repositoryName)}&v=5`,
|
|
13
|
-
"data-preview-url": typeof window === "undefined" ? void 0 : new URL(previewURL, document.baseURI).href
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
/** Fetches the current ref of a preview session, for share links that only carry the session. */
|
|
17
|
-
async function getPreviewSessionRef(client, session) {
|
|
18
|
-
const url = new URL(`/previews/token/${encodeURIComponent(session)}`, client.documentAPIEndpoint);
|
|
19
|
-
url.hostname = url.hostname.replace(".cdn.", ".");
|
|
20
|
-
const json = await (await fetch(url, { cache: "no-store" })).json();
|
|
21
|
-
if (!json.ref) throw new _prismicio_client.PreviewTokenExpiredError("This preview session has expired", url.href, json);
|
|
22
|
-
return json.ref;
|
|
23
|
-
}
|
|
24
|
-
/** Returns `path` if it is a path on the same origin, like `/blog?page=2`. */
|
|
25
|
-
function getSafePath(path) {
|
|
26
|
-
if (!path?.startsWith("/") || path.startsWith("//") || path.startsWith("/\\")) return;
|
|
27
|
-
const origin = "https://placeholder.invalid";
|
|
28
|
-
const url = new URL(path, origin);
|
|
29
|
-
if (url.origin === origin) return url.href.slice(27);
|
|
30
|
-
}
|
|
31
|
-
//#endregion
|
|
32
|
-
exports.getPreviewSessionRef = getPreviewSessionRef;
|
|
33
|
-
exports.getSafePath = getSafePath;
|
|
34
|
-
exports.getToolbarScriptProps = getToolbarScriptProps;
|
|
35
|
-
exports.previewSessionCookie = previewSessionCookie;
|
|
36
|
-
|
|
37
|
-
//# sourceMappingURL=toolbarV5.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"toolbarV5.cjs","names":["getToolbarSrc","PreviewTokenExpiredError"],"sources":["../../src/lib/toolbarV5.ts"],"sourcesContent":["import { getToolbarSrc, PreviewTokenExpiredError, type Client } from \"@prismicio/client\"\n\n/** The cookie storing the preview session ID. The toolbar's v5 flow follows this session. */\nexport const previewSessionCookie = \"io.prismic.previewSession\"\n\n/**\n * Props for the toolbar's `<script>`, opting into its v5 flow.\n *\n * @param previewURL - The app's preview route, as passed to `fetch()`.\n */\nexport function getToolbarScriptProps(\n\trepositoryName: string,\n\tpreviewURL: string,\n): { src: string; \"data-preview-url\"?: string } {\n\treturn {\n\t\t// TODO: Use `getToolbarSrc()` once `@prismicio/client` adds `v=5`.\n\t\tsrc: `${getToolbarSrc(repositoryName)}&v=5`,\n\t\t// The toolbar builds share links with this URL. `next/script` adds `lazyOnload` scripts in\n\t\t// the browser, where the URL is known.\n\t\t\"data-preview-url\":\n\t\t\ttypeof window === \"undefined\" ? undefined : new URL(previewURL, document.baseURI).href,\n\t}\n}\n\n/** Fetches the current ref of a preview session, for share links that only carry the session. */\nexport async function getPreviewSessionRef(\n\tclient: Pick<Client, \"documentAPIEndpoint\">,\n\tsession: string,\n): Promise<string> {\n\tconst url = new URL(`/previews/token/${encodeURIComponent(session)}`, client.documentAPIEndpoint)\n\t// The endpoint is on the repository host, not on its CDN.\n\turl.hostname = url.hostname.replace(\".cdn.\", \".\")\n\tconst res = await fetch(url, { cache: \"no-store\" })\n\tconst json = await res.json()\n\tif (!json.ref) {\n\t\t// Like the Content API's response to an expired preview token.\n\t\tthrow new PreviewTokenExpiredError(\"This preview session has expired\", url.href, json)\n\t}\n\n\treturn json.ref\n}\n\n/** Returns `path` if it is a path on the same origin, like `/blog?page=2`. */\nexport function getSafePath(path: string | undefined | null): string | undefined {\n\tif (!path?.startsWith(\"/\") || path.startsWith(\"//\") || path.startsWith(\"/\\\\\")) {\n\t\treturn\n\t}\n\n\tconst origin = \"https://placeholder.invalid\"\n\tconst url = new URL(path, origin)\n\tif (url.origin === origin) {\n\t\treturn url.href.slice(origin.length)\n\t}\n}\n"],"mappings":";;;AAGA,MAAa,uBAAuB;;;;;;AAOpC,SAAgB,sBACf,gBACA,YAC+C;CAC/C,OAAO;EAEN,KAAK,IAAA,GAAGA,kBAAAA,cAAAA,CAAc,cAAc,EAAE;EAGtC,oBACC,OAAO,WAAW,cAAc,KAAA,IAAY,IAAI,IAAI,YAAY,SAAS,OAAO,CAAC,CAAC;CACpF;AACD;;AAGA,eAAsB,qBACrB,QACA,SACkB;CAClB,MAAM,MAAM,IAAI,IAAI,mBAAmB,mBAAmB,OAAO,KAAK,OAAO,mBAAmB;CAEhG,IAAI,WAAW,IAAI,SAAS,QAAQ,SAAS,GAAG;CAEhD,MAAM,OAAO,OAAM,MADD,MAAM,KAAK,EAAE,OAAO,WAAW,CAAC,EAAA,CAC3B,KAAK;CAC5B,IAAI,CAAC,KAAK,KAET,MAAM,IAAIC,kBAAAA,yBAAyB,oCAAoC,IAAI,MAAM,IAAI;CAGtF,OAAO,KAAK;AACb;;AAGA,SAAgB,YAAY,MAAqD;CAChF,IAAI,CAAC,MAAM,WAAW,GAAG,KAAK,KAAK,WAAW,IAAI,KAAK,KAAK,WAAW,KAAK,GAC3E;CAGD,MAAM,SAAS;CACf,MAAM,MAAM,IAAI,IAAI,MAAM,MAAM;CAChC,IAAI,IAAI,WAAW,QAClB,OAAO,IAAI,KAAK,MAAM,EAAa;AAErC"}
|
package/dist/lib/toolbarV5.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { PreviewTokenExpiredError, getToolbarSrc } from "@prismicio/client";
|
|
2
|
-
//#region src/lib/toolbarV5.ts
|
|
3
|
-
/** The cookie storing the preview session ID. The toolbar's v5 flow follows this session. */
|
|
4
|
-
const previewSessionCookie = "io.prismic.previewSession";
|
|
5
|
-
/**
|
|
6
|
-
* Props for the toolbar's `<script>`, opting into its v5 flow.
|
|
7
|
-
*
|
|
8
|
-
* @param previewURL - The app's preview route, as passed to `fetch()`.
|
|
9
|
-
*/
|
|
10
|
-
function getToolbarScriptProps(repositoryName, previewURL) {
|
|
11
|
-
return {
|
|
12
|
-
src: `${getToolbarSrc(repositoryName)}&v=5`,
|
|
13
|
-
"data-preview-url": typeof window === "undefined" ? void 0 : new URL(previewURL, document.baseURI).href
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
/** Fetches the current ref of a preview session, for share links that only carry the session. */
|
|
17
|
-
async function getPreviewSessionRef(client, session) {
|
|
18
|
-
const url = new URL(`/previews/token/${encodeURIComponent(session)}`, client.documentAPIEndpoint);
|
|
19
|
-
url.hostname = url.hostname.replace(".cdn.", ".");
|
|
20
|
-
const json = await (await fetch(url, { cache: "no-store" })).json();
|
|
21
|
-
if (!json.ref) throw new PreviewTokenExpiredError("This preview session has expired", url.href, json);
|
|
22
|
-
return json.ref;
|
|
23
|
-
}
|
|
24
|
-
/** Returns `path` if it is a path on the same origin, like `/blog?page=2`. */
|
|
25
|
-
function getSafePath(path) {
|
|
26
|
-
if (!path?.startsWith("/") || path.startsWith("//") || path.startsWith("/\\")) return;
|
|
27
|
-
const origin = "https://placeholder.invalid";
|
|
28
|
-
const url = new URL(path, origin);
|
|
29
|
-
if (url.origin === origin) return url.href.slice(27);
|
|
30
|
-
}
|
|
31
|
-
//#endregion
|
|
32
|
-
export { getPreviewSessionRef, getSafePath, getToolbarScriptProps, previewSessionCookie };
|
|
33
|
-
|
|
34
|
-
//# sourceMappingURL=toolbarV5.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"toolbarV5.js","names":[],"sources":["../../src/lib/toolbarV5.ts"],"sourcesContent":["import { getToolbarSrc, PreviewTokenExpiredError, type Client } from \"@prismicio/client\"\n\n/** The cookie storing the preview session ID. The toolbar's v5 flow follows this session. */\nexport const previewSessionCookie = \"io.prismic.previewSession\"\n\n/**\n * Props for the toolbar's `<script>`, opting into its v5 flow.\n *\n * @param previewURL - The app's preview route, as passed to `fetch()`.\n */\nexport function getToolbarScriptProps(\n\trepositoryName: string,\n\tpreviewURL: string,\n): { src: string; \"data-preview-url\"?: string } {\n\treturn {\n\t\t// TODO: Use `getToolbarSrc()` once `@prismicio/client` adds `v=5`.\n\t\tsrc: `${getToolbarSrc(repositoryName)}&v=5`,\n\t\t// The toolbar builds share links with this URL. `next/script` adds `lazyOnload` scripts in\n\t\t// the browser, where the URL is known.\n\t\t\"data-preview-url\":\n\t\t\ttypeof window === \"undefined\" ? undefined : new URL(previewURL, document.baseURI).href,\n\t}\n}\n\n/** Fetches the current ref of a preview session, for share links that only carry the session. */\nexport async function getPreviewSessionRef(\n\tclient: Pick<Client, \"documentAPIEndpoint\">,\n\tsession: string,\n): Promise<string> {\n\tconst url = new URL(`/previews/token/${encodeURIComponent(session)}`, client.documentAPIEndpoint)\n\t// The endpoint is on the repository host, not on its CDN.\n\turl.hostname = url.hostname.replace(\".cdn.\", \".\")\n\tconst res = await fetch(url, { cache: \"no-store\" })\n\tconst json = await res.json()\n\tif (!json.ref) {\n\t\t// Like the Content API's response to an expired preview token.\n\t\tthrow new PreviewTokenExpiredError(\"This preview session has expired\", url.href, json)\n\t}\n\n\treturn json.ref\n}\n\n/** Returns `path` if it is a path on the same origin, like `/blog?page=2`. */\nexport function getSafePath(path: string | undefined | null): string | undefined {\n\tif (!path?.startsWith(\"/\") || path.startsWith(\"//\") || path.startsWith(\"/\\\\\")) {\n\t\treturn\n\t}\n\n\tconst origin = \"https://placeholder.invalid\"\n\tconst url = new URL(path, origin)\n\tif (url.origin === origin) {\n\t\treturn url.href.slice(origin.length)\n\t}\n}\n"],"mappings":";;;AAGA,MAAa,uBAAuB;;;;;;AAOpC,SAAgB,sBACf,gBACA,YAC+C;CAC/C,OAAO;EAEN,KAAK,GAAG,cAAc,cAAc,EAAE;EAGtC,oBACC,OAAO,WAAW,cAAc,KAAA,IAAY,IAAI,IAAI,YAAY,SAAS,OAAO,CAAC,CAAC;CACpF;AACD;;AAGA,eAAsB,qBACrB,QACA,SACkB;CAClB,MAAM,MAAM,IAAI,IAAI,mBAAmB,mBAAmB,OAAO,KAAK,OAAO,mBAAmB;CAEhG,IAAI,WAAW,IAAI,SAAS,QAAQ,SAAS,GAAG;CAEhD,MAAM,OAAO,OAAM,MADD,MAAM,KAAK,EAAE,OAAO,WAAW,CAAC,EAAA,CAC3B,KAAK;CAC5B,IAAI,CAAC,KAAK,KAET,MAAM,IAAI,yBAAyB,oCAAoC,IAAI,MAAM,IAAI;CAGtF,OAAO,KAAK;AACb;;AAGA,SAAgB,YAAY,MAAqD;CAChF,IAAI,CAAC,MAAM,WAAW,GAAG,KAAK,KAAK,WAAW,IAAI,KAAK,KAAK,WAAW,KAAK,GAC3E;CAGD,MAAM,SAAS;CACf,MAAM,MAAM,IAAI,IAAI,MAAM,MAAM;CAChC,IAAI,IAAI,WAAW,QAClB,OAAO,IAAI,KAAK,MAAM,EAAa;AAErC"}
|
|
@@ -1,12 +0,0 @@
|
|
|
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, name, value, expires) {
|
|
4
|
-
const previous = res.getHeader("Set-Cookie") ?? [];
|
|
5
|
-
let cookie = `${name}=${encodeURIComponent(value)}; Path=/; SameSite=None; Secure`;
|
|
6
|
-
if (expires) cookie += `; Expires=${expires.toUTCString()}`;
|
|
7
|
-
res.setHeader("Set-Cookie", [...[previous].flat().map(String), cookie]);
|
|
8
|
-
}
|
|
9
|
-
//#endregion
|
|
10
|
-
exports.appendCookie = appendCookie;
|
|
11
|
-
|
|
12
|
-
//# sourceMappingURL=appendCookie.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
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(\n\tres: NextApiResponseLike,\n\tname: string,\n\tvalue: string,\n\texpires?: Date,\n): void {\n\tconst previous = res.getHeader(\"Set-Cookie\") ?? []\n\tlet cookie = `${name}=${encodeURIComponent(value)}; Path=/; SameSite=None; Secure`\n\tif (expires) {\n\t\tcookie += `; Expires=${expires.toUTCString()}`\n\t}\n\tres.setHeader(\"Set-Cookie\", [...[previous].flat().map(String), cookie])\n}\n"],"mappings":";;AAGA,SAAgB,aACf,KACA,MACA,OACA,SACO;CACP,MAAM,WAAW,IAAI,UAAU,YAAY,KAAK,CAAC;CACjD,IAAI,SAAS,GAAG,KAAK,GAAG,mBAAmB,KAAK,EAAE;CAClD,IAAI,SACH,UAAU,aAAa,QAAQ,YAAY;CAE5C,IAAI,UAAU,cAAc,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,MAAM,GAAG,MAAM,CAAC;AACvE"}
|
|
@@ -1,12 +0,0 @@
|
|
|
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, name, value, expires) {
|
|
4
|
-
const previous = res.getHeader("Set-Cookie") ?? [];
|
|
5
|
-
let cookie = `${name}=${encodeURIComponent(value)}; Path=/; SameSite=None; Secure`;
|
|
6
|
-
if (expires) cookie += `; Expires=${expires.toUTCString()}`;
|
|
7
|
-
res.setHeader("Set-Cookie", [...[previous].flat().map(String), cookie]);
|
|
8
|
-
}
|
|
9
|
-
//#endregion
|
|
10
|
-
export { appendCookie };
|
|
11
|
-
|
|
12
|
-
//# sourceMappingURL=appendCookie.js.map
|
|
@@ -1 +0,0 @@
|
|
|
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(\n\tres: NextApiResponseLike,\n\tname: string,\n\tvalue: string,\n\texpires?: Date,\n): void {\n\tconst previous = res.getHeader(\"Set-Cookie\") ?? []\n\tlet cookie = `${name}=${encodeURIComponent(value)}; Path=/; SameSite=None; Secure`\n\tif (expires) {\n\t\tcookie += `; Expires=${expires.toUTCString()}`\n\t}\n\tres.setHeader(\"Set-Cookie\", [...[previous].flat().map(String), cookie])\n}\n"],"mappings":";;AAGA,SAAgB,aACf,KACA,MACA,OACA,SACO;CACP,MAAM,WAAW,IAAI,UAAU,YAAY,KAAK,CAAC;CACjD,IAAI,SAAS,GAAG,KAAK,GAAG,mBAAmB,KAAK,EAAE;CAClD,IAAI,SACH,UAAU,aAAa,QAAQ,YAAY;CAE5C,IAAI,UAAU,cAAc,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,MAAM,GAAG,MAAM,CAAC;AACvE"}
|