@prismicio/svelte 3.1.0-canary.dc5542e → 3.1.0-pr.71.2f3ecf2

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.
@@ -23,7 +23,7 @@
23
23
 
24
24
  const { repositoryName, routePrefix = "preview", routePrefixName = routePrefix }: Props = $props()
25
25
 
26
- const toolbarSrc = $derived(getToolbarSrc(repositoryName))
26
+ const toolbarSrc = $derived(getToolbarSrc(repositoryName, { version: 5 }))
27
27
 
28
28
  /** Set to `true` when the next `beforeNavigate()` call should not prefix the route. */
29
29
  let endingPreview = $state(false)
@@ -1,4 +1,5 @@
1
1
  import * as prismic from "@prismicio/client";
2
+ import { getSafePath } from "../lib/getSafePath";
2
3
  /**
3
4
  * Redirects a visitor to the URL of a previewed Prismic document from within a SvelteKit `+server`
4
5
  * file.
@@ -21,11 +22,15 @@ 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
+ // Toolbar share links carry the shared page's `path`.
26
+ const location = getSafePath(new URL(config.request.url).searchParams.get("path")) ??
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) {
@@ -34,10 +39,18 @@ export const redirectToPreviewURL = async (config) => {
34
39
  httpOnly: false,
35
40
  });
36
41
  }
42
+ // The toolbar's `v=5` flow follows the preview session with this cookie.
43
+ const session = new URL(config.request.url).searchParams.get("session");
44
+ if (session) {
45
+ config.cookies.set(prismic.cookie.previewSession, session, {
46
+ path: "/",
47
+ httpOnly: false,
48
+ });
49
+ }
37
50
  return new Response(undefined, {
38
51
  status: 307,
39
52
  headers: {
40
- Location: "/" + routePrefix + previewURL,
53
+ Location: location,
41
54
  },
42
55
  });
43
56
  };
@@ -0,0 +1,5 @@
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
+ */
5
+ export declare function getSafePath(path: string | null | undefined): string | undefined;
@@ -0,0 +1,22 @@
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
+ */
5
+ export function getSafePath(path) {
6
+ if (!path?.startsWith("/") || path.startsWith("//") || path.startsWith("/\\")) {
7
+ return;
8
+ }
9
+ const origin = "https://placeholder.invalid";
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;
21
+ }
22
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/svelte",
3
- "version": "3.1.0-canary.dc5542e",
3
+ "version": "3.1.0-pr.71.2f3ecf2",
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.23.0",
66
66
  "@prismicio/mock": "^0.7.2",
67
67
  "@sveltejs/kit": "^3.1.0",
68
68
  "@sveltejs/package": "^3.0.1",
@@ -80,7 +80,7 @@
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
  },
@@ -23,7 +23,7 @@
23
23
 
24
24
  const { repositoryName, routePrefix = "preview", routePrefixName = routePrefix }: Props = $props()
25
25
 
26
- const toolbarSrc = $derived(getToolbarSrc(repositoryName))
26
+ const toolbarSrc = $derived(getToolbarSrc(repositoryName, { version: 5 }))
27
27
 
28
28
  /** Set to `true` when the next `beforeNavigate()` call should not prefix the route. */
29
29
  let endingPreview = $state(false)
@@ -1,6 +1,8 @@
1
1
  import * as prismic from "@prismicio/client"
2
2
  import type { Cookies } from "@sveltejs/kit"
3
3
 
4
+ import { getSafePath } from "../lib/getSafePath"
5
+
4
6
  export type RedirectToPreviewURLConfig = {
5
7
  /** The Prismic client configured for the preview session's repository. */
6
8
  // `Pick` is used to use the smallest possible subset of
@@ -54,11 +56,16 @@ export const redirectToPreviewURL = async (
54
56
  const documentID = new URL(config.request.url).searchParams.get("documentId") ?? undefined
55
57
  const routePrefix = config.routePrefix ?? "preview"
56
58
 
57
- const previewURL = await config.client.resolvePreviewURL({
58
- previewToken,
59
- documentID,
60
- defaultURL: config.defaultURL || "/",
61
- })
59
+ // Toolbar share links carry the shared page's `path`.
60
+ const location =
61
+ getSafePath(new URL(config.request.url).searchParams.get("path")) ??
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.
@@ -69,10 +76,19 @@ export const redirectToPreviewURL = async (
69
76
  })
70
77
  }
71
78
 
79
+ // The toolbar's `v=5` flow follows the preview session with this cookie.
80
+ const session = new URL(config.request.url).searchParams.get("session")
81
+ if (session) {
82
+ config.cookies.set(prismic.cookie.previewSession, session, {
83
+ path: "/",
84
+ httpOnly: false,
85
+ })
86
+ }
87
+
72
88
  return new Response(undefined, {
73
89
  status: 307,
74
90
  headers: {
75
- Location: "/" + routePrefix + previewURL,
91
+ Location: location,
76
92
  },
77
93
  })
78
94
  }
@@ -0,0 +1,22 @@
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
+ */
5
+ export function getSafePath(path: string | null | undefined): string | undefined {
6
+ if (!path?.startsWith("/") || path.startsWith("//") || path.startsWith("/\\")) {
7
+ return
8
+ }
9
+
10
+ const origin = "https://placeholder.invalid"
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
21
+ }
22
+ }