@uniformdev/canvas-sveltekit 20.48.1-alpha.28 → 20.48.1-alpha.48.sha-989c5a3792
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/hooks/createUniformHandle.d.ts +14 -4
- package/dist/hooks/createUniformHandle.js +15 -6
- package/dist/isr/createRevalidateHandler.js +2 -2
- package/dist/models.d.ts +10 -0
- package/dist/preview/createPreviewHandler.d.ts +1 -1
- package/dist/preview/createPreviewHandler.js +1 -1
- package/dist/preview/previewCookie.d.ts +14 -11
- package/dist/preview/previewCookie.js +58 -16
- package/dist/preview/previewHandlerGet.d.ts +6 -3
- package/dist/preview/previewHandlerGet.js +11 -6
- package/dist/preview/previewHandlerPost.d.ts +6 -3
- package/dist/preview/previewHandlerPost.js +5 -16
- package/dist/route/createRouteFetcher.d.ts +2 -0
- package/dist/route/createRouteFetcher.js +2 -1
- package/dist/route/createUniformLoad.d.ts +1 -0
- package/dist/route/createUniformLoad.js +6 -3
- package/package.json +25 -28
|
@@ -1,9 +1,15 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
1
|
+
import type { RequestEvent } from '@sveltejs/kit';
|
|
2
|
+
import type { Handle } from '@sveltejs/kit/hooks';
|
|
3
|
+
import { type UniformPreviewData } from '../models.js';
|
|
3
4
|
/**
|
|
4
5
|
* Options for creating the Uniform handle hook.
|
|
5
6
|
*/
|
|
6
7
|
export type CreateUniformHandleOptions = {
|
|
8
|
+
/**
|
|
9
|
+
* Returns the preview secret that signs the preview cookie. Must match the secret given to
|
|
10
|
+
* `createPreviewHandler`. When it returns an empty value, preview cookies are ignored.
|
|
11
|
+
*/
|
|
12
|
+
secret: () => string | undefined;
|
|
7
13
|
/**
|
|
8
14
|
* Optional callback when preview data is present.
|
|
9
15
|
* Useful for logging or additional setup.
|
|
@@ -14,15 +20,19 @@ export type CreateUniformHandleOptions = {
|
|
|
14
20
|
* Creates a SvelteKit handle hook that integrates Uniform preview mode.
|
|
15
21
|
*
|
|
16
22
|
* This hook:
|
|
17
|
-
* -
|
|
23
|
+
* - Verifies the signed preview cookie and makes its data available via `event.locals.uniformPreview`
|
|
18
24
|
* - Optionally calls a callback when in preview mode
|
|
19
25
|
*
|
|
26
|
+
* `createUniformLoad` reads preview state from `event.locals.uniformPreview`, so this hook is required
|
|
27
|
+
* for draft content and contextual editing.
|
|
28
|
+
*
|
|
20
29
|
* @example
|
|
21
30
|
* // In src/hooks.server.ts
|
|
22
31
|
* import { createUniformHandle } from '@uniformdev/canvas-sveltekit';
|
|
23
32
|
* import { sequence } from '@sveltejs/kit/hooks';
|
|
24
33
|
*
|
|
25
34
|
* const uniformHandle = createUniformHandle({
|
|
35
|
+
* secret: () => process.env.UNIFORM_PREVIEW_SECRET,
|
|
26
36
|
* onPreview: (event, previewData) => {
|
|
27
37
|
* console.log('Preview mode active:', previewData);
|
|
28
38
|
* },
|
|
@@ -38,4 +48,4 @@ export type CreateUniformHandleOptions = {
|
|
|
38
48
|
* }
|
|
39
49
|
* };
|
|
40
50
|
*/
|
|
41
|
-
export declare function createUniformHandle(options
|
|
51
|
+
export declare function createUniformHandle(options: CreateUniformHandleOptions): Handle;
|
|
@@ -1,17 +1,23 @@
|
|
|
1
|
+
import { UNIFORM_PREVIEW_COOKIE_NAME } from '../models.js';
|
|
1
2
|
import { getPreviewData } from '../preview/previewCookie.js';
|
|
3
|
+
let hasWarnedMissingSecret = false;
|
|
2
4
|
/**
|
|
3
5
|
* Creates a SvelteKit handle hook that integrates Uniform preview mode.
|
|
4
6
|
*
|
|
5
7
|
* This hook:
|
|
6
|
-
* -
|
|
8
|
+
* - Verifies the signed preview cookie and makes its data available via `event.locals.uniformPreview`
|
|
7
9
|
* - Optionally calls a callback when in preview mode
|
|
8
10
|
*
|
|
11
|
+
* `createUniformLoad` reads preview state from `event.locals.uniformPreview`, so this hook is required
|
|
12
|
+
* for draft content and contextual editing.
|
|
13
|
+
*
|
|
9
14
|
* @example
|
|
10
15
|
* // In src/hooks.server.ts
|
|
11
16
|
* import { createUniformHandle } from '@uniformdev/canvas-sveltekit';
|
|
12
17
|
* import { sequence } from '@sveltejs/kit/hooks';
|
|
13
18
|
*
|
|
14
19
|
* const uniformHandle = createUniformHandle({
|
|
20
|
+
* secret: () => process.env.UNIFORM_PREVIEW_SECRET,
|
|
15
21
|
* onPreview: (event, previewData) => {
|
|
16
22
|
* console.log('Preview mode active:', previewData);
|
|
17
23
|
* },
|
|
@@ -28,14 +34,17 @@ import { getPreviewData } from '../preview/previewCookie.js';
|
|
|
28
34
|
* };
|
|
29
35
|
*/
|
|
30
36
|
export function createUniformHandle(options) {
|
|
31
|
-
const { onPreview } = options
|
|
37
|
+
const { secret, onPreview } = options;
|
|
32
38
|
return async ({ event, resolve }) => {
|
|
33
|
-
|
|
34
|
-
|
|
39
|
+
const previewSecret = secret();
|
|
40
|
+
if (!previewSecret && event.cookies.get(UNIFORM_PREVIEW_COOKIE_NAME) && !hasWarnedMissingSecret) {
|
|
41
|
+
hasWarnedMissingSecret = true;
|
|
42
|
+
// eslint-disable-next-line no-console
|
|
43
|
+
console.warn('[canvas-sveltekit] Ignoring the Uniform preview cookie because no preview secret is configured.');
|
|
44
|
+
}
|
|
45
|
+
const previewData = previewSecret ? await getPreviewData(event.cookies, previewSecret) : null;
|
|
35
46
|
if (previewData) {
|
|
36
|
-
// Attach to locals for access in load functions
|
|
37
47
|
event.locals.uniformPreview = previewData;
|
|
38
|
-
// Call optional callback
|
|
39
48
|
await onPreview?.(event, previewData);
|
|
40
49
|
}
|
|
41
50
|
return resolve(event);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { error
|
|
1
|
+
import { error } from '@sveltejs/kit';
|
|
2
2
|
/**
|
|
3
3
|
* Creates a SvelteKit request handler for on-demand ISR revalidation.
|
|
4
4
|
*
|
|
@@ -41,7 +41,7 @@ export function createRevalidateHandler(options) {
|
|
|
41
41
|
// Note: On Vercel, actual ISR revalidation happens automatically
|
|
42
42
|
// This endpoint is primarily for logging/tracking purposes
|
|
43
43
|
// or for custom cache invalidation logic
|
|
44
|
-
return json({
|
|
44
|
+
return Response.json({
|
|
45
45
|
revalidated: true,
|
|
46
46
|
paths,
|
|
47
47
|
timestamp: Date.now(),
|
package/dist/models.d.ts
CHANGED
|
@@ -14,6 +14,8 @@ export type UniformPreviewData = {
|
|
|
14
14
|
compositionPath?: string;
|
|
15
15
|
/** The locale being previewed */
|
|
16
16
|
locale?: string;
|
|
17
|
+
/** The release being previewed. Route fetches use it unless request options set a release. */
|
|
18
|
+
releaseId?: string;
|
|
17
19
|
/** Whether this is a playground preview (component/pattern preview) */
|
|
18
20
|
isPlayground?: boolean;
|
|
19
21
|
/**
|
|
@@ -23,6 +25,14 @@ export type UniformPreviewData = {
|
|
|
23
25
|
*/
|
|
24
26
|
patternType?: Exclude<CompositionGetParameters['patternType'], 'all'>;
|
|
25
27
|
};
|
|
28
|
+
declare global {
|
|
29
|
+
namespace App {
|
|
30
|
+
interface Locals {
|
|
31
|
+
/** Verified preview data from the signed preview cookie, set by `createUniformHandle`. */
|
|
32
|
+
uniformPreview?: UniformPreviewData;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
26
36
|
/**
|
|
27
37
|
* The name of the cookie used to store preview data.
|
|
28
38
|
*/
|
|
@@ -22,7 +22,7 @@ export type CreatePreviewHandlerOptions = CreatePreviewHandlerGetOptions & Creat
|
|
|
22
22
|
* export const POST = handlers.POST;
|
|
23
23
|
* export const OPTIONS = handlers.OPTIONS;
|
|
24
24
|
*/
|
|
25
|
-
export declare function createPreviewHandler(options
|
|
25
|
+
export declare function createPreviewHandler(options: CreatePreviewHandlerOptions): {
|
|
26
26
|
GET: RequestHandler;
|
|
27
27
|
POST: RequestHandler;
|
|
28
28
|
OPTIONS: RequestHandler;
|
|
@@ -18,7 +18,7 @@ import { createPreviewHandlerPost } from './previewHandlerPost.js';
|
|
|
18
18
|
* export const OPTIONS = handlers.OPTIONS;
|
|
19
19
|
*/
|
|
20
20
|
export function createPreviewHandler(options) {
|
|
21
|
-
const { secret, resolveFullPath, playgroundPath, enhance } = options
|
|
21
|
+
const { secret, resolveFullPath, playgroundPath, enhance } = options;
|
|
22
22
|
const GET = createPreviewHandlerGet({ secret, resolveFullPath, playgroundPath });
|
|
23
23
|
const POST = createPreviewHandlerPost({ secret, enhance });
|
|
24
24
|
const OPTIONS = async () => {
|
|
@@ -1,33 +1,36 @@
|
|
|
1
1
|
import type { Cookies } from '@sveltejs/kit';
|
|
2
2
|
import { type UniformPreviewData } from '../models.js';
|
|
3
3
|
/**
|
|
4
|
-
* Serializes preview data to a cookie value.
|
|
4
|
+
* Serializes preview data to a cookie value signed with the preview secret.
|
|
5
|
+
* Throws when `secret` is empty, because an unsigned cookie would let anyone enable preview.
|
|
5
6
|
*/
|
|
6
|
-
export declare function serializePreviewData(data: UniformPreviewData): string
|
|
7
|
+
export declare function serializePreviewData(data: UniformPreviewData, secret: string): Promise<string>;
|
|
7
8
|
/**
|
|
8
|
-
*
|
|
9
|
+
* Verifies a signed preview cookie value and returns its preview data.
|
|
10
|
+
* Returns null when `secret` is empty, the value is malformed, or the signature does not match.
|
|
9
11
|
*/
|
|
10
|
-
export declare function deserializePreviewData(value: string): UniformPreviewData | null
|
|
12
|
+
export declare function deserializePreviewData(value: string, secret: string): Promise<UniformPreviewData | null>;
|
|
11
13
|
/**
|
|
12
|
-
*
|
|
14
|
+
* Reads the preview cookie and returns its preview data if the signature matches `secret`, otherwise null.
|
|
13
15
|
*/
|
|
14
|
-
export declare function getPreviewData(cookies: Cookies): UniformPreviewData | null
|
|
16
|
+
export declare function getPreviewData(cookies: Cookies, secret: string): Promise<UniformPreviewData | null>;
|
|
15
17
|
/**
|
|
16
|
-
* Sets preview
|
|
18
|
+
* Sets the preview cookie, signed with `secret`. Throws when `secret` is empty.
|
|
17
19
|
*
|
|
18
20
|
* @param cookies - SvelteKit Cookies object
|
|
19
21
|
* @param data - Preview data to set
|
|
22
|
+
* @param secret - The preview secret used to sign the cookie
|
|
20
23
|
* @param options - Cookie options
|
|
21
24
|
*/
|
|
22
|
-
export declare function setPreviewData(cookies: Cookies, data: UniformPreviewData, options?: {
|
|
25
|
+
export declare function setPreviewData(cookies: Cookies, data: UniformPreviewData, secret: string, options?: {
|
|
23
26
|
/** Max age in seconds. For contextual editing, use a short value (10s). */
|
|
24
27
|
maxAge?: number;
|
|
25
|
-
}): void
|
|
28
|
+
}): Promise<void>;
|
|
26
29
|
/**
|
|
27
30
|
* Clears preview data from cookies.
|
|
28
31
|
*/
|
|
29
32
|
export declare function clearPreviewData(cookies: Cookies): void;
|
|
30
33
|
/**
|
|
31
|
-
*
|
|
34
|
+
* Returns true when the request carries a preview cookie signed with `secret`.
|
|
32
35
|
*/
|
|
33
|
-
export declare function isPreviewMode(cookies: Cookies): boolean
|
|
36
|
+
export declare function isPreviewMode(cookies: Cookies, secret: string): Promise<boolean>;
|
|
@@ -1,41 +1,83 @@
|
|
|
1
1
|
import { UNIFORM_PREVIEW_COOKIE_NAME } from '../models.js';
|
|
2
|
+
const encoder = new TextEncoder();
|
|
3
|
+
const decoder = new TextDecoder();
|
|
4
|
+
function toBase64Url(bytes) {
|
|
5
|
+
let binary = '';
|
|
6
|
+
for (const byte of bytes) {
|
|
7
|
+
binary += String.fromCharCode(byte);
|
|
8
|
+
}
|
|
9
|
+
return btoa(binary).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|
|
10
|
+
}
|
|
11
|
+
function fromBase64Url(value) {
|
|
12
|
+
const binary = atob(value.replace(/-/g, '+').replace(/_/g, '/'));
|
|
13
|
+
return Uint8Array.from(binary, (char) => char.charCodeAt(0));
|
|
14
|
+
}
|
|
2
15
|
/**
|
|
3
|
-
*
|
|
16
|
+
* Imports the preview secret as an HMAC-SHA256 key. Web Crypto keeps this usable on edge runtimes.
|
|
4
17
|
*/
|
|
5
|
-
|
|
6
|
-
return
|
|
18
|
+
function importSigningKey(secret) {
|
|
19
|
+
return crypto.subtle.importKey('raw', encoder.encode(secret), { name: 'HMAC', hash: 'SHA-256' }, false, [
|
|
20
|
+
'sign',
|
|
21
|
+
'verify',
|
|
22
|
+
]);
|
|
7
23
|
}
|
|
8
24
|
/**
|
|
9
|
-
*
|
|
25
|
+
* Serializes preview data to a cookie value signed with the preview secret.
|
|
26
|
+
* Throws when `secret` is empty, because an unsigned cookie would let anyone enable preview.
|
|
10
27
|
*/
|
|
11
|
-
export function
|
|
28
|
+
export async function serializePreviewData(data, secret) {
|
|
29
|
+
if (!secret) {
|
|
30
|
+
throw new Error('A preview secret is required to sign the Uniform preview cookie.');
|
|
31
|
+
}
|
|
32
|
+
const payload = toBase64Url(encoder.encode(JSON.stringify(data)));
|
|
33
|
+
const key = await importSigningKey(secret);
|
|
34
|
+
const signature = await crypto.subtle.sign('HMAC', key, encoder.encode(payload));
|
|
35
|
+
return `${payload}.${toBase64Url(new Uint8Array(signature))}`;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Verifies a signed preview cookie value and returns its preview data.
|
|
39
|
+
* Returns null when `secret` is empty, the value is malformed, or the signature does not match.
|
|
40
|
+
*/
|
|
41
|
+
export async function deserializePreviewData(value, secret) {
|
|
42
|
+
if (!secret) {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
const [payload, signature, ...rest] = value.split('.');
|
|
46
|
+
if (!payload || !signature || rest.length > 0) {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
12
49
|
try {
|
|
13
|
-
const
|
|
14
|
-
|
|
50
|
+
const key = await importSigningKey(secret);
|
|
51
|
+
const isValid = await crypto.subtle.verify('HMAC', key, fromBase64Url(signature), encoder.encode(payload));
|
|
52
|
+
if (!isValid) {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
return JSON.parse(decoder.decode(fromBase64Url(payload)));
|
|
15
56
|
}
|
|
16
57
|
catch {
|
|
17
58
|
return null;
|
|
18
59
|
}
|
|
19
60
|
}
|
|
20
61
|
/**
|
|
21
|
-
*
|
|
62
|
+
* Reads the preview cookie and returns its preview data if the signature matches `secret`, otherwise null.
|
|
22
63
|
*/
|
|
23
|
-
export function getPreviewData(cookies) {
|
|
64
|
+
export async function getPreviewData(cookies, secret) {
|
|
24
65
|
const cookie = cookies.get(UNIFORM_PREVIEW_COOKIE_NAME);
|
|
25
66
|
if (!cookie) {
|
|
26
67
|
return null;
|
|
27
68
|
}
|
|
28
|
-
return deserializePreviewData(cookie);
|
|
69
|
+
return deserializePreviewData(cookie, secret);
|
|
29
70
|
}
|
|
30
71
|
/**
|
|
31
|
-
* Sets preview
|
|
72
|
+
* Sets the preview cookie, signed with `secret`. Throws when `secret` is empty.
|
|
32
73
|
*
|
|
33
74
|
* @param cookies - SvelteKit Cookies object
|
|
34
75
|
* @param data - Preview data to set
|
|
76
|
+
* @param secret - The preview secret used to sign the cookie
|
|
35
77
|
* @param options - Cookie options
|
|
36
78
|
*/
|
|
37
|
-
export function setPreviewData(cookies, data, options) {
|
|
38
|
-
const value = serializePreviewData(data);
|
|
79
|
+
export async function setPreviewData(cookies, data, secret, options) {
|
|
80
|
+
const value = await serializePreviewData(data, secret);
|
|
39
81
|
cookies.set(UNIFORM_PREVIEW_COOKIE_NAME, value, {
|
|
40
82
|
path: '/',
|
|
41
83
|
httpOnly: true,
|
|
@@ -51,8 +93,8 @@ export function clearPreviewData(cookies) {
|
|
|
51
93
|
cookies.delete(UNIFORM_PREVIEW_COOKIE_NAME, { path: '/' });
|
|
52
94
|
}
|
|
53
95
|
/**
|
|
54
|
-
*
|
|
96
|
+
* Returns true when the request carries a preview cookie signed with `secret`.
|
|
55
97
|
*/
|
|
56
|
-
export function isPreviewMode(cookies) {
|
|
57
|
-
return cookies
|
|
98
|
+
export async function isPreviewMode(cookies, secret) {
|
|
99
|
+
return (await getPreviewData(cookies, secret)) !== null;
|
|
58
100
|
}
|
|
@@ -4,8 +4,11 @@ import { type ResolveFullPath } from '../models.js';
|
|
|
4
4
|
* Options for creating the preview GET handler.
|
|
5
5
|
*/
|
|
6
6
|
export type CreatePreviewHandlerGetOptions = {
|
|
7
|
-
/**
|
|
8
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Returns the preview secret. Requests must include it, and it signs the preview cookie.
|
|
9
|
+
* When it returns an empty value, preview cannot be enabled.
|
|
10
|
+
*/
|
|
11
|
+
secret: () => string | undefined;
|
|
9
12
|
/**
|
|
10
13
|
* Resolves the full path to redirect to.
|
|
11
14
|
* Defaults to returning `path` if truthy, otherwise returns `slug`.
|
|
@@ -26,4 +29,4 @@ export type CreatePreviewHandlerGetOptions = {
|
|
|
26
29
|
* playgroundPath: '/playground',
|
|
27
30
|
* });
|
|
28
31
|
*/
|
|
29
|
-
export declare function createPreviewHandlerGet(options
|
|
32
|
+
export declare function createPreviewHandlerGet(options: CreatePreviewHandlerGetOptions): RequestHandler;
|
|
@@ -35,7 +35,7 @@ function validateLocalRedirectUrl(pathToRedirectTo) {
|
|
|
35
35
|
* });
|
|
36
36
|
*/
|
|
37
37
|
export function createPreviewHandlerGet(options) {
|
|
38
|
-
const { secret, resolveFullPath = resolveFullPathDefault, playgroundPath } = options
|
|
38
|
+
const { secret, resolveFullPath = resolveFullPathDefault, playgroundPath } = options;
|
|
39
39
|
return async ({ url, cookies, request }) => {
|
|
40
40
|
const searchParams = url.searchParams;
|
|
41
41
|
// Config check endpoint
|
|
@@ -76,6 +76,7 @@ export function createPreviewHandlerGet(options) {
|
|
|
76
76
|
const slug = searchParams.get(compositionQueryParams.slug) ?? undefined;
|
|
77
77
|
const path = searchParams.get(compositionQueryParams.path) ?? undefined;
|
|
78
78
|
const locale = searchParams.get(compositionQueryParams.locale) ?? undefined;
|
|
79
|
+
const releaseId = searchParams.get('releaseId') || undefined;
|
|
79
80
|
if (typeof pathToRedirectTo === 'undefined') {
|
|
80
81
|
pathToRedirectTo = resolveFullPath({ id, slug, path, locale });
|
|
81
82
|
}
|
|
@@ -88,10 +89,13 @@ export function createPreviewHandlerGet(options) {
|
|
|
88
89
|
clearPreviewData(cookies);
|
|
89
90
|
redirect(307, pathToRedirectTo);
|
|
90
91
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
92
|
+
const previewSecret = secret();
|
|
93
|
+
if (!previewSecret) {
|
|
94
|
+
// eslint-disable-next-line no-console
|
|
95
|
+
console.warn('[canvas-sveltekit] Preview was requested, but no preview secret is configured.');
|
|
96
|
+
error(401, 'Preview is not configured');
|
|
97
|
+
}
|
|
98
|
+
if (searchParams.get(SECRET_QUERY_STRING_PARAM) !== previewSecret) {
|
|
95
99
|
error(401, 'Invalid token');
|
|
96
100
|
}
|
|
97
101
|
// Check if this is valid contextual editing
|
|
@@ -104,12 +108,13 @@ export function createPreviewHandlerGet(options) {
|
|
|
104
108
|
compositionSlug: slug,
|
|
105
109
|
compositionPath: path,
|
|
106
110
|
locale,
|
|
111
|
+
releaseId,
|
|
107
112
|
isPlayground,
|
|
108
113
|
patternType: !isPlayground
|
|
109
114
|
? undefined
|
|
110
115
|
: searchParams.get('patternType'),
|
|
111
116
|
};
|
|
112
|
-
setPreviewData(cookies, previewData, {
|
|
117
|
+
await setPreviewData(cookies, previewData, previewSecret, {
|
|
113
118
|
// Short max age for contextual editing to avoid stale preview state
|
|
114
119
|
maxAge: isUniformContextualEditing ? 10 : undefined,
|
|
115
120
|
});
|
|
@@ -4,8 +4,11 @@ import { type RootComponentInstance } from '@uniformdev/canvas';
|
|
|
4
4
|
* Options for creating the preview POST handler.
|
|
5
5
|
*/
|
|
6
6
|
export type CreatePreviewHandlerPostOptions = {
|
|
7
|
-
/**
|
|
8
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Returns the preview secret that validates the composition hash.
|
|
9
|
+
* When it returns an empty value, every request is rejected.
|
|
10
|
+
*/
|
|
11
|
+
secret: () => string | undefined;
|
|
9
12
|
/** Function to enhance the composition after receiving it. */
|
|
10
13
|
enhance?: (composition: RootComponentInstance, context: {
|
|
11
14
|
event: RequestEvent;
|
|
@@ -26,4 +29,4 @@ export type CreatePreviewHandlerPostOptions = {
|
|
|
26
29
|
* },
|
|
27
30
|
* });
|
|
28
31
|
*/
|
|
29
|
-
export declare function createPreviewHandlerPost(options
|
|
32
|
+
export declare function createPreviewHandlerPost(options: CreatePreviewHandlerPostOptions): RequestHandler;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { error
|
|
1
|
+
import { error } from '@sveltejs/kit';
|
|
2
2
|
import { generateHash } from '@uniformdev/canvas';
|
|
3
3
|
/**
|
|
4
4
|
* Creates a SvelteKit POST request handler for preview mode.
|
|
@@ -16,29 +16,18 @@ import { generateHash } from '@uniformdev/canvas';
|
|
|
16
16
|
* });
|
|
17
17
|
*/
|
|
18
18
|
export function createPreviewHandlerPost(options) {
|
|
19
|
-
const { secret, enhance } = options
|
|
19
|
+
const { secret, enhance } = options;
|
|
20
20
|
return async (event) => {
|
|
21
21
|
const body = (await event.request.json());
|
|
22
22
|
const { composition, hash } = body;
|
|
23
23
|
if (!composition) {
|
|
24
24
|
error(422, 'Missing "composition" parameter');
|
|
25
25
|
}
|
|
26
|
-
const previewSecret =
|
|
27
|
-
|
|
28
|
-
const hasConfiguredHash = Boolean(previewSecret);
|
|
29
|
-
if (hasProvidedHash && hasConfiguredHash) {
|
|
30
|
-
const calculatedHash = generateHash({
|
|
31
|
-
composition,
|
|
32
|
-
secret: previewSecret,
|
|
33
|
-
});
|
|
34
|
-
if (calculatedHash !== hash) {
|
|
35
|
-
error(401, 'Not authorized');
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
else if (hasConfiguredHash) {
|
|
26
|
+
const previewSecret = secret();
|
|
27
|
+
if (!previewSecret || !hash || generateHash({ composition, secret: previewSecret }) !== hash) {
|
|
39
28
|
error(401, 'Not authorized');
|
|
40
29
|
}
|
|
41
30
|
await enhance?.(composition, { event });
|
|
42
|
-
return json({ composition });
|
|
31
|
+
return Response.json({ composition });
|
|
43
32
|
};
|
|
44
33
|
}
|
|
@@ -13,6 +13,8 @@ export declare function createRouteFetcher<TContext, TResult, TSuccessResult>(op
|
|
|
13
13
|
resolvedUrl: string;
|
|
14
14
|
/** Default state (draft/published) for fetching */
|
|
15
15
|
defaultFetchState?: number | undefined;
|
|
16
|
+
/** The release from the verified preview cookie, used when request options do not set one */
|
|
17
|
+
previewReleaseId?: string;
|
|
16
18
|
};
|
|
17
19
|
defaultHandleNotFound: NonNullable<typeof options.handleNotFound>;
|
|
18
20
|
defaultHandleComposition: NonNullable<typeof options.handleComposition>;
|
|
@@ -15,7 +15,7 @@ export function createRouteFetcher(options) {
|
|
|
15
15
|
});
|
|
16
16
|
const handleModifyPath = modifyPath ?? ((path) => decodeURI(path));
|
|
17
17
|
return async function fetcher(context) {
|
|
18
|
-
const { contextualEditingCompositionId, contextualEditingPath, resolvedUrl, defaultFetchState: defaultPreviewState, } = parseContext(context);
|
|
18
|
+
const { contextualEditingCompositionId, contextualEditingPath, resolvedUrl, defaultFetchState: defaultPreviewState, previewReleaseId, } = parseContext(context);
|
|
19
19
|
let nodePath = prefix ? resolvedUrl.replace(new RegExp(`^${prefix}`), '') : resolvedUrl;
|
|
20
20
|
nodePath = handleModifyPath(nodePath, context);
|
|
21
21
|
const invokeRedirectResult = (redirect, duration) => (handleRedirect ?? defaultHandleRedirect)(nodePath, redirect, context, (response) => defaultHandleRedirect(nodePath, response, context, () => {
|
|
@@ -58,6 +58,7 @@ export function createRouteFetcher(options) {
|
|
|
58
58
|
dataSourceVariant: getDataSourceVariantFromRouteGetParams(resolvedOptions ?? {}, defaultPreviewState),
|
|
59
59
|
projectMapId: projectMapId,
|
|
60
60
|
path: nodePath,
|
|
61
|
+
releaseId: resolvedOptions?.releaseId ?? previewReleaseId,
|
|
61
62
|
});
|
|
62
63
|
const duration = Date.now() - time;
|
|
63
64
|
if (!silent) {
|
|
@@ -2,6 +2,7 @@ import type { ServerLoadEvent } from '@sveltejs/kit';
|
|
|
2
2
|
import type { CreateUniformLoadOptions, UniformLoadFoundResult, UniformLoadResult } from './types.js';
|
|
3
3
|
/**
|
|
4
4
|
* Creates a SvelteKit load function that fetches compositions from the Uniform Route API.
|
|
5
|
+
* Fetches draft content when `createUniformHandle` has verified a preview cookie for the request.
|
|
5
6
|
*
|
|
6
7
|
* @example
|
|
7
8
|
* // In +page.server.ts
|
|
@@ -2,7 +2,6 @@ import { error, redirect } from '@sveltejs/kit';
|
|
|
2
2
|
import { CANVAS_DRAFT_STATE } from '@uniformdev/canvas';
|
|
3
3
|
import { getTargetVariableExpandedUrl } from '@uniformdev/redirect';
|
|
4
4
|
import { resolvePathFromParams } from '../helpers/resolvePathFromParams.js';
|
|
5
|
-
import { getPreviewData } from '../preview/previewCookie.js';
|
|
6
5
|
import { createRouteFetcher } from './createRouteFetcher.js';
|
|
7
6
|
export function createUniformLoad(options) {
|
|
8
7
|
const defaultHandleRedirect = (requestUrl, matched, event) => {
|
|
@@ -11,7 +10,10 @@ export function createUniformLoad(options) {
|
|
|
11
10
|
}
|
|
12
11
|
const absoluteUrl = `${event.url.protocol}//${event.url.host}${requestUrl}`;
|
|
13
12
|
const destination = getTargetVariableExpandedUrl(absoluteUrl, matched.redirect);
|
|
14
|
-
|
|
13
|
+
// Redirect targets can leave the site. Kit 3 rejects those unless external is set.
|
|
14
|
+
redirect(matched.redirect.targetStatusCode, destination, {
|
|
15
|
+
external: true,
|
|
16
|
+
});
|
|
15
17
|
};
|
|
16
18
|
const defaultHandleNotFound = () => {
|
|
17
19
|
error(404, 'Not Found');
|
|
@@ -29,7 +31,7 @@ export function createUniformLoad(options) {
|
|
|
29
31
|
defaultHandleNotFound,
|
|
30
32
|
defaultHandleRedirect,
|
|
31
33
|
parseContext(event) {
|
|
32
|
-
const previewData =
|
|
34
|
+
const previewData = event.locals.uniformPreview;
|
|
33
35
|
return {
|
|
34
36
|
contextualEditingCompositionId: previewData?.isUniformContextualEditing
|
|
35
37
|
? previewData?.compositionId
|
|
@@ -43,6 +45,7 @@ export function createUniformLoad(options) {
|
|
|
43
45
|
}),
|
|
44
46
|
// Auto-engage draft content when in preview mode
|
|
45
47
|
defaultFetchState: previewData ? CANVAS_DRAFT_STATE : undefined,
|
|
48
|
+
previewReleaseId: previewData?.releaseId,
|
|
46
49
|
};
|
|
47
50
|
},
|
|
48
51
|
});
|
package/package.json
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uniformdev/canvas-sveltekit",
|
|
3
|
-
"version": "20.48.1-alpha.
|
|
3
|
+
"version": "20.48.1-alpha.48.sha-989c5a3792",
|
|
4
4
|
"description": "SvelteKit SDK for Uniform Canvas",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE.txt",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"dev": "vite dev",
|
|
8
|
-
"build": "
|
|
9
|
-
"preview": "vite preview",
|
|
10
|
-
"prepare": "svelte-kit sync || echo ''",
|
|
11
|
-
"prepack": "svelte-kit sync && svelte-package && publint",
|
|
8
|
+
"build": "svelte-kit sync && svelte-package && publint",
|
|
12
9
|
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
|
|
10
|
+
"lint": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
|
|
11
|
+
"test": "vitest run",
|
|
13
12
|
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch"
|
|
14
13
|
},
|
|
15
14
|
"files": [
|
|
16
15
|
"dist",
|
|
17
16
|
"!dist/**/*.test.*",
|
|
18
|
-
"!dist/**/*.spec.*"
|
|
17
|
+
"!dist/**/*.spec.*",
|
|
18
|
+
"!dist/**/__tests__/**"
|
|
19
19
|
],
|
|
20
20
|
"sideEffects": [
|
|
21
21
|
"**/*.css"
|
|
@@ -38,9 +38,9 @@
|
|
|
38
38
|
}
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
|
-
"@sveltejs/kit": "^
|
|
41
|
+
"@sveltejs/kit": "^3.0.0",
|
|
42
42
|
"@uniformdev/context-edge": ">=20.0.0",
|
|
43
|
-
"svelte": "^5.
|
|
43
|
+
"svelte": "^5.57.1"
|
|
44
44
|
},
|
|
45
45
|
"peerDependenciesMeta": {
|
|
46
46
|
"@uniformdev/context-edge": {
|
|
@@ -48,27 +48,24 @@
|
|
|
48
48
|
}
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"@uniformdev/canvas": "20.48.1-alpha.
|
|
52
|
-
"@uniformdev/canvas-svelte": "20.48.1-alpha.
|
|
53
|
-
"@uniformdev/
|
|
54
|
-
"@uniformdev/
|
|
55
|
-
"
|
|
56
|
-
"@uniformdev/richtext": "20.48.1-alpha.28+77388717bf",
|
|
57
|
-
"colorette": "2.0.20",
|
|
58
|
-
"uuid": "^13.0.0"
|
|
51
|
+
"@uniformdev/canvas": "20.48.1-alpha.48.sha-989c5a3792",
|
|
52
|
+
"@uniformdev/canvas-svelte": "20.48.1-alpha.48.sha-989c5a3792",
|
|
53
|
+
"@uniformdev/project-map": "20.48.1-alpha.48.sha-989c5a3792",
|
|
54
|
+
"@uniformdev/redirect": "20.48.1-alpha.48.sha-989c5a3792",
|
|
55
|
+
"colorette": "2.0.20"
|
|
59
56
|
},
|
|
60
57
|
"devDependencies": {
|
|
61
|
-
"@sveltejs/adapter-auto": "
|
|
62
|
-
"@sveltejs/adapter-vercel": "
|
|
63
|
-
"@sveltejs/kit": "
|
|
64
|
-
"@sveltejs/package": "
|
|
65
|
-
"@sveltejs/vite-plugin-svelte": "
|
|
66
|
-
"@uniformdev/context-edge": "20.48.1-alpha.
|
|
67
|
-
"publint": "
|
|
68
|
-
"svelte": "
|
|
69
|
-
"svelte-check": "
|
|
70
|
-
"typescript": "
|
|
71
|
-
"vite": "
|
|
58
|
+
"@sveltejs/adapter-auto": "8.0.0",
|
|
59
|
+
"@sveltejs/adapter-vercel": "7.0.1",
|
|
60
|
+
"@sveltejs/kit": "3.1.0",
|
|
61
|
+
"@sveltejs/package": "3.0.1",
|
|
62
|
+
"@sveltejs/vite-plugin-svelte": "7.3.1",
|
|
63
|
+
"@uniformdev/context-edge": "20.48.1-alpha.48.sha-989c5a3792",
|
|
64
|
+
"publint": "0.3.25",
|
|
65
|
+
"svelte": "5.57.3",
|
|
66
|
+
"svelte-check": "4.7.7",
|
|
67
|
+
"typescript": "6.0.3",
|
|
68
|
+
"vite": "8.3.5"
|
|
72
69
|
},
|
|
73
70
|
"keywords": [
|
|
74
71
|
"svelte",
|
|
@@ -79,5 +76,5 @@
|
|
|
79
76
|
"publishConfig": {
|
|
80
77
|
"access": "public"
|
|
81
78
|
},
|
|
82
|
-
"gitHead": "
|
|
79
|
+
"gitHead": "989c5a3792acdbac74cf9fa97ec4a60e0a1c3d0d"
|
|
83
80
|
}
|