@prismicio/svelte 3.0.0-pr.71.dde7d8b → 3.0.0-pr.71.f7751fa
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.
- package/dist/kit/PrismicPreview.svelte +2 -19
- package/dist/kit/PrismicPreview.svelte.d.ts +0 -7
- package/dist/kit/redirectToPreviewURL.js +4 -3
- package/dist/lib/getSafePath.d.ts +4 -1
- package/dist/lib/getSafePath.js +15 -4
- package/package.json +3 -6
- package/src/kit/PrismicPreview.svelte +2 -19
- package/src/kit/redirectToPreviewURL.ts +4 -4
- package/src/lib/getSafePath.ts +14 -4
|
@@ -1,7 +1,5 @@
|
|
|
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"
|
|
5
3
|
import { getToolbarSrc } from "@prismicio/client"
|
|
6
4
|
import { onMount } from "svelte"
|
|
7
5
|
|
|
@@ -21,26 +19,11 @@
|
|
|
21
19
|
* Only set this value if the route parameter's name differs from the parameter's value.
|
|
22
20
|
*/
|
|
23
21
|
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
|
|
32
22
|
}
|
|
33
23
|
|
|
34
|
-
const {
|
|
35
|
-
repositoryName,
|
|
36
|
-
routePrefix = "preview",
|
|
37
|
-
routePrefixName = routePrefix,
|
|
38
|
-
previewURL = "/api/preview",
|
|
39
|
-
}: Props = $props()
|
|
24
|
+
const { repositoryName, routePrefix = "preview", routePrefixName = routePrefix }: Props = $props()
|
|
40
25
|
|
|
41
26
|
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)
|
|
44
27
|
|
|
45
28
|
/** Set to `true` when the next `beforeNavigate()` call should not prefix the route. */
|
|
46
29
|
let endingPreview = $state(false)
|
|
@@ -106,5 +89,5 @@
|
|
|
106
89
|
</script>
|
|
107
90
|
|
|
108
91
|
<svelte:head>
|
|
109
|
-
<script defer src={toolbarSrc}
|
|
92
|
+
<script defer src={toolbarSrc}></script>
|
|
110
93
|
</svelte:head>
|
|
@@ -12,13 +12,6 @@ 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;
|
|
22
15
|
};
|
|
23
16
|
declare const PrismicPreview: import("svelte").Component<Props, {}, "">;
|
|
24
17
|
type PrismicPreview = ReturnType<typeof PrismicPreview>;
|
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
-
/**
|
|
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;
|
package/dist/lib/getSafePath.js
CHANGED
|
@@ -1,11 +1,22 @@
|
|
|
1
|
-
/**
|
|
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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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.
|
|
3
|
+
"version": "3.0.0-pr.71.f7751fa",
|
|
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.
|
|
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.
|
|
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",
|
|
@@ -89,9 +89,6 @@
|
|
|
89
89
|
"optional": true
|
|
90
90
|
}
|
|
91
91
|
},
|
|
92
|
-
"overrides": {
|
|
93
|
-
"@prismicio/client": "$@prismicio/client"
|
|
94
|
-
},
|
|
95
92
|
"engines": {
|
|
96
93
|
"node": ">=22"
|
|
97
94
|
}
|
|
@@ -1,7 +1,5 @@
|
|
|
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"
|
|
5
3
|
import { getToolbarSrc } from "@prismicio/client"
|
|
6
4
|
import { onMount } from "svelte"
|
|
7
5
|
|
|
@@ -21,26 +19,11 @@
|
|
|
21
19
|
* Only set this value if the route parameter's name differs from the parameter's value.
|
|
22
20
|
*/
|
|
23
21
|
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
|
|
32
22
|
}
|
|
33
23
|
|
|
34
|
-
const {
|
|
35
|
-
repositoryName,
|
|
36
|
-
routePrefix = "preview",
|
|
37
|
-
routePrefixName = routePrefix,
|
|
38
|
-
previewURL = "/api/preview",
|
|
39
|
-
}: Props = $props()
|
|
24
|
+
const { repositoryName, routePrefix = "preview", routePrefixName = routePrefix }: Props = $props()
|
|
40
25
|
|
|
41
26
|
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)
|
|
44
27
|
|
|
45
28
|
/** Set to `true` when the next `beforeNavigate()` call should not prefix the route. */
|
|
46
29
|
let endingPreview = $state(false)
|
|
@@ -106,5 +89,5 @@
|
|
|
106
89
|
</script>
|
|
107
90
|
|
|
108
91
|
<svelte:head>
|
|
109
|
-
<script defer src={toolbarSrc}
|
|
92
|
+
<script defer src={toolbarSrc}></script>
|
|
110
93
|
</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
|
-
|
|
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
|
-
|
|
110
|
-
|
|
111
|
-
return pathname.split(/[/?#]/)[1] === routePrefix ? path : `${base}/${routePrefix}${pathname}`
|
|
111
|
+
return /^\/([^/?#]*)/.exec(rest)?.[1] === routePrefix ? path : `${base}/${routePrefix}${rest}`
|
|
112
112
|
}
|
package/src/lib/getSafePath.ts
CHANGED
|
@@ -1,12 +1,22 @@
|
|
|
1
|
-
/**
|
|
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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
}
|