@prismicio/svelte 3.0.0-pr.71.0f237fd → 3.0.0-pr.71.249fd8d
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,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
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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) {
|
|
@@ -45,7 +50,23 @@ export const redirectToPreviewURL = async (config) => {
|
|
|
45
50
|
return new Response(undefined, {
|
|
46
51
|
status: 307,
|
|
47
52
|
headers: {
|
|
48
|
-
Location:
|
|
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
|
+
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)) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
return /^\/([^/?#]*)/.exec(rest)?.[1] === routePrefix ? path : `${base}/${routePrefix}${rest}`;
|
|
72
|
+
};
|
|
@@ -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.0.0-pr.71.
|
|
3
|
+
"version": "3.0.0-pr.71.249fd8d",
|
|
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,9 +71,9 @@
|
|
|
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.
|
|
75
|
-
"oxfmt": "^0.
|
|
76
|
-
"oxlint": "^1.
|
|
74
|
+
"happy-dom": "^21.0.4",
|
|
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",
|
|
@@ -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,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
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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.
|
|
@@ -81,7 +88,25 @@ export const redirectToPreviewURL = async (
|
|
|
81
88
|
return new Response(undefined, {
|
|
82
89
|
status: 307,
|
|
83
90
|
headers: {
|
|
84
|
-
Location:
|
|
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
|
+
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)) {
|
|
108
|
+
return
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return /^\/([^/?#]*)/.exec(rest)?.[1] === routePrefix ? path : `${base}/${routePrefix}${rest}`
|
|
112
|
+
}
|
|
@@ -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
|
+
}
|