@prismicio/svelte 3.0.0-pr.70.0fcf71f → 3.0.0-pr.71.08694b9

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.
@@ -19,11 +19,22 @@
19
19
  * Only set this value if the route parameter's name differs from the parameter's value.
20
20
  */
21
21
  routePrefixName?: string
22
+
23
+ /**
24
+ * The URL of your preview route, such as `/api/preview`. The Prismic Toolbar uses it to create
25
+ * share links, and hides its Share button without it.
26
+ */
27
+ previewURL?: string
22
28
  }
23
29
 
24
- const { repositoryName, routePrefix = "preview", routePrefixName = routePrefix }: Props = $props()
30
+ const {
31
+ repositoryName,
32
+ routePrefix = "preview",
33
+ routePrefixName = routePrefix,
34
+ previewURL,
35
+ }: Props = $props()
25
36
 
26
- const toolbarSrc = $derived(getToolbarSrc(repositoryName))
37
+ const toolbarSrc = $derived(getToolbarSrc(repositoryName, { version: 5 }))
27
38
 
28
39
  /** Set to `true` when the next `beforeNavigate()` call should not prefix the route. */
29
40
  let endingPreview = $state(false)
@@ -89,5 +100,5 @@
89
100
  </script>
90
101
 
91
102
  <svelte:head>
92
- <script defer src={toolbarSrc}></script>
103
+ <script defer src={toolbarSrc} data-preview-url={previewURL}></script>
93
104
  </svelte:head>
@@ -12,6 +12,11 @@ type Props = {
12
12
  * Only set this value if the route parameter's name differs from the parameter's value.
13
13
  */
14
14
  routePrefixName?: string;
15
+ /**
16
+ * The URL of your preview route, such as `/api/preview`. The Prismic Toolbar uses it to create
17
+ * share links, and hides its Share button without it.
18
+ */
19
+ previewURL?: string;
15
20
  };
16
21
  declare const PrismicPreview: import("svelte").Component<Props, {}, "">;
17
22
  type PrismicPreview = ReturnType<typeof PrismicPreview>;
@@ -1,3 +1,4 @@
1
+ import { resolve } from "$app/paths";
1
2
  import * as prismic from "@prismicio/client";
