@prismicio/next 1.2.0 → 1.3.0-alpha.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.
Files changed (47) hide show
  1. package/dist/PrismicPreview.cjs +3 -1
  2. package/dist/PrismicPreview.cjs.map +1 -1
  3. package/dist/PrismicPreview.js +3 -1
  4. package/dist/PrismicPreview.js.map +1 -1
  5. package/dist/PrismicPreviewClient.cjs +9 -13
  6. package/dist/PrismicPreviewClient.cjs.map +1 -1
  7. package/dist/PrismicPreviewClient.d.ts +4 -2
  8. package/dist/PrismicPreviewClient.js +9 -13
  9. package/dist/PrismicPreviewClient.js.map +1 -1
  10. package/dist/_node_modules/@prismicio/client/dist/helpers/asLink.cjs +12 -3
  11. package/dist/_node_modules/@prismicio/client/dist/helpers/asLink.cjs.map +1 -1
  12. package/dist/_node_modules/@prismicio/client/dist/helpers/asLink.js +12 -3
  13. package/dist/_node_modules/@prismicio/client/dist/helpers/asLink.js.map +1 -1
  14. package/dist/_node_modules/@prismicio/client/dist/helpers/isFilled.cjs.map +1 -1
  15. package/dist/_node_modules/@prismicio/client/dist/helpers/isFilled.js.map +1 -1
  16. package/dist/enableAutoPreviews.cjs +11 -2
  17. package/dist/enableAutoPreviews.cjs.map +1 -1
  18. package/dist/enableAutoPreviews.js +12 -3
  19. package/dist/enableAutoPreviews.js.map +1 -1
  20. package/dist/exitPreview.cjs +14 -2
  21. package/dist/exitPreview.cjs.map +1 -1
  22. package/dist/exitPreview.d.ts +36 -6
  23. package/dist/exitPreview.js +14 -2
  24. package/dist/exitPreview.js.map +1 -1
  25. package/dist/index.d.ts +1 -1
  26. package/dist/lib/getPreviewCookieRepositoryName.cjs +1 -1
  27. package/dist/lib/getPreviewCookieRepositoryName.cjs.map +1 -1
  28. package/dist/lib/getPreviewCookieRepositoryName.js +1 -1
  29. package/dist/lib/getPreviewCookieRepositoryName.js.map +1 -1
  30. package/dist/package.json.cjs +1 -1
  31. package/dist/package.json.js +1 -1
  32. package/dist/types.d.ts +7 -1
  33. package/package.json +12 -10
  34. package/src/PrismicPreview.tsx +8 -2
  35. package/src/PrismicPreviewClient.tsx +17 -20
  36. package/src/enableAutoPreviews.ts +24 -15
  37. package/src/exitPreview.ts +62 -13
  38. package/src/index.ts +1 -1
  39. package/src/lib/getPreviewCookieRepositoryName.ts +1 -1
  40. package/src/types.ts +8 -1
  41. package/bin/prismic-next.js +0 -3
  42. package/dist/cli/index.d.ts +0 -1
  43. package/dist/cli.cjs +0 -123
  44. package/dist/cli.cjs.map +0 -1
  45. package/dist/cli.js +0 -104
  46. package/dist/cli.js.map +0 -1
  47. package/src/cli/index.ts +0 -168
