@prismicio/svelte 3.0.0-pr.71.61897e8 → 3.0.0-pr.71.dde7d8b

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.
@@ -1,5 +1,7 @@
1
1
  <script lang="ts">
2
2
  import { beforeNavigate, goto, refreshAll } from "$app/navigation"
3
+ import { resolve } from "$app/paths"
4
+ import { page } from "$app/state"
3
5
  import { getToolbarSrc } from "@prismicio/client"
4
6
  import { onMount } from "svelte"
5
7
 
@@ -19,13 +21,26 @@
19
21
  * Only set this value if the route parameter's name differs from the parameter's value.
20
22
  */
21
23
  routePrefixName?: string
24
+
25
+ /**
26
+ * The path of your preview route, without `paths.base`. The Prismic Toolbar uses it to create
27
+ * share links.
28
+ *
29
+ * @defaultValue `"/api/preview"`
30
+ */
31
+ previewURL?: string
22
32
  }
23
33
 
24
- const { repositoryName, routePrefix = "preview", routePrefixName = routePrefix }: Props = $props()
34
+ const {
35
+ repositoryName,
36
+ routePrefix = "preview",
37
+ routePrefixName = routePrefix,
38
+ previewURL = "/api/preview",
39
+ }: Props = $props()
25
40
 
26
- // `v=5` opts into the toolbar's modern flow.
27
- // TODO: Remove `&v=5` once `getToolbarSrc()` from `@prismicio/client` adds it.
28
- const toolbarSrc = $derived(`${getToolbarSrc(repositoryName)}&v=5`)
41
+ const toolbarSrc = $derived(getToolbarSrc(repositoryName, { version: 5 }))
42
+ // `resolve()` can return a path relative to the current page, which changes on navigation.
43
+ const previewPath = $derived(new URL(resolve(previewURL, {}), page.url).pathname)
29
44
 
30
45
  /** Set to `true` when the next `beforeNavigate()` call should not prefix the route. */
31
46
  let endingPreview = $state(false)
@@ -91,5 +106,5 @@
91
106
  </script>
92
107
 
93
108
  <svelte:head>
94
- <script defer src={toolbarSrc}></script>
109
+ <script defer src={toolbarSrc} data-preview-url={previewPath}></script>
95
110
  </svelte:head>
@@ -12,6 +12,13 @@ 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 path of your preview route, without `paths.base`. The Prismic Toolbar uses it to create
17
+ * share links.
18
+ *
19
+ * @defaultValue `"/api/preview"`
20
+ */
21
+ previewURL?: string;
15
22
  };
16
23
  declare const PrismicPreview: import("svelte").Component<Props, {}, "">;
17
24
  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) {
@@ -37,7 +42,7 @@ export const redirectToPreviewURL = async (config) => {
37
42
  // The toolbar's `v=5` flow follows the preview session with this cookie.
38
43
  const session = new URL(config.request.url).searchParams.get("session");
39
44
  if (session) {
40
- config.cookies.set("io.prismic.previewSession", session, {
45
+ config.cookies.set(prismic.cookie.previewSession, session, {
41
46
  path: "/",
42
47
  httpOnly: false,
43
48
  });
@@ -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.61897e8",
3
+ "version": "3.0.0-pr.71.dde7d8b",
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
  }
@@ -1,5 +1,7 @@
1
1
  <script lang="ts">
2
2
  import { beforeNavigate, goto, refreshAll } from "$app/navigation"
3
+ import { resolve } from "$app/paths"
4
+ import { page } from "$app/state"
3
5
  import { getToolbarSrc } from "@prismicio/client"
4
6
  import { onMount } from "svelte"
5
7
 
@@ -19,13 +21,26 @@
19
21
  * Only set this value if the route parameter's name differs from the parameter's value.
20
22
  */
21
23
  routePrefixName?: string
24
+
25
+ /**
26
+ * The path of your preview route, without `paths.base`. The Prismic Toolbar uses it to create
27
+ * share links.
28
+ *
29
+ * @defaultValue `"/api/preview"`
30
+ */
31
+ previewURL?: string
22
32
  }
23
33
 
24
- const { repositoryName, routePrefix = "preview", routePrefixName = routePrefix }: Props = $props()
34
+ const {
35
+ repositoryName,
36
+ routePrefix = "preview",
37
+ routePrefixName = routePrefix,
38
+ previewURL = "/api/preview",
39
+ }: Props = $props()
25
40
 
26
- // `v=5` opts into the toolbar's modern flow.
27
- // TODO: Remove `&v=5` once `getToolbarSrc()` from `@prismicio/client` adds it.
28
- const toolbarSrc = $derived(`${getToolbarSrc(repositoryName)}&v=5`)
41
+ const toolbarSrc = $derived(getToolbarSrc(repositoryName, { version: 5 }))
42
+ // `resolve()` can return a path relative to the current page, which changes on navigation.
43
+ const previewPath = $derived(new URL(resolve(previewURL, {}), page.url).pathname)
29
44
 
30
45
  /** Set to `true` when the next `beforeNavigate()` call should not prefix the route. */
31
46
  let endingPreview = $state(false)
@@ -91,5 +106,5 @@
91
106
  </script>
92
107
 
93
108
  <svelte:head>
94
- <script defer src={toolbarSrc}></script>
109
+ <script defer src={toolbarSrc} data-preview-url={previewPath}></script>
95
110
  </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.
@@ -72,7 +79,7 @@ export const redirectToPreviewURL = async (
72
79
  // The toolbar's `v=5` flow follows the preview session with this cookie.
73
80
  const session = new URL(config.request.url).searchParams.get("session")
74
81
  if (session) {
75
- config.cookies.set("io.prismic.previewSession", session, {
82
+ config.cookies.set(prismic.cookie.previewSession, session, {
76
83
  path: "/",
77
84
  httpOnly: false,
78
85
  })
@@ -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
+ }