2
3
  /**
3
4
  * Redirects a visitor to the URL of a previewed Prismic document from within a SvelteKit `+server`
@@ -21,11 +22,14 @@ export const redirectToPreviewURL = async (config) => {
21
22
  const previewToken = new URL(config.request.url).searchParams.get("token") ?? undefined;
22
23
  const documentID = new URL(config.request.url).searchParams.get("documentId") ?? undefined;
23
24
  const routePrefix = config.routePrefix ?? "preview";
24
- const previewURL = await config.client.resolvePreviewURL({
25
- previewToken,
26
- documentID,
27
- defaultURL: config.defaultURL || "/",
28
- });
25
+ const location = getSharedPath(config.request.url, routePrefix) ??
26
+ "/" +
27
+ routePrefix +
28
+ (await config.client.resolvePreviewURL({
29
+ previewToken,
30
+ documentID,
31
+ defaultURL: config.defaultURL || "/",
32
+ }));
29
33
  // Prevent a flash of non-preview content by setting the preview token
30
34
  // on the initial page load.
31
35
  if (previewToken) {
@@ -34,10 +38,42 @@ export const redirectToPreviewURL = async (config) => {
34
38
  httpOnly: false,
35
39
  });
36
40
  }
41
+ // The toolbar's `v=5` flow follows the preview session with this cookie.
42
+ const session = new URL(config.request.url).searchParams.get("session");
43
+ if (session) {
44
+ config.cookies.set(prismic.cookie.previewSession, session, {
45
+ path: "/",
46
+ httpOnly: false,
47
+ });
48
+ }
37
49
  return new Response(undefined, {
38
50
  status: 307,
39
51
  headers: {
40
- Location: "/" + routePrefix + previewURL,
52
+ Location: location,
41
53
  },
42
54
  });
43
55
  };
56
+ /**
57
+ * Returns the `path` param of toolbar share links inside the preview route prefix, or `undefined`
58
+ * if it is missing or not a path on this website.
59
+ */
60
+ const getSharedPath = (requestURL, routePrefix) => {
61
+ const path = new URL(requestURL).searchParams.get("path");
62
+ if (!path?.startsWith("/")) {
63
+ return;
64
+ }
65
+ // Paths like `//evil.com` and `/\evil.com` resolve to another origin.
66
+ const origin = "https://placeholder.invalid";
67
+ const url = new URL(path, origin);
68
+ // `path` comes from the browser, so it already includes `paths.base`. `resolve()` returns `base`
69
+ // either absolute or relative to the request.
70
+ const base = new URL(resolve("/", {}), requestURL).pathname.slice(0, -1);
71
+ if (url.origin !== origin || !url.pathname.startsWith(`${base}/`)) {
72
+ return;
73
+ }
74
+ const pathname = url.pathname.slice(base.length);
75
+ if (pathname !== `/${routePrefix}` && !pathname.startsWith(`/${routePrefix}/`)) {
76
+ url.pathname = `${base}/${routePrefix}${pathname}`;
77
+ }
78
+ return url.pathname + url.search + url.hash;
79
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/svelte",
3
- "version": "3.0.0-pr.70.0fcf71f",
3
+ "version": "3.0.0-pr.71.08694b9",
4
4
  "description": "Svelte components to present Prismic content.",
5
5
  "keywords": [
6
6
  "prismic",
@@ -62,7 +62,7 @@
62
62
  "esm-env": "^1.2.2"
63
63
  },
64
64
  "devDependencies": {
65
- "@prismicio/client": "^7.22.3",
65
+ "@prismicio/client": "7.22.3-pr.477.9eb2102",
66
66
  "@prismicio/mock": "^0.7.2",
67
67
  "@sveltejs/kit": "^3.0.1",
68
68
  "@sveltejs/package": "^3.0.1",
@@ -72,15 +72,15 @@
72
72
  "@vitest/coverage-v8": "^5.0.5",
73
73
  "globals": "^17.13.0",
74
74
  "happy-dom": "^21.0.3",
75
- "oxfmt": "^0.73.0",
76
- "oxlint": "^1.88.0",
75
+ "oxfmt": "^0.74.0",
76
+ "oxlint": "^1.89.0",
77
77
  "standard-version": "^9.5.0",
78
78
  "svelte": "^5.57.2",
79
79
  "typescript": "^6.0.3",
80
80
  "vitest": "^5.0.5"
81
81
  },
82
82
  "peerDependencies": {
83
- "@prismicio/client": ">=7",
83
+ "@prismicio/client": "^7.23.0",
84
84
  "@sveltejs/kit": "^3.0.0",
85
85
  "svelte": "^5.16.0"
86
86
  },
@@ -89,6 +89,9 @@
89
89
  "optional": true
90
90
  }
91
91
  },
92
+ "overrides": {
93
+ "@prismicio/client": "$@prismicio/client"
94
+ },
92
95
  "engines": {
93
96
  "node": ">=22"
94
97
  }
@@ -19,11 +19,22 @@
19
19
  * Only set this value if the route parameter's name differs from the parameter's value.
20
20
  */
21
21
  routePrefixName?: string
22
+
23
+ /**
24
+ * The URL of your preview route, such as `/api/preview`. The Prismic Toolbar uses it to create
25
+ * share links, and hides its Share button without it.
26
+ */
27
+ previewURL?: string
22
28
  }
23
29
 
24
- const { repositoryName, routePrefix = "preview", routePrefixName = routePrefix }: Props = $props()
30
+ const {
31
+ repositoryName,
32
+ routePrefix = "preview",
33
+ routePrefixName = routePrefix,
34
+ previewURL,
35
+ }: Props = $props()
25
36
 