package/dist/cli.js DELETED
@@ -1,104 +0,0 @@
1
- import mri from "mri";
2
- import * as path from "node:path";
3
- import * as fs from "node:fs/promises";
4
- import * as tty from "node:tty";
5
- import { Buffer } from "node:buffer";
6
- import { version } from "./package.json.js";
7
- async function pathExists(filePath) {
8
- try {
9
- await fs.access(filePath);
10
- return true;
11
- } catch {
12
- return false;
13
- }
14
- }
15
- function color(colorCode, string) {
16
- return tty.WriteStream.prototype.hasColors() ? "\x1B[" + colorCode + "m" + string + "\x1B[39m" : string;
17
- }
18
- function warn(string) {
19
- return console.warn(`${color(33, "warn")} - ${string}`);
20
- }
21
- function info(string) {
22
- return console.info(`${color(35, "info")} - ${string}`);
23
- }
24
- async function run(argv) {
25
- const args = mri(argv.slice(2), {
26
- boolean: ["help", "version"],
27
- alias: {
28
- help: "h",
29
- version: "v"
30
- },
31
- default: {
32
- help: false,
33
- version: false
34
- }
35
- });
36
- const command = args._[0];
37
- switch (command) {
38
- case "clear-cache": {
39
- warn("`prismic-next clear-cache` is an experimental utility. It may be replaced with a different solution in the future.");
40
- async function getAppRootDir() {
41
- let currentDir = process.cwd();
42
- while (!await pathExists(path.join(currentDir, ".next")) && !await pathExists(path.join(currentDir, "package.json"))) {
43
- if (currentDir === path.resolve("/")) {
44
- break;
45
- }
46
- currentDir = path.join(currentDir, "..");
47
- }
48
- if (await pathExists(path.join(currentDir, ".next")) || await pathExists(path.join(currentDir, "package.json"))) {
49
- return currentDir;
50
- }
51
- }
52
- const appRootDir = await getAppRootDir();
53
- if (!appRootDir) {
54
- warn("Could not find the Next.js app root. Run `prismic-next clear-cache` in a Next.js project with a `.next` directory or `package.json` file.");
55
- return;
56
- }
57
- const fetchCacheDir = path.join(appRootDir, ".next", "cache", "fetch-cache");
58
- if (!await pathExists(fetchCacheDir)) {
59
- info("No Next.js fetch cache directory found. You are good to go!");
60
- return;
61
- }
62
- const cacheEntries = await fs.readdir(fetchCacheDir);
63
- await Promise.all(cacheEntries.map(async (entry) => {
64
- try {
65
- const contents = await fs.readFile(path.join(fetchCacheDir, entry), "utf8");
66
- const payload = JSON.parse(contents);
67
- if (payload.kind !== "FETCH") {
68
- return;
69
- }
70
- const bodyPayload = JSON.parse(Buffer.from(payload.data.body, "base64").toString());
71
- if (/\.prismic\.io\/auth$/.test(bodyPayload.oauth_initiate)) {
72
- await fs.unlink(path.join(fetchCacheDir, entry));
73
- info(`Prismic /api/v2 request cache cleared: ${entry}`);
74
- }
75
- } catch (e) {
76
- }
77
- }));
78
- info("The Prismic request cache has been cleared. Uncached requests will begin on the next Next.js server start-up.");
79
- return;
80
- }
81
- default: {
82
- if (command && (!args.version || !args.help)) {
83
- warn("Invalid command.\n");
84
- }
85
- if (args.version) {
86
- console.info(version);
87
- return;
88
- }
89
- console.info(`
90
- Usage:
91
- prismic-next <command> [options...]
92
- Available commands:
93
- clear-cache
94
- Options:
95
- --help, -h Show help text
96
- --version, -v Show version
97
- `.trim());
98
- }
99
- }
100
- }
101
- export {
102
- run
103
- };
104
- //# sourceMappingURL=cli.js.map
package/dist/cli.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"cli.js","sources":["../../src/cli/index.ts"],"sourcesContent":["import mri from \"mri\";\nimport * as path from \"node:path\";\nimport * as fs from \"node:fs/promises\";\nimport * as tty from \"node:tty\";\nimport { Buffer } from \"node:buffer\";\n\nimport * as pkg from \"../../package.json\";\n\nasync function pathExists(filePath: string) {\n\ttry {\n\t\tawait fs.access(filePath);\n\n\t\treturn true;\n\t} catch {\n\t\treturn false;\n\t}\n}\n\nfunction color(colorCode: number, string: string) {\n\treturn tty.WriteStream.prototype.hasColors()\n\t\t? \"\\u001B[\" + colorCode + \"m\" + string + \"\\u001B[39m\"\n\t\t: string;\n}\n\nfunction warn(string: string) {\n\t// Yellow\n\treturn console.warn(`${color(33, \"warn\")} - ${string}`);\n}\n\nfunction info(string: string) {\n\t// Magenta\n\treturn console.info(`${color(35, \"info\")} - ${string}`);\n}\n\ntype Args = {\n\thelp: boolean;\n\tversion: boolean;\n};\n\nexport async function run(argv: string[]) {\n\tconst args = mri<Args>(argv.slice(2), {\n\t\tboolean: [\"help\", \"version\"],\n\t\talias: {\n\t\t\thelp: \"h\",\n\t\t\tversion: \"v\",\n\t\t},\n\t\tdefault: {\n\t\t\thelp: false,\n\t\t\tversion: false,\n\t\t},\n\t});\n\n\tconst command = args._[0];\n\n\tswitch (command) {\n\t\tcase \"clear-cache\": {\n\t\t\twarn(\n\t\t\t\t\"`prismic-next clear-cache` is an experimental utility. It may be replaced with a different solution in the future.\",\n\t\t\t);\n\n\t\t\tasync function getAppRootDir() {\n\t\t\t\tlet currentDir = process.cwd();\n\n\t\t\t\twhile (\n\t\t\t\t\t!(await pathExists(path.join(currentDir, \".next\"))) &&\n\t\t\t\t\t!(await pathExists(path.join(currentDir, \"package.json\")))\n\t\t\t\t) {\n\t\t\t\t\tif (currentDir === path.resolve(\"/\")) {\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\n\t\t\t\t\tcurrentDir = path.join(currentDir, \"..\");\n\t\t\t\t}\n\n\t\t\t\tif (\n\t\t\t\t\t(await pathExists(path.join(currentDir, \".next\"))) ||\n\t\t\t\t\t(await pathExists(path.join(currentDir, \"package.json\")))\n\t\t\t\t) {\n\t\t\t\t\treturn currentDir;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconst appRootDir = await getAppRootDir();\n\n\t\t\tif (!appRootDir) {\n\t\t\t\twarn(\n\t\t\t\t\t\"Could not find the Next.js app root. Run `prismic-next clear-cache` in a Next.js project with a `.next` directory or `package.json` file.\",\n\t\t\t\t);\n\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst fetchCacheDir = path.join(\n\t\t\t\tappRootDir,\n\t\t\t\t\".next\",\n\t\t\t\t\"cache\",\n\t\t\t\t\"fetch-cache\",\n\t\t\t);\n\n\t\t\tif (!(await pathExists(fetchCacheDir))) {\n\t\t\t\tinfo(\"No Next.js fetch cache directory found. You are good to go!\");\n\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst cacheEntries = await fs.readdir(fetchCacheDir);\n\n\t\t\tawait Promise.all(\n\t\t\t\tcacheEntries.map(async (entry) => {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst contents = await fs.readFile(\n\t\t\t\t\t\t\tpath.join(fetchCacheDir, entry),\n\t\t\t\t\t\t\t\"utf8\",\n\t\t\t\t\t\t);\n\t\t\t\t\t\tconst payload = JSON.parse(contents);\n\n\t\t\t\t\t\tif (payload.kind !== \"FETCH\") {\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst bodyPayload = JSON.parse(\n\t\t\t\t\t\t\tBuffer.from(payload.data.body, \"base64\").toString(),\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\t// Delete `/api/v2` requests.\n\t\t\t\t\t\tif (/\\.prismic\\.io\\/auth$/.test(bodyPayload.oauth_initiate)) {\n\t\t\t\t\t\t\tawait fs.unlink(path.join(fetchCacheDir, entry));\n\n\t\t\t\t\t\t\tinfo(`Prismic /api/v2 request cache cleared: ${entry}`);\n\t\t\t\t\t\t}\n\t\t\t\t\t} catch (e) {\n\t\t\t\t\t\t// noop\n\t\t\t\t\t}\n\t\t\t\t}),\n\t\t\t);\n\n\t\t\tinfo(\n\t\t\t\t\"The Prismic request cache has been cleared. Uncached requests will begin on the next Next.js server start-up.\",\n\t\t\t);\n\n\t\t\treturn;\n\t\t}\n\n\t\tdefault: {\n\t\t\tif (command && (!args.version || !args.help)) {\n\t\t\t\twarn(\"Invalid command.\\n\");\n\t\t\t}\n\n\t\t\tif (args.version) {\n\t\t\t\tconsole.info(pkg.version);\n\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconsole.info(\n\t\t\t\t`\nUsage:\n prismic-next <command> [options...]\nAvailable commands:\n clear-cache\nOptions:\n --help, -h Show help text\n --version, -v Show version\n`.trim(),\n\t\t\t);\n\t\t}\n\t}\n}\n"],"names":["pkg.version"],"mappings":";;;;;;AAQA,eAAe,WAAW,UAAgB;AACrC,MAAA;AACG,UAAA,GAAG,OAAO,QAAQ;AAEjB,WAAA;AAAA,EAAA,QACN;AACM,WAAA;AAAA,EACP;AACF;AAEA,SAAS,MAAM,WAAmB,QAAc;AACxC,SAAA,IAAI,YAAY,UAAU,cAC9B,UAAY,YAAY,MAAM,SAAS,aACvC;AACJ;AAEA,SAAS,KAAK,QAAc;AAE3B,SAAO,QAAQ,KAAK,GAAG,MAAM,IAAI,MAAM,QAAQ,QAAQ;AACxD;AAEA,SAAS,KAAK,QAAc;AAE3B,SAAO,QAAQ,KAAK,GAAG,MAAM,IAAI,MAAM,QAAQ,QAAQ;AACxD;AAOA,eAAsB,IAAI,MAAc;AACvC,QAAM,OAAO,IAAU,KAAK,MAAM,CAAC,GAAG;AAAA,IACrC,SAAS,CAAC,QAAQ,SAAS;AAAA,IAC3B,OAAO;AAAA,MACN,MAAM;AAAA,MACN,SAAS;AAAA,IACT;AAAA,IACD,SAAS;AAAA,MACR,MAAM;AAAA,MACN,SAAS;AAAA,IACT;AAAA,EAAA,CACD;AAEK,QAAA,UAAU,KAAK,EAAE,CAAC;AAExB,UAAQ,SAAS;AAAA,IAChB,KAAK,eAAe;AACnB,WACC,oHAAoH;AAGrH,qBAAe,gBAAa;AACvB,YAAA,aAAa,QAAQ;AA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C7C,aAAK,oBAAoB;AAAA,MACzB;AAED,UAAI,KAAK,SAAS;AACT,gBAAA,KAAKA,OAAW;AAExB;AAAA,MACA;AAED,cAAQ,KACP;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQF,KAAM,CAAA;AAAA,IAEL;AAAA,EACD;AACF;"}
package/src/cli/index.ts DELETED
@@ -1,168 +0,0 @@
1
- import mri from "mri";
2
- import * as path from "node:path";
3
- import * as fs from "node:fs/promises";
4
- import * as tty from "node:tty";
5
- import { Buffer } from "node:buffer";
6
-
7
- import * as pkg from "../../package.json";
8
-
9
- async function pathExists(filePath: string) {
10
- try {
11
- await fs.access(filePath);
12
-
13
- return true;
14
- } catch {
15
- return false;
16
- }
17
- }
18
-
19
- function color(colorCode: number, string: string) {
20
- return tty.WriteStream.prototype.hasColors()
21
- ? "\u001B[" + colorCode + "m" + string + "\u001B[39m"
22
- : string;
23
- }
24
-
25
- function warn(string: string) {
26
- // Yellow
27
- return console.warn(`${color(33, "warn")} - ${string}`);
28
- }
29
-
30
- function info(string: string) {
31
- // Magenta
32
- return console.info(`${color(35, "info")} - ${string}`);
33
- }
34
-
35
- type Args = {
36
- help: boolean;
37
- version: boolean;
38
- };
39
-
40
- export async function run(argv: string[]) {
41
- const args = mri<Args>(argv.slice(2), {
42
- boolean: ["help", "version"],
43
- alias: {
44
- help: "h",
45
- version: "v",
46
- },
47
- default: {
48
- help: false,
49
- version: false,
50
- },
51
- });
52
-
53
- const command = args._[0];
54
-
55
- switch (command) {
56
- case "clear-cache": {
57
- warn(
58
- "`prismic-next clear-cache` is an experimental utility. It may be replaced with a different solution in the future.",
59
- );
60
-
61
- async function getAppRootDir() {
62
- let currentDir = process.cwd();
63
-
64
- while (
65
- !(await pathExists(path.join(currentDir, ".next"))) &&
66
- !(await pathExists(path.join(currentDir, "package.json")))
67
- ) {
68
- if (currentDir === path.resolve("/")) {
69
- break;
70
- }
71
-
72
- currentDir = path.join(currentDir, "..");
73
- }
74
-
75
- if (
76
- (await pathExists(path.join(currentDir, ".next"))) ||
77
- (await pathExists(path.join(currentDir, "package.json")))
78
- ) {
79
- return currentDir;
80
- }
81
- }
82
-
83
- const appRootDir = await getAppRootDir();
84
-
85
- if (!appRootDir) {
86
- warn(
87
- "Could not find the Next.js app root. Run `prismic-next clear-cache` in a Next.js project with a `.next` directory or `package.json` file.",
88
- );
89
-
90
- return;
91
- }
92
-
93
- const fetchCacheDir = path.join(
94
- appRootDir,
95
- ".next",
96
- "cache",
97
- "fetch-cache",
98
- );
99
-
100
- if (!(await pathExists(fetchCacheDir))) {
101
- info("No Next.js fetch cache directory found. You are good to go!");
102
-
103
- return;
104
- }
105
-
106
- const cacheEntries = await fs.readdir(fetchCacheDir);
107
-
108
- await Promise.all(
109
- cacheEntries.map(async (entry) => {
110
- try {
111
- const contents = await fs.readFile(
112
- path.join(fetchCacheDir, entry),
113
- "utf8",
114
- );
115
- const payload = JSON.parse(contents);
116
-
117
- if (payload.kind !== "FETCH") {
118
- return;
119
- }
120
-
121
- const bodyPayload = JSON.parse(
122
- Buffer.from(payload.data.body, "base64").toString(),
123
- );
124
-
125
- // Delete `/api/v2` requests.
126
- if (/\.prismic\.io\/auth$/.test(bodyPayload.oauth_initiate)) {
127
- await fs.unlink(path.join(fetchCacheDir, entry));
128
-
129
- info(`Prismic /api/v2 request cache cleared: ${entry}`);
130
- }
131
- } catch (e) {
132
- // noop
133
- }
134
- }),
135
- );
136
-
137
- info(
138
- "The Prismic request cache has been cleared. Uncached requests will begin on the next Next.js server start-up.",
139
- );
140
-
141
- return;
142
- }
143
-
144
- default: {
145
- if (command && (!args.version || !args.help)) {
146
- warn("Invalid command.\n");
147
- }
148
-
149
- if (args.version) {
150
- console.info(pkg.version);
151
-
152
- return;
153
- }
154
-
155
- console.info(
156
- `
157
- Usage:
158
- prismic-next <command> [options...]
159
- Available commands:
160
- clear-cache
161
- Options:
162
- --help, -h Show help text
163
- --version, -v Show version
164
- `.trim(),
165
- );
166
- }
167
- }
168
- }