@prismicio/svelte 3.0.0-pr.71.35fb12f → 3.0.0-pr.71.88eae6d

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
 
@@ -21,8 +23,10 @@
21
23
  routePrefixName?: string
22
24
 
23
25
  /**
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
+ * The path of your preview route, without `paths.base`. The Prismic Toolbar uses it to create
27
+ * share links.
28
+ *
29
+ * @defaultValue `"/api/preview"`
26
30
  */
27
31
  previewURL?: string
28
32
  }
@@ -31,10 +35,12 @@
31
35
  repositoryName,
32
36
  routePrefix = "preview",
33
37
  routePrefixName = routePrefix,
34
- previewURL,
38
+ previewURL = "/api/preview",
35
39
  }: Props = $props()
36
40
 
37
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)
38
44
 
39
45
  /** Set to `true` when the next `beforeNavigate()` call should not prefix the route. */
40
46
  let endingPreview = $state(false)
@@ -100,5 +106,5 @@
100
106
  </script>
101
107
 
102
108
  <svelte:head>
103
- <script defer src={toolbarSrc} data-preview-url={previewURL}></script>
109
+ <script defer src={toolbarSrc} data-preview-url={previewPath}></script>
104
110
  </svelte:head>
@@ -13,8 +13,10 @@ type Props = {
13
13
  */
14
14
  routePrefixName?: string;
15
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.
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"`
18
20
  */
19
21
  previewURL?: string;
20
22
  };
