@prismicio/svelte 3.0.0-pr.71.0f237fd → 3.0.0-pr.71.35fb12f

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,9 +19,20 @@
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
37
  const toolbarSrc = $derived(getToolbarSrc(repositoryName, { version: 5 }))
27
38
 
@@ -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,4 +1,6 @@
1
+ import { resolve } from "$app/paths";
1
2
  import * as prismic from "@prismicio/client";
3
+ import { getSafePath } from "../lib/getSafePath";
2
4
  /**
3
5
  * Redirects a visitor to the URL of a previewed Prismic document from within a SvelteKit `+server`
4
6
  * file.
@@ -21,11 +23,14 @@ export const redirectToPreviewURL = async (config) => {
21
23
  const previewToken = new URL(config.request.url).searchParams.get("token") ?? undefined;
22
24
  const documentID = new URL(config.request.url).searchParams.get("documentId") ?? undefined;
23
25
  const routePrefix = config.routePrefix ?? "preview";
24
- const previewURL = await config.client.resolvePreviewURL({
25
- previewToken,
26
- documentID,
27
- defaultURL: config.defaultURL || "/",
28
- });
26
+ const location = getSharedPath(config.request.url, routePrefix) ??
27
+ "/" +
28
+ routePrefix +
29
+ (await config.client.resolvePreviewURL({
30
+ previewToken,
31
+ documentID,
32
+ defaultURL: config.defaultURL || "/",
33
+ }));
29
34
  // Prevent a flash of non-preview content by setting the preview token
30
35
  // on the initial page load.
31
36
  if (previewToken) {
@@ -45,7 +50,22 @@ export const redirectToPreviewURL = async (config) => {
45
50
  return new Response(undefined, {
46
51
  status: 307,
47
52
  headers: {
48
- Location: "/" + routePrefix + previewURL,
53
+ Location: location,
49
54
  },
50
55
  });
51
56
  };
57
+ /**
58
+ * Returns the `path` param of toolbar share links inside the preview route prefix, or `undefined`
59
+ * if it is missing or not a path on this website.
60
+ */
61
+ const getSharedPath = (requestURL, routePrefix) => {
62
+ const path = getSafePath(new URL(requestURL).searchParams.get("path"));
63
+ // `path` comes from the browser, so it already includes `paths.base`. `resolve()` returns `base`
64
+ // either absolute or relative to the request.
65
+ const base = new URL(resolve("/", {}), requestURL).pathname.slice(0, -1);
66
+ if (!path?.startsWith(`${base}/`)) {
67
+ return;
68
+ }
69
+ const pathname = path.slice(base.length);
70
+ return pathname.split(/[/?#]/)[1] === routePrefix ? path : `${base}/${routePrefix}${pathname}`;
71
+ };
@@ -0,0 +1,2 @@
1
+ /** Returns `path` if it stays on the current origin, like `/blog?page=2#top`. */
2
+ export declare function getSafePath(path: string | null | undefined): string | undefined;
@@ -0,0 +1,11 @@
1
+ /** Returns `path` if it stays on the current origin, like `/blog?page=2#top`. */
2
+ export function getSafePath(path) {
3
+ if (!path?.startsWith("/") || path.startsWith("//") || path.startsWith("/\\")) {
4
+ return;
5
+ }
6
+ const origin = "https://placeholder.invalid";
7
+ const url = new URL(path, origin);
8
+ if (url.origin === origin) {
9
+ return url.href.slice(origin.length);
10
+ }
11
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/svelte",
3
- "version": "3.0.0-pr.71.0f237fd",
3
+ "version": "3.0.0-pr.71.35fb12f",
4
4
  "description": "Svelte components to present Prismic content.",
5
5
  "keywords": [
6
6
  "prismic",
@@ -72,8 +72,8 @@
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",
@@ -19,9 +19,20 @@
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
37
  const toolbarSrc = $derived(getToolbarSrc(repositoryName, { version: 5 }))
27
38
 
@@ -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,6 +1,9 @@
1
+ import { resolve } from "$app/paths"
1
2
  import * as prismic from "@prismicio/client"
2
3
  import type { Cookies } from "@sveltejs/kit"
3
4
 
5
+ import { getSafePath } from "../lib/getSafePath"
6
+
4
7
  export type RedirectToPreviewURLConfig = {
5
8
  /** The Prismic client configured for the preview session's repository. */
6
9
  // `Pick` is used to use the smallest possible subset of
@@ -54,11 +57,15 @@ export const redirectToPreviewURL = async (
54
57
  const documentID = new URL(config.request.url).searchParams.get("documentId") ?? undefined
55
58
  const routePrefix = config.routePrefix ?? "preview"
56
59
 
57
- const previewURL = await config.client.resolvePreviewURL({
58
- previewToken,
59
- documentID,
60
- defaultURL: config.defaultURL || "/",
61
- })
60
+ const location =
61
+ getSharedPath(config.request.url, routePrefix) ??
62
+ "/" +
63
+ routePrefix +
64
+ (await config.client.resolvePreviewURL({
65
+ previewToken,
66
+ documentID,
67
+ defaultURL: config.defaultURL || "/",
68
+ }))
62
69
 
63
70
  // Prevent a flash of non-preview content by setting the preview token
64
71
  // on the initial page load.
@@ -81,7 +88,25 @@ export const redirectToPreviewURL = async (
81
88
  return new Response(undefined, {
82
89
  status: 307,
83
90
  headers: {
84
- Location: "/" + routePrefix + previewURL,
91
+ Location: location,
85
92
  },
86
93
  })
87
94
  }
95
+
96
+ /**
97
+ * Returns the `path` param of toolbar share links inside the preview route prefix, or `undefined`
98
+ * if it is missing or not a path on this website.
99
+ */
100
+ const getSharedPath = (requestURL: string, routePrefix: string): string | undefined => {
101
+ const path = getSafePath(new URL(requestURL).searchParams.get("path"))
102
+ // `path` comes from the browser, so it already includes `paths.base`. `resolve()` returns `base`
103
+ // either absolute or relative to the request.
104
+ const base = new URL(resolve("/", {}), requestURL).pathname.slice(0, -1)
105
+ if (!path?.startsWith(`${base}/`)) {
106
+ return
107
+ }
108
+
109
+ const pathname = path.slice(base.length)
110
+
111
+ return pathname.split(/[/?#]/)[1] === routePrefix ? path : `${base}/${routePrefix}${pathname}`
112
+ }
@@ -0,0 +1,12 @@
1
+ /** Returns `path` if it stays on the current origin, like `/blog?page=2#top`. */
2
+ export function getSafePath(path: string | null | undefined): string | undefined {
3
+ if (!path?.startsWith("/") || path.startsWith("//") || path.startsWith("/\\")) {
4
+ return
5
+ }
6
+
7
+ const origin = "https://placeholder.invalid"
8
+ const url = new URL(path, origin)
9
+ if (url.origin === origin) {
10
+ return url.href.slice(origin.length)
11
+ }
12
+ }