@prismicio/next 1.3.3 → 1.3.4
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/package.json.cjs +1 -1
- package/dist/package.json.js +1 -1
- package/dist/redirectToPreviewURL.cjs +7 -0
- package/dist/redirectToPreviewURL.cjs.map +1 -1
- package/dist/redirectToPreviewURL.d.ts +1 -1
- package/dist/redirectToPreviewURL.js +7 -0
- package/dist/redirectToPreviewURL.js.map +1 -1
- package/dist/types.d.ts +5 -1
- package/package.json +1 -1
- package/src/redirectToPreviewURL.ts +14 -1
- package/src/types.ts +5 -1
package/dist/package.json.cjs
CHANGED
package/dist/package.json.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const navigation = require("next/navigation");
|
|
4
|
+
const headers = require("next/headers");
|
|
5
|
+
const cookie = require('./_node_modules/@prismicio/client/dist/cookie.cjs');
|
|
4
6
|
async function redirectToPreviewURL(config) {
|
|
5
7
|
const basePath = config.basePath || "";
|
|
6
8
|
const request = "request" in config ? config.request : config.req;
|
|
@@ -10,6 +12,11 @@ async function redirectToPreviewURL(config) {
|
|
|
10
12
|
defaultURL: config.defaultURL || "/"
|
|
11
13
|
});
|
|
12
14
|
if ("nextUrl" in request) {
|
|
15
|
+
headers.draftMode().enable();
|
|
16
|
+
const previewCookie = request.nextUrl.searchParams.get("token");
|
|
17
|
+
if (previewCookie) {
|
|
18
|
+
headers.cookies().set(cookie.preview, previewCookie);
|
|
19
|
+
}
|
|
13
20
|
navigation.redirect(basePath + previewUrl);
|
|
14
21
|
} else {
|
|
15
22
|
if (!("res" in config)) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redirectToPreviewURL.cjs","sources":["../../src/redirectToPreviewURL.ts"],"sourcesContent":["import { redirect } from \"next/navigation\";\nimport
|
|
1
|
+
{"version":3,"file":"redirectToPreviewURL.cjs","sources":["../../src/redirectToPreviewURL.ts"],"sourcesContent":["import { redirect } from \"next/navigation\";\nimport { cookies, draftMode } from \"next/headers\";\nimport * as prismic from \"@prismicio/client\";\n\nimport {\n\tNextApiRequestLike,\n\tNextApiResponseLike,\n\tNextRequestLike,\n} from \"./types\";\n\n/**\n * Preview config for enabling previews with redirectToPreviewURL\n */\nexport type RedirectToPreviewURLConfig = (\n\t| {\n\t\t\t/**\n\t\t\t * The `request` object from a Next.js Route Handler.\n\t\t\t *\n\t\t\t * @see Next.js Route Handler docs: \\<https://beta.nextjs.org/docs/routing/route-handlers\\>\n\t\t\t */\n\t\t\trequest: NextRequestLike;\n\t }\n\t| {\n\t\t\t/**\n\t\t\t * The `req` object from a Next.js API route.\n\t\t\t *\n\t\t\t * @see Next.js API route docs: \\<https://nextjs.org/docs/api-routes/introduction\\>\n\t\t\t */\n\t\t\treq: NextApiRequestLike;\n\n\t\t\t/**\n\t\t\t * The `res` object from a Next.js API route.\n\t\t\t *\n\t\t\t * @see Next.js API route docs: \\<https://nextjs.org/docs/api-routes/introduction\\>\n\t\t\t */\n\t\t\tres: NextApiResponseLike;\n\t }\n) & {\n\t/**\n\t * The Prismic client configured for the preview session's repository.\n\t */\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<\n\t\tprismic.Client,\n\t\t\"enableAutoPreviewsFromReq\" | \"resolvePreviewURL\"\n\t>;\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?: prismic.LinkResolverFunction;\n\n\t/**\n\t * The default redirect URL if a URL cannot be determined for the previewed\n\t * document.\n\t *\n\t * **Note**: If you `next.config.js` file contains a `basePath`, the\n\t * `defaultURL` option must _not_ include it. Instead, provide the `basePath`\n\t * 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`.\n\t * This option can be 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`\n\t * automatically. It must be provided to `redirectToPreviewURL()` manually.\n\t */\n\tbasePath?: string;\n};\n\n/**\n * Redirects a visitor to the URL of a previewed Prismic document from within a\n * Next.js Route Handler or API route.\n */\nexport async function redirectToPreviewURL(\n\tconfig: RedirectToPreviewURLConfig,\n): Promise<void> {\n\tconst basePath = config.basePath || \"\";\n\tconst request = \"request\" in config ? config.request : config.req;\n\n\tconfig.client.enableAutoPreviewsFromReq(request);\n\n\tconst previewUrl = await config.client.resolvePreviewURL({\n\t\tlinkResolver: config.linkResolver,\n\t\tdefaultURL: config.defaultURL || \"/\",\n\t});\n\n\tif (\"nextUrl\" in request) {\n\t\tdraftMode().enable();\n\n\t\t// Set the initial preview cookie, if available.\n\t\t// Setting the cookie here is necessary to support unpublished\n\t\t// previews. Without setting it here, the page will try to\n\t\t// render without the preview cookie, leading to a\n\t\t// PrismicNotFound error.\n\t\tconst previewCookie = request.nextUrl.searchParams.get(\"token\");\n\t\tif (previewCookie) {\n\t\t\tcookies().set(prismic.cookie.preview, previewCookie);\n\t\t}\n\n\t\tredirect(basePath + previewUrl);\n\t} else {\n\t\tif (!(\"res\" in config)) {\n\t\t\tthrow new Error(\n\t\t\t\t\"[redirectToPreviewURL] The `res` object from the API route must be provided to `redirectToPreviewURL()`.\",\n\t\t\t);\n\t\t}\n\n\t\tconfig.res.redirect(basePath + previewUrl);\n\n\t\treturn;\n\t}\n}\n"],"names":["draftMode","cookies","prismic.cookie.preview","redirect"],"mappings":";;;;;AAiFA,eAAsB,qBACrB,QAAkC;AAE5B,QAAA,WAAW,OAAO,YAAY;AACpC,QAAM,UAAU,aAAa,SAAS,OAAO,UAAU,OAAO;AAEvD,SAAA,OAAO,0BAA0B,OAAO;AAE/C,QAAM,aAAa,MAAM,OAAO,OAAO,kBAAkB;AAAA,IACxD,cAAc,OAAO;AAAA,IACrB,YAAY,OAAO,cAAc;AAAA,EAAA,CACjC;AAED,MAAI,aAAa,SAAS;AACzBA,YAAA,UAAA,EAAY;AAOZ,UAAM,gBAAgB,QAAQ,QAAQ,aAAa,IAAI,OAAO;AAC9D,QAAI,eAAe;AAClBC,cAAAA,UAAU,IAAIC,gBAAwB,aAAa;AAAA,IACnD;AAEDC,wBAAS,WAAW,UAAU;AAAA,EAAA,OACxB;AACF,QAAA,EAAE,SAAS,SAAS;AACjB,YAAA,IAAI,MACT,0GAA0G;AAAA,IAE3G;AAEM,WAAA,IAAI,SAAS,WAAW,UAAU;AAEzC;AAAA,EACA;AACF;;"}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { redirect } from "next/navigation";
|
|
2
|
+
import { draftMode, cookies } from "next/headers";
|
|
3
|
+
import { preview } from './_node_modules/@prismicio/client/dist/cookie.js';
|
|
2
4
|
async function redirectToPreviewURL(config) {
|
|
3
5
|
const basePath = config.basePath || "";
|
|
4
6
|
const request = "request" in config ? config.request : config.req;
|
|
@@ -8,6 +10,11 @@ async function redirectToPreviewURL(config) {
|
|
|
8
10
|
defaultURL: config.defaultURL || "/"
|
|
9
11
|
});
|
|
10
12
|
if ("nextUrl" in request) {
|
|
13
|
+
draftMode().enable();
|
|
14
|
+
const previewCookie = request.nextUrl.searchParams.get("token");
|
|
15
|
+
if (previewCookie) {
|
|
16
|
+
cookies().set(preview, previewCookie);
|
|
17
|
+
}
|
|
11
18
|
redirect(basePath + previewUrl);
|
|
12
19
|
} else {
|
|
13
20
|
if (!("res" in config)) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redirectToPreviewURL.js","sources":["../../src/redirectToPreviewURL.ts"],"sourcesContent":["import { redirect } from \"next/navigation\";\nimport
|
|
1
|
+
{"version":3,"file":"redirectToPreviewURL.js","sources":["../../src/redirectToPreviewURL.ts"],"sourcesContent":["import { redirect } from \"next/navigation\";\nimport { cookies, draftMode } from \"next/headers\";\nimport * as prismic from \"@prismicio/client\";\n\nimport {\n\tNextApiRequestLike,\n\tNextApiResponseLike,\n\tNextRequestLike,\n} from \"./types\";\n\n/**\n * Preview config for enabling previews with redirectToPreviewURL\n */\nexport type RedirectToPreviewURLConfig = (\n\t| {\n\t\t\t/**\n\t\t\t * The `request` object from a Next.js Route Handler.\n\t\t\t *\n\t\t\t * @see Next.js Route Handler docs: \\<https://beta.nextjs.org/docs/routing/route-handlers\\>\n\t\t\t */\n\t\t\trequest: NextRequestLike;\n\t }\n\t| {\n\t\t\t/**\n\t\t\t * The `req` object from a Next.js API route.\n\t\t\t *\n\t\t\t * @see Next.js API route docs: \\<https://nextjs.org/docs/api-routes/introduction\\>\n\t\t\t */\n\t\t\treq: NextApiRequestLike;\n\n\t\t\t/**\n\t\t\t * The `res` object from a Next.js API route.\n\t\t\t *\n\t\t\t * @see Next.js API route docs: \\<https://nextjs.org/docs/api-routes/introduction\\>\n\t\t\t */\n\t\t\tres: NextApiResponseLike;\n\t }\n) & {\n\t/**\n\t * The Prismic client configured for the preview session's repository.\n\t */\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<\n\t\tprismic.Client,\n\t\t\"enableAutoPreviewsFromReq\" | \"resolvePreviewURL\"\n\t>;\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?: prismic.LinkResolverFunction;\n\n\t/**\n\t * The default redirect URL if a URL cannot be determined for the previewed\n\t * document.\n\t *\n\t * **Note**: If you `next.config.js` file contains a `basePath`, the\n\t * `defaultURL` option must _not_ include it. Instead, provide the `basePath`\n\t * 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`.\n\t * This option can be 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`\n\t * automatically. It must be provided to `redirectToPreviewURL()` manually.\n\t */\n\tbasePath?: string;\n};\n\n/**\n * Redirects a visitor to the URL of a previewed Prismic document from within a\n * Next.js Route Handler or API route.\n */\nexport async function redirectToPreviewURL(\n\tconfig: RedirectToPreviewURLConfig,\n): Promise<void> {\n\tconst basePath = config.basePath || \"\";\n\tconst request = \"request\" in config ? config.request : config.req;\n\n\tconfig.client.enableAutoPreviewsFromReq(request);\n\n\tconst previewUrl = await config.client.resolvePreviewURL({\n\t\tlinkResolver: config.linkResolver,\n\t\tdefaultURL: config.defaultURL || \"/\",\n\t});\n\n\tif (\"nextUrl\" in request) {\n\t\tdraftMode().enable();\n\n\t\t// Set the initial preview cookie, if available.\n\t\t// Setting the cookie here is necessary to support unpublished\n\t\t// previews. Without setting it here, the page will try to\n\t\t// render without the preview cookie, leading to a\n\t\t// PrismicNotFound error.\n\t\tconst previewCookie = request.nextUrl.searchParams.get(\"token\");\n\t\tif (previewCookie) {\n\t\t\tcookies().set(prismic.cookie.preview, previewCookie);\n\t\t}\n\n\t\tredirect(basePath + previewUrl);\n\t} else {\n\t\tif (!(\"res\" in config)) {\n\t\t\tthrow new Error(\n\t\t\t\t\"[redirectToPreviewURL] The `res` object from the API route must be provided to `redirectToPreviewURL()`.\",\n\t\t\t);\n\t\t}\n\n\t\tconfig.res.redirect(basePath + previewUrl);\n\n\t\treturn;\n\t}\n}\n"],"names":["prismic.cookie.preview"],"mappings":";;;AAiFA,eAAsB,qBACrB,QAAkC;AAE5B,QAAA,WAAW,OAAO,YAAY;AACpC,QAAM,UAAU,aAAa,SAAS,OAAO,UAAU,OAAO;AAEvD,SAAA,OAAO,0BAA0B,OAAO;AAE/C,QAAM,aAAa,MAAM,OAAO,OAAO,kBAAkB;AAAA,IACxD,cAAc,OAAO;AAAA,IACrB,YAAY,OAAO,cAAc;AAAA,EAAA,CACjC;AAED,MAAI,aAAa,SAAS;AACzB,cAAA,EAAY;AAOZ,UAAM,gBAAgB,QAAQ,QAAQ,aAAa,IAAI,OAAO;AAC9D,QAAI,eAAe;AAClB,gBAAU,IAAIA,SAAwB,aAAa;AAAA,IACnD;AAED,aAAS,WAAW,UAAU;AAAA,EAAA,OACxB;AACF,QAAA,EAAE,SAAS,SAAS;AACjB,YAAA,IAAI,MACT,0GAA0G;AAAA,IAE3G;AAEM,WAAA,IAAI,SAAS,WAAW,UAAU;AAEzC;AAAA,EACA;AACF;"}
|
package/dist/types.d.ts
CHANGED
|
@@ -43,7 +43,11 @@ export type NextRequestLike = {
|
|
|
43
43
|
get(name: string): string | null;
|
|
44
44
|
};
|
|
45
45
|
url: string;
|
|
46
|
-
nextUrl:
|
|
46
|
+
nextUrl: {
|
|
47
|
+
searchParams: {
|
|
48
|
+
get(name: string): string | null;
|
|
49
|
+
};
|
|
50
|
+
};
|
|
47
51
|
};
|
|
48
52
|
/**
|
|
49
53
|
* The minimal set of properties needed from `next`'s `NextApiRequest` type.
|
package/package.json
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { redirect } from "next/navigation";
|
|
2
|
-
import
|
|
2
|
+
import { cookies, draftMode } from "next/headers";
|
|
3
|
+
import * as prismic from "@prismicio/client";
|
|
3
4
|
|
|
4
5
|
import {
|
|
5
6
|
NextApiRequestLike,
|
|
@@ -92,6 +93,18 @@ export async function redirectToPreviewURL(
|
|
|
92
93
|
});
|
|
93
94
|
|
|
94
95
|
if ("nextUrl" in request) {
|
|
96
|
+
draftMode().enable();
|
|
97
|
+
|
|
98
|
+
// Set the initial preview cookie, if available.
|
|
99
|
+
// Setting the cookie here is necessary to support unpublished
|
|
100
|
+
// previews. Without setting it here, the page will try to
|
|
101
|
+
// render without the preview cookie, leading to a
|
|
102
|
+
// PrismicNotFound error.
|
|
103
|
+
const previewCookie = request.nextUrl.searchParams.get("token");
|
|
104
|
+
if (previewCookie) {
|
|
105
|
+
cookies().set(prismic.cookie.preview, previewCookie);
|
|
106
|
+
}
|
|
107
|
+
|
|
95
108
|
redirect(basePath + previewUrl);
|
|
96
109
|
} else {
|
|
97
110
|
if (!("res" in config)) {
|