26
- const toolbarSrc = $derived(getToolbarSrc(repositoryName))
37
+ const toolbarSrc = $derived(getToolbarSrc(repositoryName, { version: 5 }))
27
38
 
28
39
  /** Set to `true` when the next `beforeNavigate()` call should not prefix the route. */
29
40
  let endingPreview = $state(false)
@@ -89,5 +100,5 @@
89
100
  </script>
90
101
 
91
102
  <svelte:head>
92
- <script defer src={toolbarSrc}></script>
103
+ <script defer src={toolbarSrc} data-preview-url={previewURL}></script>
93
104
  </svelte:head>
@@ -1,3 +1,4 @@
1
+ import { resolve } from "$app/paths"
1
2
  import * as prismic from "@prismicio/client"
2
3
  import type { Cookies } from "@sveltejs/kit"
3
4
 
@@ -54,11 +55,15 @@ export const redirectToPreviewURL = async (
54
55
  const documentID = new URL(config.request.url).searchParams.get("documentId") ?? undefined
55
56
  const routePrefix = config.routePrefix ?? "preview"
56
57
 
57
- const previewURL = await config.client.resolvePreviewURL({
58
- previewToken,
59
- documentID,
60
- defaultURL: config.defaultURL || "/",
61
- })
58
+ const location =
59
+ getSharedPath(config.request.url, routePrefix) ??
60
+ "/" +
61
+ routePrefix +
62
+ (await config.client.resolvePreviewURL({
63
+ previewToken,
64
+ documentID,
65
+ defaultURL: config.defaultURL || "/",
66
+ }))
62
67
 
63
68
  // Prevent a flash of non-preview content by setting the preview token
64
69
  // on the initial page load.
@@ -69,10 +74,47 @@ export const redirectToPreviewURL = async (
69
74
  })
70
75
  }
71
76
 
77
+ // The toolbar's `v=5` flow follows the preview session with this cookie.
78
+ const session = new URL(config.request.url).searchParams.get("session")
79
+ if (session) {
80
+ config.cookies.set(prismic.cookie.previewSession, session, {
81
+ path: "/",
82
+ httpOnly: false,
83
+ })
84
+ }
85
+
72
86
  return new Response(undefined, {
73
87
  status: 307,
74
88
  headers: {
75
- Location: "/" + routePrefix + previewURL,
89
+ Location: location,
76
90
  },
77
91
  })
78
92
  }
93
+
94
+ /**
95
+ * Returns the `path` param of toolbar share links inside the preview route prefix, or `undefined`
96
+ * if it is missing or not a path on this website.
97
+ */
98
+ const getSharedPath = (requestURL: string, routePrefix: string): string | undefined => {
99
+ const path = new URL(requestURL).searchParams.get("path")
100
+ if (!path?.startsWith("/")) {
101
+ return
102
+ }
103
+
104
+ // Paths like `//evil.com` and `/\evil.com` resolve to another origin.
105
+ const origin = "https://placeholder.invalid"
106
+ const url = new URL(path, origin)
107
+ // `path` comes from the browser, so it already includes `paths.base`. `resolve()` returns `base`
108
+ // either absolute or relative to the request.
109
+ const base = new URL(resolve("/", {}), requestURL).pathname.slice(0, -1)
110
+ if (url.origin !== origin || !url.pathname.startsWith(`${base}/`)) {
111
+ return
112
+ }
113
+
114
+ const pathname = url.pathname.slice(base.length)
115
+ if (pathname !== `/${routePrefix}` && !pathname.startsWith(`/${routePrefix}/`)) {
116
+ url.pathname = `${base}/${routePrefix}${pathname}`
117
+ }
118
+
119
+ return url.pathname + url.search + url.hash
120
+ }