@prismicio/svelte 3.0.0-pr.71.35fb12f → 3.0.0-pr.71.949ea7f

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
  };
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.949ea7f",
4
4
  "description": "Svelte components to present Prismic content.",
5
5
  "keywords": [
6
6
  "prismic",
@@ -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
  }