@prismicio/next 1.2.1 → 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 (45) hide show
  1. package/dist/PrismicNextImage.d.ts +1 -1
  2. package/dist/PrismicNextLink.d.ts +4 -4
  3. package/dist/PrismicPreview.cjs +3 -1
  4. package/dist/PrismicPreview.cjs.map +1 -1
  5. package/dist/PrismicPreview.d.ts +1 -1
  6. package/dist/PrismicPreview.js +3 -1
  7. package/dist/PrismicPreview.js.map +1 -1
  8. package/dist/PrismicPreviewClient.cjs +9 -13
  9. package/dist/PrismicPreviewClient.cjs.map +1 -1
  10. package/dist/PrismicPreviewClient.d.ts +4 -2
  11. package/dist/PrismicPreviewClient.js +9 -13
  12. package/dist/PrismicPreviewClient.js.map +1 -1
  13. package/dist/enableAutoPreviews.cjs +7 -1
  14. package/dist/enableAutoPreviews.cjs.map +1 -1
  15. package/dist/enableAutoPreviews.d.ts +1 -1
  16. package/dist/enableAutoPreviews.js +8 -2
  17. package/dist/enableAutoPreviews.js.map +1 -1
  18. package/dist/exitPreview.cjs +14 -2
  19. package/dist/exitPreview.cjs.map +1 -1
  20. package/dist/exitPreview.d.ts +37 -7
  21. package/dist/exitPreview.js +14 -2
  22. package/dist/exitPreview.js.map +1 -1
  23. package/dist/lib/getPreviewCookieRepositoryName.cjs +1 -1
  24. package/dist/lib/getPreviewCookieRepositoryName.cjs.map +1 -1
  25. package/dist/lib/getPreviewCookieRepositoryName.js +1 -1
  26. package/dist/lib/getPreviewCookieRepositoryName.js.map +1 -1
  27. package/dist/package.json.cjs +1 -1
  28. package/dist/package.json.js +1 -1
  29. package/dist/redirectToPreviewURL.d.ts +1 -1
  30. package/dist/setPreviewData.d.ts +1 -1
  31. package/dist/types.d.ts +6 -6
  32. package/package.json +3 -7
  33. package/src/PrismicPreview.tsx +8 -2
  34. package/src/PrismicPreviewClient.tsx +17 -20
  35. package/src/enableAutoPreviews.ts +18 -9
  36. package/src/exitPreview.ts +62 -13
  37. package/src/lib/getPreviewCookieRepositoryName.ts +1 -1
  38. package/src/types.ts +1 -1
  39. package/bin/prismic-next.js +0 -3
  40. package/dist/cli/index.d.ts +0 -1
  41. package/dist/cli.cjs +0 -123
  42. package/dist/cli.cjs.map +0 -1
  43. package/dist/cli.js +0 -104
  44. package/dist/cli.js.map +0 -1
  45. package/src/cli/index.ts +0 -168
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
- }