@prismicio/svelte 3.0.0-pr.71.9e4af9f → 3.0.0-pr.72.7fa1f89
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.
|
@@ -23,9 +23,7 @@
|
|
|
23
23
|
|
|
24
24
|
const { repositoryName, routePrefix = "preview", routePrefixName = routePrefix }: Props = $props()
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
// `redirectToPreviewURL()`.
|
|
28
|
-
const toolbarSrc = $derived(`${getToolbarSrc(repositoryName)}&v=5`)
|
|
26
|
+
const toolbarSrc = $derived(getToolbarSrc(repositoryName))
|
|
29
27
|
|
|
30
28
|
/** Set to `true` when the next `beforeNavigate()` call should not prefix the route. */
|
|
31
29
|
let endingPreview = $state(false)
|
|
@@ -47,7 +45,6 @@
|
|
|
47
45
|
(event) => {
|
|
48
46
|
event.preventDefault()
|
|
49
47
|
|
|
50
|
-
document.cookie = "io.prismic.previewSession=; Path=/; Max-Age=0"
|
|
51
48
|
endingPreview = true
|
|
52
49
|
|
|
53
50
|
goto(
|
|
@@ -2,7 +2,7 @@ import * as prismic from "@prismicio/client";
|
|
|
2
2
|
import type { Cookies } from "@sveltejs/kit";
|
|
3
3
|
export type RedirectToPreviewURLConfig = {
|
|
4
4
|
/** The Prismic client configured for the preview session's repository. */
|
|
5
|
-
client: Pick<prismic.Client, "resolvePreviewURL"
|
|
5
|
+
client: Pick<prismic.Client, "resolvePreviewURL">;
|
|
6
6
|
/**
|
|
7
7
|
* The `request` object from a `+server` file.
|
|
8
8
|
*
|
|
@@ -18,59 +18,26 @@ import * as prismic from "@prismicio/client";
|
|
|
18
18
|
* ```
|
|
19
19
|
*/
|
|
20
20
|
export const redirectToPreviewURL = async (config) => {
|
|
21
|
-
const
|
|
22
|
-
const
|
|
23
|
-
const documentID = searchParams.get("documentId") ?? undefined;
|
|
21
|
+
const previewToken = new URL(config.request.url).searchParams.get("token") ?? undefined;
|
|
22
|
+
const documentID = new URL(config.request.url).searchParams.get("documentId") ?? undefined;
|
|
24
23
|
const routePrefix = config.routePrefix ?? "preview";
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
"/" +
|
|
31
|
-
routePrefix +
|
|
32
|
-
(await config.client.resolvePreviewURL({
|
|
33
|
-
previewToken,
|
|
34
|
-
documentID,
|
|
35
|
-
defaultURL: config.defaultURL || "/",
|
|
36
|
-
}));
|
|
24
|
+
const previewURL = await config.client.resolvePreviewURL({
|
|
25
|
+
previewToken,
|
|
26
|
+
documentID,
|
|
27
|
+
defaultURL: config.defaultURL || "/",
|
|
28
|
+
});
|
|
37
29
|
// Prevent a flash of non-preview content by setting the preview token
|
|
38
30
|
// on the initial page load.
|
|
39
31
|
if (previewToken) {
|
|
40
|
-
config.cookies.set(prismic.cookie.preview, previewToken,
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
config.cookies.set("io.prismic.previewSession", session, cookieOptions);
|
|
32
|
+
config.cookies.set(prismic.cookie.preview, previewToken, {
|
|
33
|
+
path: "/",
|
|
34
|
+
httpOnly: false,
|
|
35
|
+
});
|
|
45
36
|
}
|
|
46
37
|
return new Response(undefined, {
|
|
47
38
|
status: 307,
|
|
48
39
|
headers: {
|
|
49
|
-
Location:
|
|
40
|
+
Location: "/" + routePrefix + previewURL,
|
|
50
41
|
},
|
|
51
42
|
});
|
|
52
43
|
};
|
|
53
|
-
const cookieOptions = { path: "/", httpOnly: false };
|
|
54
|
-
/** Returns the session's current ref from the repository's public endpoint. */
|
|
55
|
-
const getSessionRef = async (documentAPIEndpoint, session) => {
|
|
56
|
-
const url = new URL(`/previews/token/${encodeURIComponent(session)}`, documentAPIEndpoint);
|
|
57
|
-
url.hostname = url.hostname.replace(".cdn.", ".");
|
|
58
|
-
const { ref } = await (await fetch(url)).json();
|
|
59
|
-
if (!ref) {
|
|
60
|
-
throw new prismic.PreviewTokenExpiredError("The preview session has ended.", url.href, undefined);
|
|
61
|
-
}
|
|
62
|
-
return ref;
|
|
63
|
-
};
|
|
64
|
-
/**
|
|
65
|
-
* Returns `path` if it is a path on this website. The path already includes SvelteKit's
|
|
66
|
-
* `paths.base`.
|
|
67
|
-
*/
|
|
68
|
-
const getSafePath = (path) => {
|
|
69
|
-
if (!path?.startsWith("/")) {
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
// Paths like `//evil.com` and `/\evil.com` resolve to another origin.
|
|
73
|
-
const origin = "https://placeholder.invalid";
|
|
74
|
-
const url = new URL(path, origin);
|
|
75
|
-
return url.origin === origin ? url.pathname + url.search + url.hash : undefined;
|
|
76
|
-
};
|
package/package.json
CHANGED
|
@@ -23,9 +23,7 @@
|
|
|
23
23
|
|
|
24
24
|
const { repositoryName, routePrefix = "preview", routePrefixName = routePrefix }: Props = $props()
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
// `redirectToPreviewURL()`.
|
|
28
|
-
const toolbarSrc = $derived(`${getToolbarSrc(repositoryName)}&v=5`)
|
|
26
|
+
const toolbarSrc = $derived(getToolbarSrc(repositoryName))
|
|
29
27
|
|
|
30
28
|
/** Set to `true` when the next `beforeNavigate()` call should not prefix the route. */
|
|
31
29
|
let endingPreview = $state(false)
|
|
@@ -47,7 +45,6 @@
|
|
|
47
45
|
(event) => {
|
|
48
46
|
event.preventDefault()
|
|
49
47
|
|
|
50
|
-
document.cookie = "io.prismic.previewSession=; Path=/; Max-Age=0"
|
|
51
48
|
endingPreview = true
|
|
52
49
|
|
|
53
50
|
goto(
|
|
@@ -6,7 +6,7 @@ export type RedirectToPreviewURLConfig = {
|
|
|
6
6
|
// `Pick` is used to use the smallest possible subset of
|
|
7
7
|
// `prismic.Client`. Doing this reduces the surface area for breaking
|
|
8
8
|
// type changes.
|
|
9
|
-
client: Pick<prismic.Client, "resolvePreviewURL"
|
|
9
|
+
client: Pick<prismic.Client, "resolvePreviewURL">
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* The `request` object from a `+server` file.
|
|
@@ -50,77 +50,29 @@ export type RedirectToPreviewURLConfig = {
|
|
|
50
50
|
export const redirectToPreviewURL = async (
|
|
51
51
|
config: RedirectToPreviewURLConfig,
|
|
52
52
|
): Promise<Response> => {
|
|
53
|
-
const
|
|
54
|
-
const
|
|
55
|
-
const documentID = searchParams.get("documentId") ?? undefined
|
|
53
|
+
const previewToken = new URL(config.request.url).searchParams.get("token") ?? undefined
|
|
54
|
+
const documentID = new URL(config.request.url).searchParams.get("documentId") ?? undefined
|
|
56
55
|
const routePrefix = config.routePrefix ?? "preview"
|
|
57
56
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
const path = session ? getSafePath(searchParams.get("path")) : undefined
|
|
64
|
-
const location =
|
|
65
|
-
path ??
|
|
66
|
-
"/" +
|
|
67
|
-
routePrefix +
|
|
68
|
-
(await config.client.resolvePreviewURL({
|
|
69
|
-
previewToken,
|
|
70
|
-
documentID,
|
|
71
|
-
defaultURL: config.defaultURL || "/",
|
|
72
|
-
}))
|
|
57
|
+
const previewURL = await config.client.resolvePreviewURL({
|
|
58
|
+
previewToken,
|
|
59
|
+
documentID,
|
|
60
|
+
defaultURL: config.defaultURL || "/",
|
|
61
|
+
})
|
|
73
62
|
|
|
74
63
|
// Prevent a flash of non-preview content by setting the preview token
|
|
75
64
|
// on the initial page load.
|
|
76
65
|
if (previewToken) {
|
|
77
|
-
config.cookies.set(prismic.cookie.preview, previewToken,
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
if (session) {
|
|
82
|
-
config.cookies.set("io.prismic.previewSession", session, cookieOptions)
|
|
66
|
+
config.cookies.set(prismic.cookie.preview, previewToken, {
|
|
67
|
+
path: "/",
|
|
68
|
+
httpOnly: false,
|
|
69
|
+
})
|
|
83
70
|
}
|
|
84
71
|
|
|
85
72
|
return new Response(undefined, {
|
|
86
73
|
status: 307,
|
|
87
74
|
headers: {
|
|
88
|
-
Location:
|
|
75
|
+
Location: "/" + routePrefix + previewURL,
|
|
89
76
|
},
|
|
90
77
|
})
|
|
91
78
|
}
|
|
92
|
-
|
|
93
|
-
const cookieOptions = { path: "/", httpOnly: false }
|
|
94
|
-
|
|
95
|
-
/** Returns the session's current ref from the repository's public endpoint. */
|
|
96
|
-
const getSessionRef = async (documentAPIEndpoint: string, session: string): Promise<string> => {
|
|
97
|
-
const url = new URL(`/previews/token/${encodeURIComponent(session)}`, documentAPIEndpoint)
|
|
98
|
-
url.hostname = url.hostname.replace(".cdn.", ".")
|
|
99
|
-
|
|
100
|
-
const { ref } = await (await fetch(url)).json()
|
|
101
|
-
if (!ref) {
|
|
102
|
-
throw new prismic.PreviewTokenExpiredError(
|
|
103
|
-
"The preview session has ended.",
|
|
104
|
-
url.href,
|
|
105
|
-
undefined,
|
|
106
|
-
)
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
return ref
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Returns `path` if it is a path on this website. The path already includes SvelteKit's
|
|
114
|
-
* `paths.base`.
|
|
115
|
-
*/
|
|
116
|
-
const getSafePath = (path: string | null): string | undefined => {
|
|
117
|
-
if (!path?.startsWith("/")) {
|
|
118
|
-
return
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// Paths like `//evil.com` and `/\evil.com` resolve to another origin.
|
|
122
|
-
const origin = "https://placeholder.invalid"
|
|
123
|
-
const url = new URL(path, origin)
|
|
124
|
-
|
|
125
|
-
return url.origin === origin ? url.pathname + url.search + url.hash : undefined
|
|
126
|
-
}
|