@@ -63,9 +63,10 @@ const getSharedPath = (requestURL, routePrefix) => {
63
63
  // `path` comes from the browser, so it already includes `paths.base`. `resolve()` returns `base`
64
64
  // either absolute or relative to the request.
65
65
  const base = new URL(resolve("/", {}), requestURL).pathname.slice(0, -1);
66
- if (!path?.startsWith(`${base}/`)) {
66
+ const rest = path?.startsWith(base) ? path.slice(base.length) : undefined;
67
+ // `/docs` and `/docs?q=1` are under the `/docs` base, but `/docs-old` isn't.
68
+ if (rest === undefined || !/^([/?#]|$)/.test(rest)) {
67
69
  return;
68
70
  }
69
- const pathname = path.slice(base.length);
70
- return pathname.split(/[/?#]/)[1] === routePrefix ? path : `${base}/${routePrefix}${pathname}`;
71
+ return /^\/([^/?#]*)/.exec(rest)?.[1] === routePrefix ? path : `${base}/${routePrefix}${rest}`;
71
72
  };
@@ -1,2 +1,5 @@
1
- /** Returns `path` if it stays on the current origin, like `/blog?page=2#top`. */
1
+ /**
2
+ * Returns `path` if it stays on the current origin, like `/blog?page=2#top`. It comes from
3
+ * `location`, so it already includes `paths.base`.
4
+ */
2
5
  export declare function getSafePath(path: string | null | undefined): string | undefined;
@@ -1,11 +1,22 @@
1
- /** Returns `path` if it stays on the current origin, like `/blog?page=2#top`. */
1
+ /**
2
+ * Returns `path` if it stays on the current origin, like `/blog?page=2#top`. It comes from
3
+ * `location`, so it already includes `paths.base`.
4
+ */
2
5
  export function getSafePath(path) {
3
6
  if (!path?.startsWith("/") || path.startsWith("//") || path.startsWith("/\\")) {
4
7
  return;
5
8
  }
6
9
  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
+ let url;
11
+ try {
12
+ url = new URL(path, origin);
13
+ }
14
+ catch {
15
+ return;
16
+ }
17
+ const safePath = url.href.slice(origin.length);
18
+ // Normalizing can create `//`, like with `/.//example.com`.
19
+ if (url.origin === origin && !safePath.startsWith("//")) {
20
+ return safePath;
10
21
  }
11
22
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/svelte",
3
- "version": "3.0.0-pr.71.35fb12f",
3
+ "version": "3.0.0-pr.71.88eae6d",
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-pr.477.9eb2102",
65
+ "@prismicio/client": "^7.23.0",
66
66
  "@prismicio/mock": "^0.7.2",
67
67
  "@sveltejs/kit": "^3.0.1",
68
68
  "@sveltejs/package": "^3.0.1",
@@ -71,7 +71,7 @@
71
71
  "@types/node": "^26.6.5",
72
72
  "@vitest/coverage-v8": "^5.0.5",
73
73
  "globals": "^17.13.0",
74
- "happy-dom": "^21.0.3",
74
+ "happy-dom": "^21.0.4",
75
75
  "oxfmt": "^0.74.0",
76
76
  "oxlint": "^1.89.0",
77
77
  "standard-version": "^9.5.0",
@@ -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
 
@@ -21,8 +23,10 @@
21
23
  routePrefixName?: string
22
24
 
23
25
  /**
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
+ * The path of your preview route, without `paths.base`. The Prismic Toolbar uses it to create
27
+ * share links.
28
+ *
29
+ * @defaultValue `"/api/preview"`
26
30
  */
27
31
  previewURL?: string
28
32
  }
@@ -31,10 +35,12 @@
31
35
  repositoryName,
32
36
  routePrefix = "preview",
33
37
  routePrefixName = routePrefix,
34
- previewURL,
38
+ previewURL = "/api/preview",
35
39
  }: Props = $props()
36
40
 
37
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)
38
44
 
39
45
  /** Set to `true` when the next `beforeNavigate()` call should not prefix the route. */
40
46
  let endingPreview = $state(false)
@@ -100,5 +106,5 @@
100
106
  </script>
101
107
 
102
108
  <svelte:head>
103
- <script defer src={toolbarSrc} data-preview-url={previewURL}></script>
109
+ <script defer src={toolbarSrc} data-preview-url={previewPath}></script>
104
110
  </svelte:head>
@@ -102,11 +102,11 @@ const getSharedPath = (requestURL: string, routePrefix: string): string | undefi
102
102
  // `path` comes from the browser, so it already includes `paths.base`. `resolve()` returns `base`
103
103
  // either absolute or relative to the request.
104
104
  const base = new URL(resolve("/", {}), requestURL).pathname.slice(0, -1)
105
- if (!path?.startsWith(`${base}/`)) {
105
+ const rest = path?.startsWith(base) ? path.slice(base.length) : undefined
106
+ // `/docs` and `/docs?q=1` are under the `/docs` base, but `/docs-old` isn't.
107
+ if (rest === undefined || !/^([/?#]|$)/.test(rest)) {
106
108
  return
107
109
  }
108
110
 
109
- const pathname = path.slice(base.length)
110
-
111
- return pathname.split(/[/?#]/)[1] === routePrefix ? path : `${base}/${routePrefix}${pathname}`
111
+ return /^\/([^/?#]*)/.exec(rest)?.[1] === routePrefix ? path : `${base}/${routePrefix}${rest}`
112
112
  }
@@ -1,12 +1,22 @@
1
- /** Returns `path` if it stays on the current origin, like `/blog?page=2#top`. */
1
+ /**
2
+ * Returns `path` if it stays on the current origin, like `/blog?page=2#top`. It comes from
3
+ * `location`, so it already includes `paths.base`.
4
+ */
2
5
  export function getSafePath(path: string | null | undefined): string | undefined {
3
6
  if (!path?.startsWith("/") || path.startsWith("//") || path.startsWith("/\\")) {
4
7
  return
5
8
  }
6
9
 
7
10
  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
+ let url: URL
12
+ try {
13
+ url = new URL(path, origin)
14
+ } catch {
15
+ return
16
+ }
17
+ const safePath = url.href.slice(origin.length)
18
+ // Normalizing can create `//`, like with `/.//example.com`.
19
+ if (url.origin === origin && !safePath.startsWith("//")) {
20
+ return safePath
11
21
  }
12
22
  }