@prismicio/next 1.1.0-alpha.1 → 1.1.0
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/PrismicNextLink.d.ts +3 -3
- package/dist/PrismicPreview.cjs +5 -74
- package/dist/PrismicPreview.cjs.map +1 -1
- package/dist/PrismicPreview.d.ts +8 -11
- package/dist/PrismicPreview.js +5 -57
- package/dist/PrismicPreview.js.map +1 -1
- package/dist/PrismicPreviewClient.cjs +85 -0
- package/dist/PrismicPreviewClient.cjs.map +1 -0
- package/dist/PrismicPreviewClient.d.ts +4 -0
- package/dist/PrismicPreviewClient.js +85 -0
- package/dist/PrismicPreviewClient.js.map +1 -0
- package/dist/_node_modules/@prismicio/client/dist/errors/PrismicError.cjs +19 -0
- package/dist/_node_modules/@prismicio/client/dist/errors/PrismicError.cjs.map +1 -0
- package/dist/_node_modules/@prismicio/client/dist/errors/PrismicError.js +19 -0
- package/dist/_node_modules/@prismicio/client/dist/errors/PrismicError.js.map +1 -0
- package/dist/_node_modules/@prismicio/client/dist/getToolbarSrc.cjs +13 -0
- package/dist/_node_modules/@prismicio/client/dist/getToolbarSrc.cjs.map +1 -0
- package/dist/_node_modules/@prismicio/client/dist/getToolbarSrc.js +13 -0
- package/dist/_node_modules/@prismicio/client/dist/getToolbarSrc.js.map +1 -0
- package/dist/_node_modules/@prismicio/client/dist/isRepositoryName.cjs +7 -0
- package/dist/_node_modules/@prismicio/client/dist/isRepositoryName.cjs.map +1 -0
- package/dist/_node_modules/@prismicio/client/dist/isRepositoryName.js +7 -0
- package/dist/_node_modules/@prismicio/client/dist/isRepositoryName.js.map +1 -0
- package/dist/enableAutoPreviews.cjs +17 -6
- package/dist/enableAutoPreviews.cjs.map +1 -1
- package/dist/enableAutoPreviews.d.ts +11 -12
- package/dist/enableAutoPreviews.js +17 -6
- package/dist/enableAutoPreviews.js.map +1 -1
- package/dist/exitPreview.cjs +1 -1
- package/dist/exitPreview.cjs.map +1 -1
- package/dist/exitPreview.d.ts +11 -16
- package/dist/exitPreview.js +1 -1
- package/dist/exitPreview.js.map +1 -1
- package/dist/package.json.cjs +1 -1
- package/dist/package.json.js +1 -1
- package/dist/redirectToPreviewURL.cjs +13 -12
- package/dist/redirectToPreviewURL.cjs.map +1 -1
- package/dist/redirectToPreviewURL.d.ts +23 -21
- package/dist/redirectToPreviewURL.js +13 -12
- package/dist/redirectToPreviewURL.js.map +1 -1
- package/dist/setPreviewData.cjs.map +1 -1
- package/dist/setPreviewData.d.ts +9 -14
- package/dist/setPreviewData.js.map +1 -1
- package/dist/types.d.ts +52 -9
- package/package.json +5 -10
- package/src/PrismicPreview.tsx +13 -131
- package/src/PrismicPreviewClient.tsx +159 -0
- package/src/enableAutoPreviews.ts +73 -45
- package/src/exitPreview.ts +14 -26
- package/src/redirectToPreviewURL.ts +58 -64
- package/src/setPreviewData.ts +10 -14
- package/src/types.ts +58 -9
- package/dist/react-server/PrismicPreview.d.ts +0 -22
- package/dist/react-server/index.d.ts +0 -2
- package/dist/react-server/unsupported.cjs +0 -26
- package/dist/react-server/unsupported.cjs.map +0 -1
- package/dist/react-server/unsupported.d.ts +0 -6
- package/dist/react-server/unsupported.js +0 -26
- package/dist/react-server/unsupported.js.map +0 -1
- package/dist/react-server.cjs +0 -15
- package/dist/react-server.cjs.map +0 -1
- package/dist/react-server.js +0 -15
- package/dist/react-server.js.map +0 -1
- package/src/react-server/PrismicPreview.tsx +0 -74
- package/src/react-server/index.ts +0 -11
- package/src/react-server/unsupported.ts +0 -26
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PrismicError.js","sources":["../../../../../../node_modules/@prismicio/client/dist/errors/PrismicError.js"],"sourcesContent":["var __defProp = Object.defineProperty;\nvar __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;\nvar __publicField = (obj, key, value) => {\n __defNormalProp(obj, typeof key !== \"symbol\" ? key + \"\" : key, value);\n return value;\n};\nclass PrismicError extends Error {\n constructor(message = \"An invalid API response was returned\", url, response) {\n super(message);\n __publicField(this, \"url\");\n __publicField(this, \"response\");\n this.url = url;\n this.response = response;\n }\n}\nexport {\n PrismicError\n};\n//# sourceMappingURL=PrismicError.js.map\n"],"names":[],"mappings":"AAAA,IAAI,YAAY,OAAO;AACvB,IAAI,kBAAkB,CAAC,KAAK,KAAK,UAAU,OAAO,MAAM,UAAU,KAAK,KAAK,EAAE,YAAY,MAAM,cAAc,MAAM,UAAU,MAAM,MAAO,CAAA,IAAI,IAAI,GAAG,IAAI;AAC1J,IAAI,gBAAgB,CAAC,KAAK,KAAK,UAAU;AACvC,kBAAgB,KAAK,OAAO,QAAQ,WAAW,MAAM,KAAK,KAAK,KAAK;AACpE,SAAO;AACT;AACA,MAAM,qBAAqB,MAAM;AAAA,EAC/B,YAAY,UAAU,wCAAwC,KAAK,UAAU;AAC3E,UAAM,OAAO;AACb,kBAAc,MAAM,KAAK;AACzB,kBAAc,MAAM,UAAU;AAC9B,SAAK,MAAM;AACX,SAAK,WAAW;AAAA,EACjB;AACH;","x_google_ignoreList":[0]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const PrismicError = require("./errors/PrismicError.cjs");
|
|
4
|
+
const isRepositoryName = require("./isRepositoryName.cjs");
|
|
5
|
+
const getToolbarSrc = (repositoryName) => {
|
|
6
|
+
if (isRepositoryName.isRepositoryName(repositoryName)) {
|
|
7
|
+
return `https://static.cdn.prismic.io/prismic.js?new=true&repo=${repositoryName}`;
|
|
8
|
+
} else {
|
|
9
|
+
throw new PrismicError.PrismicError(`An invalid Prismic repository name was given: ${repositoryName}`, void 0, void 0);
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
exports.getToolbarSrc = getToolbarSrc;
|
|
13
|
+
//# sourceMappingURL=getToolbarSrc.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getToolbarSrc.cjs","sources":["../../../../../node_modules/@prismicio/client/dist/getToolbarSrc.js"],"sourcesContent":["import { PrismicError } from \"./errors/PrismicError.js\";\nimport { isRepositoryName } from \"./isRepositoryName.js\";\nconst getToolbarSrc = (repositoryName) => {\n if (isRepositoryName(repositoryName)) {\n return `https://static.cdn.prismic.io/prismic.js?new=true&repo=${repositoryName}`;\n } else {\n throw new PrismicError(`An invalid Prismic repository name was given: ${repositoryName}`, void 0, void 0);\n }\n};\nexport {\n getToolbarSrc\n};\n//# sourceMappingURL=getToolbarSrc.js.map\n"],"names":["isRepositoryName","PrismicError"],"mappings":";;;;AAEK,MAAC,gBAAgB,CAAC,mBAAmB;AACxC,MAAIA,iBAAAA,iBAAiB,cAAc,GAAG;AACpC,WAAO,0DAA0D;AAAA,EACrE,OAAS;AACL,UAAM,IAAIC,aAAAA,aAAa,iDAAiD,kBAAkB,QAAQ,MAAM;AAAA,EACzG;AACH;;","x_google_ignoreList":[0]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { PrismicError } from "./errors/PrismicError.js";
|
|
2
|
+
import { isRepositoryName } from "./isRepositoryName.js";
|
|
3
|
+
const getToolbarSrc = (repositoryName) => {
|
|
4
|
+
if (isRepositoryName(repositoryName)) {
|
|
5
|
+
return `https://static.cdn.prismic.io/prismic.js?new=true&repo=${repositoryName}`;
|
|
6
|
+
} else {
|
|
7
|
+
throw new PrismicError(`An invalid Prismic repository name was given: ${repositoryName}`, void 0, void 0);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
export {
|
|
11
|
+
getToolbarSrc
|
|
12
|
+
};
|
|
13
|
+
//# sourceMappingURL=getToolbarSrc.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getToolbarSrc.js","sources":["../../../../../node_modules/@prismicio/client/dist/getToolbarSrc.js"],"sourcesContent":["import { PrismicError } from \"./errors/PrismicError.js\";\nimport { isRepositoryName } from \"./isRepositoryName.js\";\nconst getToolbarSrc = (repositoryName) => {\n if (isRepositoryName(repositoryName)) {\n return `https://static.cdn.prismic.io/prismic.js?new=true&repo=${repositoryName}`;\n } else {\n throw new PrismicError(`An invalid Prismic repository name was given: ${repositoryName}`, void 0, void 0);\n }\n};\nexport {\n getToolbarSrc\n};\n//# sourceMappingURL=getToolbarSrc.js.map\n"],"names":[],"mappings":";;AAEK,MAAC,gBAAgB,CAAC,mBAAmB;AACxC,MAAI,iBAAiB,cAAc,GAAG;AACpC,WAAO,0DAA0D;AAAA,EACrE,OAAS;AACL,UAAM,IAAI,aAAa,iDAAiD,kBAAkB,QAAQ,MAAM;AAAA,EACzG;AACH;","x_google_ignoreList":[0]}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const isRepositoryName = (input) => {
|
|
4
|
+
return /^[a-zA-Z0-9][-a-zA-Z0-9]{2,}[a-zA-Z0-9]$/.test(input);
|
|
5
|
+
};
|
|
6
|
+
exports.isRepositoryName = isRepositoryName;
|
|
7
|
+
//# sourceMappingURL=isRepositoryName.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isRepositoryName.cjs","sources":["../../../../../node_modules/@prismicio/client/dist/isRepositoryName.js"],"sourcesContent":["const isRepositoryName = (input) => {\n return /^[a-zA-Z0-9][-a-zA-Z0-9]{2,}[a-zA-Z0-9]$/.test(input);\n};\nexport {\n isRepositoryName\n};\n//# sourceMappingURL=isRepositoryName.js.map\n"],"names":[],"mappings":";;AAAK,MAAC,mBAAmB,CAAC,UAAU;AAClC,SAAO,2CAA2C,KAAK,KAAK;AAC9D;;","x_google_ignoreList":[0]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isRepositoryName.js","sources":["../../../../../node_modules/@prismicio/client/dist/isRepositoryName.js"],"sourcesContent":["const isRepositoryName = (input) => {\n return /^[a-zA-Z0-9][-a-zA-Z0-9]{2,}[a-zA-Z0-9]$/.test(input);\n};\nexport {\n isRepositoryName\n};\n//# sourceMappingURL=isRepositoryName.js.map\n"],"names":[],"mappings":"AAAK,MAAC,mBAAmB,CAAC,UAAU;AAClC,SAAO,2CAA2C,KAAK,KAAK;AAC9D;","x_google_ignoreList":[0]}
|
|
@@ -1,16 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
};
|
|
3
|
+
const headers = require("next/headers");
|
|
4
|
+
const cookie = require('./_node_modules/@prismicio/client/dist/cookie.cjs');
|
|
6
5
|
const enableAutoPreviews = (config) => {
|
|
7
6
|
if ("previewData" in config && config.previewData) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
config.client.queryContentFromRef(previewData.ref);
|
|
7
|
+
if (typeof config.previewData === "object" && "ref" in config.previewData && typeof config.previewData.ref === "string") {
|
|
8
|
+
config.client.queryContentFromRef(config.previewData.ref);
|
|
11
9
|
}
|
|
12
10
|
} else if ("req" in config && config.req) {
|
|
13
11
|
config.client.enableAutoPreviewsFromReq(config.req);
|
|
12
|
+
} else {
|
|
13
|
+
config.client.queryContentFromRef(() => {
|
|
14
|
+
var _a;
|
|
15
|
+
let cookie$1;
|
|
16
|
+
try {
|
|
17
|
+
cookie$1 = (_a = headers.cookies().get(cookie.preview)) == null ? void 0 : _a.value;
|
|
18
|
+
} catch {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
if (cookie$1 && /\.prismic\.io/.test(cookie$1)) {
|
|
22
|
+
return cookie$1;
|
|
23
|
+
}
|
|
24
|
+
});
|
|
14
25
|
}
|
|
15
26
|
};
|
|
16
27
|
exports.enableAutoPreviews = enableAutoPreviews;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"enableAutoPreviews.cjs","sources":["../../src/enableAutoPreviews.ts"],"sourcesContent":["import { PreviewData } from \"next\";\nimport
|
|
1
|
+
{"version":3,"file":"enableAutoPreviews.cjs","sources":["../../src/enableAutoPreviews.ts"],"sourcesContent":["import { PreviewData } from \"next\";\nimport { cookies } from \"next/headers\";\nimport * as prismic from \"@prismicio/client\";\n\nimport { NextApiRequestLike } from \"./types\";\n\n/**\n * Configuration for `enableAutoPreviews`.\n *\n * @typeParam TPreviewData - Next.js preview data object.\n */\nexport type EnableAutoPreviewsConfig<\n\tTPreviewData extends PreviewData = PreviewData,\n> = {\n\t/**\n\t * Prismic client with which automatic previews will be enabled.\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\"queryContentFromRef\" | \"enableAutoPreviewsFromReq\"\n\t>;\n\n\t/**\n\t * **Only used in the Pages Directory (/pages).**\n\t *\n\t * The `previewData` object provided in the `getStaticProps()` or\n\t * `getServerSideProps()` context object.\n\t */\n\tpreviewData?: TPreviewData;\n\n\t/**\n\t * **Only used in the Pages Directory (/pages).**\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\n/**\n * Configures a Prismic client to automatically query draft content during a\n * preview session. It either takes in a Next.js `getStaticProps` context object\n * or a Next.js API endpoint request object.\n *\n * @param config - Configuration for the function.\n */\nexport const enableAutoPreviews = <TPreviewData extends PreviewData>(\n\tconfig: EnableAutoPreviewsConfig<TPreviewData>,\n): void => {\n\tif (\"previewData\" in config && config.previewData) {\n\t\t// Assume we are in `getStaticProps()` or\n\t\t// `getServerSideProps()` with active Preview Mode (`pages`\n\t\t// directory).\n\n\t\tif (\n\t\t\ttypeof config.previewData === \"object\" &&\n\t\t\t\"ref\" in config.previewData &&\n\t\t\ttypeof config.previewData.ref === \"string\"\n\t\t) {\n\t\t\tconfig.client.queryContentFromRef(config.previewData.ref);\n\t\t}\n\t} else if (\"req\" in config && config.req) {\n\t\t// Assume we are in an API Route (`pages` directory).\n\n\t\tconfig.client.enableAutoPreviewsFromReq(config.req);\n\t} else {\n\t\t// Assume we are in App Router (`app` directory) OR\n\t\t// `getStaticProps()`/`getServerSideProps()` with an inactive\n\t\t// Preview Mode (`pages` directory).\n\n\t\t// We use a function value so the cookie is checked on every\n\t\t// request. We don't have a static value to read from.\n\t\tconfig.client.queryContentFromRef(() => {\n\t\t\tlet cookie: string | undefined;\n\t\t\ttry {\n\t\t\t\tcookie = cookies().get(prismic.cookie.preview)?.value;\n\t\t\t} catch {\n\t\t\t\t// noop - We are probably in\n\t\t\t\t// `getStaticProps()` or `getServerSideProps()`\n\t\t\t\t// with inactive Preview Mode where `cookies()`\n\t\t\t\t// does not work. We don't need to do any\n\t\t\t\t// preview handling.\n\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// We are probably in App Router (`app` directory).\n\n\t\t\t// We only return the cookie if a Prismic Preview session is active.\n\t\t\t//\n\t\t\t// An inactive cookie looks like this (URL encoded):\n\t\t\t// \t{\n\t\t\t// \t\t\"_tracker\": \"abc123\"\n\t\t\t// \t}\n\t\t\t//\n\t\t\t// An active cookie looks like this (URL encoded):\n\t\t\t// \t{\n\t\t\t// \t\t\"_tracker\": \"abc123\",\n\t\t\t// \t\t\"example-prismic-repo.prismic.io\": {\n\t\t\t// \t\t\tpreview: \"https://example-prismic-repo.prismic.io/previews/abc:123?websitePreviewId=xyz\"\n\t\t\t// \t\t}\n\t\t\t// \t}\n\t\t\tif (cookie && /\\.prismic\\.io/.test(cookie)) {\n\t\t\t\treturn cookie;\n\t\t\t}\n\t\t});\n\t}\n};\n"],"names":["cookie","cookies","prismic.cookie.preview"],"mappings":";;;;AAkDa,MAAA,qBAAqB,CACjC,WACS;AACL,MAAA,iBAAiB,UAAU,OAAO,aAAa;AAMjD,QAAA,OAAO,OAAO,gBAAgB,YAC9B,SAAS,OAAO,eAChB,OAAO,OAAO,YAAY,QAAQ,UACjC;AACD,aAAO,OAAO,oBAAoB,OAAO,YAAY,GAAG;AAAA,IACxD;AAAA,EACS,WAAA,SAAS,UAAU,OAAO,KAAK;AAGlC,WAAA,OAAO,0BAA0B,OAAO,GAAG;AAAA,EAAA,OAC5C;AAOC,WAAA,OAAO,oBAAoB,MAAK;;AAClC,UAAAA;AACA,UAAA;AACHA,oBAASC,aAAAA,QAAU,EAAA,IAAIC,OAAsB,OAAA,MAApCD,mBAAuC;AAAA,MAAA,QAC/C;AAOD;AAAA,MACA;AAkBD,UAAID,YAAU,gBAAgB,KAAKA,QAAM,GAAG;AACpC,eAAAA;AAAAA,MACP;AAAA,IAAA,CACD;AAAA,EACD;AACF;;"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { PreviewData } from "next";
|
|
2
|
-
import
|
|
2
|
+
import * as prismic from "@prismicio/client";
|
|
3
|
+
import { NextApiRequestLike } from "./types";
|
|
3
4
|
/**
|
|
4
5
|
* Configuration for `enableAutoPreviews`.
|
|
5
6
|
*
|
|
@@ -10,24 +11,22 @@ export type EnableAutoPreviewsConfig<TPreviewData extends PreviewData = PreviewD
|
|
|
10
11
|
* Prismic client with which automatic previews will be enabled.
|
|
11
12
|
*/
|
|
12
13
|
client: Pick<prismic.Client, "queryContentFromRef" | "enableAutoPreviewsFromReq">;
|
|
13
|
-
} & ({
|
|
14
14
|
/**
|
|
15
|
-
*
|
|
16
|
-
* `getStaticProps` or `getServerSideProps`).
|
|
15
|
+
* **Only used in the Pages Directory (/pages).**
|
|
17
16
|
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
17
|
+
* The `previewData` object provided in the `getStaticProps()` or
|
|
18
|
+
* `getServerSideProps()` context object.
|
|
20
19
|
*/
|
|
21
20
|
previewData?: TPreviewData;
|
|
22
|
-
} | {
|
|
23
21
|
/**
|
|
24
|
-
*
|
|
22
|
+
* **Only used in the Pages Directory (/pages).**
|
|
25
23
|
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
24
|
+
* The `req` object from a Next.js API route.
|
|
25
|
+
*
|
|
26
|
+
* @see Next.js API route docs: \<https://nextjs.org/docs/api-routes/introduction\>
|
|
28
27
|
*/
|
|
29
|
-
req?:
|
|
30
|
-
}
|
|
28
|
+
req?: NextApiRequestLike;
|
|
29
|
+
};
|
|
31
30
|
/**
|
|
32
31
|
* Configures a Prismic client to automatically query draft content during a
|
|
33
32
|
* preview session. It either takes in a Next.js `getStaticProps` context object
|
|
@@ -1,14 +1,25 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
};
|
|
1
|
+
import { cookies } from "next/headers";
|
|
2
|
+
import { preview } from './_node_modules/@prismicio/client/dist/cookie.js';
|
|
4
3
|
const enableAutoPreviews = (config) => {
|
|
5
4
|
if ("previewData" in config && config.previewData) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
config.client.queryContentFromRef(previewData.ref);
|
|
5
|
+
if (typeof config.previewData === "object" && "ref" in config.previewData && typeof config.previewData.ref === "string") {
|
|
6
|
+
config.client.queryContentFromRef(config.previewData.ref);
|
|
9
7
|
}
|
|
10
8
|
} else if ("req" in config && config.req) {
|
|
11
9
|
config.client.enableAutoPreviewsFromReq(config.req);
|
|
10
|
+
} else {
|
|
11
|
+
config.client.queryContentFromRef(() => {
|
|
12
|
+
var _a;
|
|
13
|
+
let cookie;
|
|
14
|
+
try {
|
|
15
|
+
cookie = (_a = cookies().get(preview)) == null ? void 0 : _a.value;
|
|
16
|
+
} catch {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
if (cookie && /\.prismic\.io/.test(cookie)) {
|
|
20
|
+
return cookie;
|
|
21
|
+
}
|
|
22
|
+
});
|
|
12
23
|
}
|
|
13
24
|
};
|
|
14
25
|
export {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"enableAutoPreviews.js","sources":["../../src/enableAutoPreviews.ts"],"sourcesContent":["import { PreviewData } from \"next\";\nimport
|
|
1
|
+
{"version":3,"file":"enableAutoPreviews.js","sources":["../../src/enableAutoPreviews.ts"],"sourcesContent":["import { PreviewData } from \"next\";\nimport { cookies } from \"next/headers\";\nimport * as prismic from \"@prismicio/client\";\n\nimport { NextApiRequestLike } from \"./types\";\n\n/**\n * Configuration for `enableAutoPreviews`.\n *\n * @typeParam TPreviewData - Next.js preview data object.\n */\nexport type EnableAutoPreviewsConfig<\n\tTPreviewData extends PreviewData = PreviewData,\n> = {\n\t/**\n\t * Prismic client with which automatic previews will be enabled.\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\"queryContentFromRef\" | \"enableAutoPreviewsFromReq\"\n\t>;\n\n\t/**\n\t * **Only used in the Pages Directory (/pages).**\n\t *\n\t * The `previewData` object provided in the `getStaticProps()` or\n\t * `getServerSideProps()` context object.\n\t */\n\tpreviewData?: TPreviewData;\n\n\t/**\n\t * **Only used in the Pages Directory (/pages).**\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\n/**\n * Configures a Prismic client to automatically query draft content during a\n * preview session. It either takes in a Next.js `getStaticProps` context object\n * or a Next.js API endpoint request object.\n *\n * @param config - Configuration for the function.\n */\nexport const enableAutoPreviews = <TPreviewData extends PreviewData>(\n\tconfig: EnableAutoPreviewsConfig<TPreviewData>,\n): void => {\n\tif (\"previewData\" in config && config.previewData) {\n\t\t// Assume we are in `getStaticProps()` or\n\t\t// `getServerSideProps()` with active Preview Mode (`pages`\n\t\t// directory).\n\n\t\tif (\n\t\t\ttypeof config.previewData === \"object\" &&\n\t\t\t\"ref\" in config.previewData &&\n\t\t\ttypeof config.previewData.ref === \"string\"\n\t\t) {\n\t\t\tconfig.client.queryContentFromRef(config.previewData.ref);\n\t\t}\n\t} else if (\"req\" in config && config.req) {\n\t\t// Assume we are in an API Route (`pages` directory).\n\n\t\tconfig.client.enableAutoPreviewsFromReq(config.req);\n\t} else {\n\t\t// Assume we are in App Router (`app` directory) OR\n\t\t// `getStaticProps()`/`getServerSideProps()` with an inactive\n\t\t// Preview Mode (`pages` directory).\n\n\t\t// We use a function value so the cookie is checked on every\n\t\t// request. We don't have a static value to read from.\n\t\tconfig.client.queryContentFromRef(() => {\n\t\t\tlet cookie: string | undefined;\n\t\t\ttry {\n\t\t\t\tcookie = cookies().get(prismic.cookie.preview)?.value;\n\t\t\t} catch {\n\t\t\t\t// noop - We are probably in\n\t\t\t\t// `getStaticProps()` or `getServerSideProps()`\n\t\t\t\t// with inactive Preview Mode where `cookies()`\n\t\t\t\t// does not work. We don't need to do any\n\t\t\t\t// preview handling.\n\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// We are probably in App Router (`app` directory).\n\n\t\t\t// We only return the cookie if a Prismic Preview session is active.\n\t\t\t//\n\t\t\t// An inactive cookie looks like this (URL encoded):\n\t\t\t// \t{\n\t\t\t// \t\t\"_tracker\": \"abc123\"\n\t\t\t// \t}\n\t\t\t//\n\t\t\t// An active cookie looks like this (URL encoded):\n\t\t\t// \t{\n\t\t\t// \t\t\"_tracker\": \"abc123\",\n\t\t\t// \t\t\"example-prismic-repo.prismic.io\": {\n\t\t\t// \t\t\tpreview: \"https://example-prismic-repo.prismic.io/previews/abc:123?websitePreviewId=xyz\"\n\t\t\t// \t\t}\n\t\t\t// \t}\n\t\t\tif (cookie && /\\.prismic\\.io/.test(cookie)) {\n\t\t\t\treturn cookie;\n\t\t\t}\n\t\t});\n\t}\n};\n"],"names":["prismic.cookie.preview"],"mappings":";;AAkDa,MAAA,qBAAqB,CACjC,WACS;AACL,MAAA,iBAAiB,UAAU,OAAO,aAAa;AAMjD,QAAA,OAAO,OAAO,gBAAgB,YAC9B,SAAS,OAAO,eAChB,OAAO,OAAO,YAAY,QAAQ,UACjC;AACD,aAAO,OAAO,oBAAoB,OAAO,YAAY,GAAG;AAAA,IACxD;AAAA,EACS,WAAA,SAAS,UAAU,OAAO,KAAK;AAGlC,WAAA,OAAO,0BAA0B,OAAO,GAAG;AAAA,EAAA,OAC5C;AAOC,WAAA,OAAO,oBAAoB,MAAK;;AAClC,UAAA;AACA,UAAA;AACH,kBAAS,aAAU,EAAA,IAAIA,OAAsB,MAApC,mBAAuC;AAAA,MAAA,QAC/C;AAOD;AAAA,MACA;AAkBD,UAAI,UAAU,gBAAgB,KAAK,MAAM,GAAG;AACpC,eAAA;AAAA,MACP;AAAA,IAAA,CACD;AAAA,EACD;AACF;"}
|
package/dist/exitPreview.cjs
CHANGED
package/dist/exitPreview.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exitPreview.cjs","sources":["../../src/exitPreview.ts"],"sourcesContent":["import
|
|
1
|
+
{"version":3,"file":"exitPreview.cjs","sources":["../../src/exitPreview.ts"],"sourcesContent":["import { NextResponse } from \"next/server\";\n\nimport { NextApiRequestLike, NextApiResponseLike } from \"./types\";\n\n/**\n * Configuration for `exitPreview`.\n */\nexport type ExitPreviewConfig = {\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 * **Only use this function in the Pages Directory (/pages).**\n *\n * Exits Next.js's Preview Mode from within a Next.js API route.\n */\nexport async function exitPreview(\n\tconfig: ExitPreviewConfig,\n): Promise<NextResponse | void> {\n\t// Exit the current user from Preview Mode.\n\tconfig.res.clearPreviewData();\n\n\t// 205 status is used to prevent CDN-level caching. The default 200\n\t// status code is typically treated as non-changing and cacheable.\n\tconfig.res.status(205).json({ success: true });\n}\n"],"names":[],"mappings":";;AA4BA,eAAsB,YACrB,QAAyB;AAGzB,SAAO,IAAI;AAIJ,SAAA,IAAI,OAAO,GAAG,EAAE,KAAK,EAAE,SAAS,MAAM;AAC9C;;"}
|
package/dist/exitPreview.d.ts
CHANGED
|
@@ -1,30 +1,25 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { NextResponse } from "next/server";
|
|
2
|
+
import { NextApiRequestLike, NextApiResponseLike } from "./types";
|
|
2
3
|
/**
|
|
3
4
|
* Configuration for `exitPreview`.
|
|
4
5
|
*/
|
|
5
6
|
export type ExitPreviewConfig = {
|
|
6
7
|
/**
|
|
7
|
-
* The `req` object from a Next.js API route.
|
|
8
|
-
* the API route.
|
|
8
|
+
* The `req` object from a Next.js API route.
|
|
9
9
|
*
|
|
10
|
-
* @see Next.js API route docs:
|
|
10
|
+
* @see Next.js API route docs: \<https://nextjs.org/docs/api-routes/introduction\>
|
|
11
11
|
*/
|
|
12
|
-
req:
|
|
13
|
-
headers: NextApiRequest["headers"];
|
|
14
|
-
};
|
|
12
|
+
req: NextApiRequestLike;
|
|
15
13
|
/**
|
|
16
|
-
* The `res` object from a Next.js API route.
|
|
17
|
-
* the API route.
|
|
14
|
+
* The `res` object from a Next.js API route.
|
|
18
15
|
*
|
|
19
|
-
* @see Next.js API route docs:
|
|
16
|
+
* @see Next.js API route docs: \<https://nextjs.org/docs/api-routes/introduction\>
|
|
20
17
|
*/
|
|
21
|
-
res:
|
|
22
|
-
clearPreviewData: NextApiResponse["clearPreviewData"];
|
|
23
|
-
status: NextApiResponse["status"];
|
|
24
|
-
json: NextApiResponse["json"];
|
|
25
|
-
};
|
|
18
|
+
res: NextApiResponseLike;
|
|
26
19
|
};
|
|
27
20
|
/**
|
|
21
|
+
* **Only use this function in the Pages Directory (/pages).**
|
|
22
|
+
*
|
|
28
23
|
* Exits Next.js's Preview Mode from within a Next.js API route.
|
|
29
24
|
*/
|
|
30
|
-
export declare function exitPreview(config: ExitPreviewConfig): void
|
|
25
|
+
export declare function exitPreview(config: ExitPreviewConfig): Promise<NextResponse | void>;
|
package/dist/exitPreview.js
CHANGED
package/dist/exitPreview.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exitPreview.js","sources":["../../src/exitPreview.ts"],"sourcesContent":["import
|
|
1
|
+
{"version":3,"file":"exitPreview.js","sources":["../../src/exitPreview.ts"],"sourcesContent":["import { NextResponse } from \"next/server\";\n\nimport { NextApiRequestLike, NextApiResponseLike } from \"./types\";\n\n/**\n * Configuration for `exitPreview`.\n */\nexport type ExitPreviewConfig = {\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 * **Only use this function in the Pages Directory (/pages).**\n *\n * Exits Next.js's Preview Mode from within a Next.js API route.\n */\nexport async function exitPreview(\n\tconfig: ExitPreviewConfig,\n): Promise<NextResponse | void> {\n\t// Exit the current user from Preview Mode.\n\tconfig.res.clearPreviewData();\n\n\t// 205 status is used to prevent CDN-level caching. The default 200\n\t// status code is typically treated as non-changing and cacheable.\n\tconfig.res.status(205).json({ success: true });\n}\n"],"names":[],"mappings":"AA4BA,eAAsB,YACrB,QAAyB;AAGzB,SAAO,IAAI;AAIJ,SAAA,IAAI,OAAO,GAAG,EAAE,KAAK,EAAE,SAAS,MAAM;AAC9C;"}
|
package/dist/package.json.cjs
CHANGED
package/dist/package.json.js
CHANGED
|
@@ -1,22 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const
|
|
4
|
-
return typeof query.documentId === "string" && typeof query.token === "string";
|
|
5
|
-
};
|
|
3
|
+
const navigation = require("next/navigation");
|
|
6
4
|
async function redirectToPreviewURL(config) {
|
|
7
|
-
const defaultURL = config.defaultURL || "/";
|
|
8
5
|
const basePath = config.basePath || "";
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
6
|
+
const request = "request" in config ? config.request : config.req;
|
|
7
|
+
config.client.enableAutoPreviewsFromReq(request);
|
|
8
|
+
const previewUrl = await config.client.resolvePreviewURL({
|
|
9
|
+
linkResolver: config.linkResolver,
|
|
10
|
+
defaultURL: config.defaultURL || "/"
|
|
11
|
+
});
|
|
12
|
+
if ("nextUrl" in request) {
|
|
13
|
+
navigation.redirect(basePath + previewUrl);
|
|
14
|
+
} else {
|
|
15
|
+
if (!("res" in config)) {
|
|
16
|
+
throw new Error("[redirectToPreviewURL] The `res` object from the API route must be provided to `redirectToPreviewURL()`.");
|
|
17
|
+
}
|
|
16
18
|
config.res.redirect(basePath + previewUrl);
|
|
17
19
|
return;
|
|
18
20
|
}
|
|
19
|
-
config.res.redirect(basePath + defaultURL);
|
|
20
21
|
}
|
|
21
22
|
exports.redirectToPreviewURL = redirectToPreviewURL;
|
|
22
23
|
//# sourceMappingURL=redirectToPreviewURL.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redirectToPreviewURL.cjs","sources":["../../src/redirectToPreviewURL.ts"],"sourcesContent":["import
|
|
1
|
+
{"version":3,"file":"redirectToPreviewURL.cjs","sources":["../../src/redirectToPreviewURL.ts"],"sourcesContent":["import { redirect } from \"next/navigation\";\nimport type * 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\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":["redirect"],"mappings":";;;AAgFA,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,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,37 +1,39 @@
|
|
|
1
|
-
import type { NextApiRequest, NextApiResponse } from "next";
|
|
2
1
|
import type * as prismic from "@prismicio/client";
|
|
2
|
+
import { NextApiRequestLike, NextApiResponseLike, NextRequestLike } from "./types";
|
|
3
3
|
/**
|
|
4
4
|
* Preview config for enabling previews with redirectToPreviewURL
|
|
5
5
|
*/
|
|
6
|
-
export type RedirectToPreviewURLConfig
|
|
6
|
+
export type RedirectToPreviewURLConfig = ({
|
|
7
7
|
/**
|
|
8
|
-
* The `
|
|
9
|
-
* the API route.
|
|
8
|
+
* The `request` object from a Next.js Route Handler.
|
|
10
9
|
*
|
|
11
|
-
* @see Next.js
|
|
10
|
+
* @see Next.js Route Handler docs: \<https://beta.nextjs.org/docs/routing/route-handlers\>
|
|
12
11
|
*/
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
};
|
|
12
|
+
request: NextRequestLike;
|
|
13
|
+
} | {
|
|
16
14
|
/**
|
|
17
|
-
* The `
|
|
18
|
-
* the API route.
|
|
15
|
+
* The `req` object from a Next.js API route.
|
|
19
16
|
*
|
|
20
|
-
* @see Next.js API route docs:
|
|
17
|
+
* @see Next.js API route docs: \<https://nextjs.org/docs/api-routes/introduction\>
|
|
21
18
|
*/
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
19
|
+
req: NextApiRequestLike;
|
|
20
|
+
/**
|
|
21
|
+
* The `res` object from a Next.js API route.
|
|
22
|
+
*
|
|
23
|
+
* @see Next.js API route docs: \<https://nextjs.org/docs/api-routes/introduction\>
|
|
24
|
+
*/
|
|
25
|
+
res: NextApiResponseLike;
|
|
26
|
+
}) & {
|
|
25
27
|
/**
|
|
26
28
|
* The Prismic client configured for the preview session's repository.
|
|
27
29
|
*/
|
|
28
|
-
client: Pick<prismic.Client, "resolvePreviewURL">;
|
|
30
|
+
client: Pick<prismic.Client, "enableAutoPreviewsFromReq" | "resolvePreviewURL">;
|
|
29
31
|
/**
|
|
30
32
|
* A Link Resolver used to resolve the previewed document's URL.
|
|
31
33
|
*
|
|
32
34
|
* @see To learn more about Link Resolver: {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver}
|
|
33
35
|
*/
|
|
34
|
-
linkResolver?:
|
|
36
|
+
linkResolver?: prismic.LinkResolverFunction;
|
|
35
37
|
/**
|
|
36
38
|
* The default redirect URL if a URL cannot be determined for the previewed
|
|
37
39
|
* document.
|
|
@@ -46,13 +48,13 @@ export type RedirectToPreviewURLConfig<TLinkResolverFunction extends prismic.Lin
|
|
|
46
48
|
* This option can be omitted if the app does not have a `basePath`.
|
|
47
49
|
*
|
|
48
50
|
* @remarks
|
|
49
|
-
* The API route is unable to detect the app's `basePath`
|
|
50
|
-
* must be provided to `redirectToPreviewURL()` manually.
|
|
51
|
+
* The Router Handler or API route is unable to detect the app's `basePath`
|
|
52
|
+
* automatically. It must be provided to `redirectToPreviewURL()` manually.
|
|
51
53
|
*/
|
|
52
54
|
basePath?: string;
|
|
53
55
|
};
|
|
54
56
|
/**
|
|
55
|
-
* Redirects a
|
|
56
|
-
* Next.js API route.
|
|
57
|
+
* Redirects a visitor to the URL of a previewed Prismic document from within a
|
|
58
|
+
* Next.js Route Handler or API route.
|
|
57
59
|
*/
|
|
58
|
-
export declare function redirectToPreviewURL
|
|
60
|
+
export declare function redirectToPreviewURL(config: RedirectToPreviewURLConfig): Promise<void>;
|
|
@@ -1,20 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
return typeof query.documentId === "string" && typeof query.token === "string";
|
|
3
|
-
};
|
|
1
|
+
import { redirect } from "next/navigation";
|
|
4
2
|
async function redirectToPreviewURL(config) {
|
|
5
|
-
const defaultURL = config.defaultURL || "/";
|
|
6
3
|
const basePath = config.basePath || "";
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
4
|
+
const request = "request" in config ? config.request : config.req;
|
|
5
|
+
config.client.enableAutoPreviewsFromReq(request);
|
|
6
|
+
const previewUrl = await config.client.resolvePreviewURL({
|
|
7
|
+
linkResolver: config.linkResolver,
|
|
8
|
+
defaultURL: config.defaultURL || "/"
|
|
9
|
+
});
|
|
10
|
+
if ("nextUrl" in request) {
|
|
11
|
+
redirect(basePath + previewUrl);
|
|
12
|
+
} else {
|
|
13
|
+
if (!("res" in config)) {
|
|
14
|
+
throw new Error("[redirectToPreviewURL] The `res` object from the API route must be provided to `redirectToPreviewURL()`.");
|
|
15
|
+
}
|
|
14
16
|
config.res.redirect(basePath + previewUrl);
|
|
15
17
|
return;
|
|
16
18
|
}
|
|
17
|
-
config.res.redirect(basePath + defaultURL);
|
|
18
19
|
}
|
|
19
20
|
export {
|
|
20
21
|
redirectToPreviewURL
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redirectToPreviewURL.js","sources":["../../src/redirectToPreviewURL.ts"],"sourcesContent":["import
|
|
1
|
+
{"version":3,"file":"redirectToPreviewURL.js","sources":["../../src/redirectToPreviewURL.ts"],"sourcesContent":["import { redirect } from \"next/navigation\";\nimport type * 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\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":[],"mappings":";AAgFA,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,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;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setPreviewData.cjs","sources":["../../src/setPreviewData.ts"],"sourcesContent":["import
|
|
1
|
+
{"version":3,"file":"setPreviewData.cjs","sources":["../../src/setPreviewData.ts"],"sourcesContent":["import * as prismic from \"@prismicio/client\";\n\nimport { NextApiRequestLike, NextApiResponseLike } from \"./types\";\n\n/**\n * Configuration for `setPreviewData`.\n */\nexport type SetPreviewDataConfig = {\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 * **Only use this function in the Pages Directory (/pages).**\n *\n * Set Prismic preview data for Next.js's Preview Mode.\n */\nexport function setPreviewData({ req, res }: SetPreviewDataConfig): void {\n\tconst ref = req.query.token || req.cookies[prismic.cookie.preview];\n\n\tif (ref) {\n\t\tres.setPreviewData({ ref });\n\t}\n}\n"],"names":["prismic.cookie.preview"],"mappings":";;;SA4BgB,eAAe,EAAE,KAAK,OAA2B;AAC1D,QAAA,MAAM,IAAI,MAAM,SAAS,IAAI,QAAQA,OAAAA,OAAsB;AAEjE,MAAI,KAAK;AACJ,QAAA,eAAe,EAAE,IAAA,CAAK;AAAA,EAC1B;AACF;;"}
|
package/dist/setPreviewData.d.ts
CHANGED
|
@@ -1,29 +1,24 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { NextApiRequestLike, NextApiResponseLike } from "./types";
|
|
2
2
|
/**
|
|
3
3
|
* Configuration for `setPreviewData`.
|
|
4
4
|
*/
|
|
5
5
|
export type SetPreviewDataConfig = {
|
|
6
6
|
/**
|
|
7
|
-
* The `req` object from a Next.js API route.
|
|
8
|
-
* the API route.
|
|
7
|
+
* The `req` object from a Next.js API route.
|
|
9
8
|
*
|
|
10
|
-
* @see Next.js API route docs:
|
|
9
|
+
* @see Next.js API route docs: \<https://nextjs.org/docs/api-routes/introduction\>
|
|
11
10
|
*/
|
|
12
|
-
req:
|
|
13
|
-
query: NextApiRequest["query"];
|
|
14
|
-
cookies: NextApiRequest["cookies"];
|
|
15
|
-
};
|
|
11
|
+
req: NextApiRequestLike;
|
|
16
12
|
/**
|
|
17
|
-
* The `res` object from a Next.js API route.
|
|
18
|
-
* the API route.
|
|
13
|
+
* The `res` object from a Next.js API route.
|
|
19
14
|
*
|
|
20
|
-
* @see Next.js API route docs:
|
|
15
|
+
* @see Next.js API route docs: \<https://nextjs.org/docs/api-routes/introduction\>
|
|
21
16
|
*/
|
|
22
|
-
res:
|
|
23
|
-
setPreviewData: NextApiResponse["setPreviewData"];
|
|
24
|
-
};
|
|
17
|
+
res: NextApiResponseLike;
|
|
25
18
|
};
|
|
26
19
|
/**
|
|
20
|
+
* **Only use this function in the Pages Directory (/pages).**
|
|
21
|
+
*
|
|
27
22
|
* Set Prismic preview data for Next.js's Preview Mode.
|
|
28
23
|
*/
|
|
29
24
|
export declare function setPreviewData({ req, res }: SetPreviewDataConfig): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setPreviewData.js","sources":["../../src/setPreviewData.ts"],"sourcesContent":["import
|
|
1
|
+
{"version":3,"file":"setPreviewData.js","sources":["../../src/setPreviewData.ts"],"sourcesContent":["import * as prismic from \"@prismicio/client\";\n\nimport { NextApiRequestLike, NextApiResponseLike } from \"./types\";\n\n/**\n * Configuration for `setPreviewData`.\n */\nexport type SetPreviewDataConfig = {\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 * **Only use this function in the Pages Directory (/pages).**\n *\n * Set Prismic preview data for Next.js's Preview Mode.\n */\nexport function setPreviewData({ req, res }: SetPreviewDataConfig): void {\n\tconst ref = req.query.token || req.cookies[prismic.cookie.preview];\n\n\tif (ref) {\n\t\tres.setPreviewData({ ref });\n\t}\n}\n"],"names":["prismic.cookie.preview"],"mappings":";SA4BgB,eAAe,EAAE,KAAK,OAA2B;AAC1D,QAAA,MAAM,IAAI,MAAM,SAAS,IAAI,QAAQA,OAAsB;AAEjE,MAAI,KAAK;AACJ,QAAA,eAAe,EAAE,IAAA,CAAK;AAAA,EAC1B;AACF;"}
|