@uniformdev/canvas-next-rsc 19.79.1-alpha.11 → 19.79.1-alpha.18

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/handler.mjs CHANGED
@@ -1,89 +1,150 @@
1
1
  // src/handler/createPreviewGETRouteHandler.ts
2
2
  import {
3
- CANVAS_DRAFT_STATE as CANVAS_DRAFT_STATE2,
4
3
  IN_CONTEXT_EDITOR_PLAYGROUND_QUERY_STRING_PARAM,
5
- IN_CONTEXT_EDITOR_QUERY_STRING_PARAM as IN_CONTEXT_EDITOR_QUERY_STRING_PARAM2
4
+ IN_CONTEXT_EDITOR_QUERY_STRING_PARAM,
5
+ isAllowedReferrer,
6
+ SECRET_QUERY_STRING_PARAM
6
7
  } from "@uniformdev/canvas";
7
- import { draftMode as draftMode2 } from "next/headers";
8
- import { redirect } from "next/navigation";
9
-
10
- // src/utils/route.ts
11
- import {
12
- CANVAS_DRAFT_STATE,
13
- CANVAS_EDITOR_STATE,
14
- CANVAS_PUBLISHED_STATE
15
- } from "@uniformdev/canvas";
16
- import { getBaseUrl, getServerConfig as getServerConfig2, resolvePath } from "@uniformdev/canvas-next-rsc-shared";
17
- import { getTargetVariableExpandedUrl } from "@uniformdev/redirect";
18
-
19
- // src/config/helpers.ts
20
- import { getServerConfig } from "@uniformdev/canvas-next-rsc-shared";
21
-
22
- // src/utils/draft.ts
23
- import { IN_CONTEXT_EDITOR_QUERY_STRING_PARAM } from "@uniformdev/canvas";
24
8
  import { draftMode } from "next/headers";
25
- var isDraftModeEnabled = ({
26
- searchParams
27
- }) => {
28
- if (isDevelopmentEnvironment()) {
29
- return isIncontextEditingEnabled({ searchParams });
30
- }
31
- let draftModeEnabled = false;
32
- try {
33
- draftModeEnabled = draftMode().isEnabled;
34
- } catch (e) {
9
+ import { redirect } from "next/navigation";
10
+ var BASE_URL_EXAMPLE = "https://example.com";
11
+ var getQueryParam = (req, paramName) => {
12
+ const value = req.nextUrl.searchParams.get(paramName);
13
+ if (typeof value === "undefined") {
14
+ return void 0;
35
15
  }
36
- return draftModeEnabled || isIncontextEditingEnabled({ searchParams });
37
- };
38
- var isIncontextEditingEnabled = ({
39
- searchParams
40
- }) => {
41
- const containsKey = typeof (searchParams == null ? void 0 : searchParams[IN_CONTEXT_EDITOR_QUERY_STRING_PARAM]) !== "undefined";
42
- return containsKey;
43
- };
44
- var isOnVercelPreviewEnvironment = () => {
45
- return process.env.VERCEL_ENV === "preview";
46
- };
47
- var isDevelopmentEnvironment = () => {
48
- return process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test";
16
+ return Array.isArray(value) ? value[0] : value;
49
17
  };
50
-
51
- // src/config/helpers.ts
52
- var shouldCacheBeDisabled = (options) => {
53
- if (isDraftModeEnabled(options)) {
54
- return { disabled: true, reason: "DRAFT" };
55
- }
56
- if (isIncontextEditingEnabled(options)) {
57
- return { disabled: true, reason: "INCONTEXT" };
58
- }
59
- if (isDevelopmentEnvironment()) {
60
- return { disabled: true, reason: "DEV" };
61
- }
62
- return {
63
- disabled: false,
64
- reason: void 0
18
+ var contextualEditingQueryParams = [
19
+ IN_CONTEXT_EDITOR_QUERY_STRING_PARAM,
20
+ IN_CONTEXT_EDITOR_PLAYGROUND_QUERY_STRING_PARAM
21
+ ];
22
+ var createPreviewGETRouteHandler = (options) => {
23
+ return async (request) => {
24
+ const isConfigCheck = getQueryParam(request, "is_config_check") === "true";
25
+ if (isConfigCheck) {
26
+ return Response.json(
27
+ {
28
+ hasPlayground: Boolean(options == null ? void 0 : options.playgroundPath),
29
+ isUsingCustomFullPathResolver: false
30
+ },
31
+ {
32
+ headers: {
33
+ "Access-Control-Allow-Origin": process.env.UNIFORM_CLI_BASE_URL || "https://uniform.app"
34
+ }
35
+ }
36
+ );
37
+ }
38
+ if (request.headers.get("sec-fetch-mode") === "no-cors") {
39
+ return new Response(null, { status: 204 });
40
+ }
41
+ if (!process.env.UNIFORM_PREVIEW_SECRET) {
42
+ return new Response("No preview secret is configured", { status: 401 });
43
+ }
44
+ const { searchParams } = new URL(request.url);
45
+ const isPlayground = searchParams.get(IN_CONTEXT_EDITOR_PLAYGROUND_QUERY_STRING_PARAM) === "true";
46
+ let pathToRedirectTo;
47
+ if (isPlayground) {
48
+ if (!(options == null ? void 0 : options.playgroundPath)) {
49
+ return new Response("No playground path is configured", { status: 401 });
50
+ }
51
+ pathToRedirectTo = options.playgroundPath;
52
+ }
53
+ const id = getQueryParam(request, compositionQueryParam.id);
54
+ const slug = getQueryParam(request, compositionQueryParam.slug);
55
+ const path = getQueryParam(request, compositionQueryParam.path);
56
+ const locale = getQueryParam(request, compositionQueryParam.locale);
57
+ const disable = getQueryParam(request, "disable");
58
+ const secret = getQueryParam(request, SECRET_QUERY_STRING_PARAM);
59
+ const referer = request.headers.get("referer");
60
+ const isUniformContextualEditing = getQueryParam(request, IN_CONTEXT_EDITOR_QUERY_STRING_PARAM) === "true" && isAllowedReferrer(referer || void 0);
61
+ if (typeof pathToRedirectTo === "undefined") {
62
+ const resolveFullPath = (options == null ? void 0 : options.resolveFullPath) || resolveFullPathDefault;
63
+ pathToRedirectTo = resolveFullPath({ id, slug, path, locale });
64
+ }
65
+ validateLocalRedirectUrl(pathToRedirectTo);
66
+ if (!pathToRedirectTo) {
67
+ return new Response("Could not resolve the full path of the preview page", { status: 400 });
68
+ }
69
+ if (disable) {
70
+ draftMode().disable();
71
+ redirect(pathToRedirectTo);
72
+ return;
73
+ }
74
+ if (secret !== process.env.UNIFORM_PREVIEW_SECRET) {
75
+ return new Response("Invalid preview secret", { status: 401 });
76
+ }
77
+ draftMode().enable();
78
+ const redirectionUrl = new URL(pathToRedirectTo, BASE_URL_EXAMPLE);
79
+ assignRequestQueryToSearchParams(redirectionUrl.searchParams, searchParams);
80
+ if (isPlayground) {
81
+ redirectionUrl.searchParams.set("id", searchParams.get("id") || "");
82
+ }
83
+ if (!isUniformContextualEditing) {
84
+ contextualEditingQueryParams.forEach((param) => {
85
+ redirectionUrl.searchParams.delete(param);
86
+ });
87
+ }
88
+ const fullPathToRedirectTo = redirectionUrl.href.replace(BASE_URL_EXAMPLE, "");
89
+ redirect(fullPathToRedirectTo);
65
90
  };
66
91
  };
67
- var getCanvasCacheStrategy = (options) => {
68
- const { disabled, reason } = shouldCacheBeDisabled(options);
69
- const config = getServerConfig();
70
- if (disabled) {
71
- if (reason === "DEV" && config.canvasCache) {
72
- console.warn("Canvas cache (disabled) has been overridden by canvasCache config.");
73
- return config.canvasCache;
92
+ function validateLocalRedirectUrl(pathToRedirectTo) {
93
+ if (pathToRedirectTo == null ? void 0 : pathToRedirectTo.match(/^[a-z]+:\/\//g)) {
94
+ throw new Error("Tried to redirect to absolute URL with protocol. Disallowing open redirect.");
95
+ }
96
+ }
97
+ var resolveFullPathDefault = ({ slug, path }) => {
98
+ return path || slug;
99
+ };
100
+ var compositionQueryParam = {
101
+ id: "id",
102
+ slug: "slug",
103
+ path: "path",
104
+ locale: "locale"
105
+ };
106
+ var assignRequestQueryToSearchParams = (searchParams, query) => {
107
+ const compositionQueryParamNames = Object.values(compositionQueryParam);
108
+ for (const [name, value] of query.entries()) {
109
+ if (name === SECRET_QUERY_STRING_PARAM) {
110
+ continue;
74
111
  }
75
- return {
76
- type: "no-cache"
77
- };
78
- }
79
- if (config.canvasCache) {
80
- return config.canvasCache;
112
+ if (compositionQueryParamNames.includes(name)) {
113
+ continue;
114
+ }
115
+ if (typeof value === "undefined") {
116
+ continue;
117
+ }
118
+ searchParams.append(name, value);
81
119
  }
82
- return {
83
- type: "force-cache"
120
+ };
121
+
122
+ // src/handler/createPreviewOPTIONSRouteHandler.ts
123
+ var createPreviewOPTIONSRouteHandler = () => {
124
+ return async () => {
125
+ return new Response(null, {
126
+ headers: {
127
+ "Access-Control-Allow-Origin": process.env.UNIFORM_CLI_BASE_URL || "https://uniform.app",
128
+ "Access-Control-Allow-Methods": "GET, POST, OPTIONS",
129
+ "Access-Control-Allow-Headers": "*"
130
+ }
131
+ });
84
132
  };
85
133
  };
86
134
 
135
+ // src/handler/createPreviewPOSTRouteHandler.ts
136
+ import { SECRET_QUERY_STRING_PARAM as SECRET_QUERY_STRING_PARAM2 } from "@uniformdev/canvas";
137
+ import { revalidateTag } from "next/cache";
138
+
139
+ // src/handler/helpers.ts
140
+ import { ApiClientError } from "@uniformdev/canvas";
141
+ import { getServerConfig } from "@uniformdev/canvas-next-rsc-shared";
142
+ import { parseConnectionString } from "@vercel/edge-config";
143
+ import { Webhook } from "svix";
144
+
145
+ // src/clients/canvasClient.ts
146
+ import { CanvasClient } from "@uniformdev/canvas";
147
+
87
148
  // src/env/index.ts
88
149
  var env = {
89
150
  getProjectId: () => {
@@ -125,11 +186,11 @@ var determineFetchCacheOptions = (mode) => {
125
186
  };
126
187
  };
127
188
 
128
- // src/clients/routeClient.ts
129
- import { RouteClient } from "@uniformdev/canvas";
130
- var getRouteClient = (options) => {
131
- const client = new RouteClient({
189
+ // src/clients/canvasClient.ts
190
+ var getCanvasClient = (options) => {
191
+ return new CanvasClient({
132
192
  projectId: env.getProjectId(),
193
+ apiHost: env.getApiHost(),
133
194
  apiKey: env.getApiKey(),
134
195
  edgeApiHost: env.getEdgeApiHost(),
135
196
  fetch: (req, init) => {
@@ -143,166 +204,36 @@ var getRouteClient = (options) => {
143
204
  }
144
205
  const tags = [];
145
206
  if (requestedUrl) {
146
- const pathKey = "path";
147
- const path = requestedUrl.searchParams.get(pathKey);
148
- if (path) {
149
- const pieces = path.split("/");
150
- for (let i = 0; i < pieces.length; i++) {
151
- const segmentPieces = pieces.slice(0, i + 1);
152
- const segment = segmentPieces.join("/");
153
- tags.push(buildPathTag(segment));
207
+ const compositionIdKey = "compositionId";
208
+ const compositionIdsKey = "compositionIDs";
209
+ const compositionId = requestedUrl.searchParams.get(compositionIdKey);
210
+ const compositionIds = requestedUrl.searchParams.get(compositionIdsKey);
211
+ if (compositionId) {
212
+ tags.push(buildCompositionTag(compositionId));
213
+ }
214
+ if (compositionIds) {
215
+ const ids = compositionIds.split(",");
216
+ for (let i = 0; i < ids.length; i++) {
217
+ tags.push(buildCompositionTag(ids[i]));
154
218
  }
155
219
  }
156
220
  }
157
221
  const { cache, revalidate } = determineFetchCacheOptions(options.cache);
158
222
  return fetch(req, {
159
223
  ...init,
224
+ cache,
160
225
  headers: {
161
226
  ...init == null ? void 0 : init.headers,
162
227
  "x-bypass-cache": "true"
163
228
  },
164
- cache,
165
229
  next: {
166
- revalidate,
167
- tags: tags.length ? tags : void 0
230
+ revalidate
168
231
  }
169
232
  });
170
233
  }
171
234
  });
172
- return client;
173
- };
174
- var getDefaultRouteClient = ({ searchParams }) => {
175
- return getRouteClient({
176
- cache: getCanvasCacheStrategy({
177
- searchParams
178
- })
179
- });
180
- };
181
-
182
- // src/utils/route.ts
183
- var retrieveRouteByPath = async ({
184
- path,
185
- state,
186
- searchParams
187
- }) => {
188
- var _a;
189
- const client = getDefaultRouteClient({
190
- searchParams
191
- });
192
- return await client.getRoute({
193
- path,
194
- state,
195
- withComponentIDs: true,
196
- withContentSourceMap: isOnVercelPreviewEnvironment() && ((_a = getServerConfig2().experimental) == null ? void 0 : _a.vercelVisualEditing)
197
- });
198
- };
199
- var resolveRedirectHref = (resolveResult, path) => {
200
- let href;
201
- if (resolveResult.redirect.targetProjectMapNodeId) {
202
- const requestUrl = `${getBaseUrl()}${path}`;
203
- const expandedUrl = getTargetVariableExpandedUrl(requestUrl, resolveResult.redirect);
204
- const url = new URL(expandedUrl);
205
- href = url.pathname;
206
- } else {
207
- href = resolveResult.redirect.targetUrl;
208
- }
209
- return href;
210
- };
211
-
212
- // src/handler/createPreviewGETRouteHandler.ts
213
- var getQueryParam = (req, paramName) => {
214
- const value = req.nextUrl.searchParams.get(paramName);
215
- if (typeof value === "undefined") {
216
- return void 0;
217
- }
218
- return Array.isArray(value) ? value[0] : value;
219
- };
220
- var createPreviewGETRouteHandler = (options) => {
221
- return async (request) => {
222
- const isConfigCheck = getQueryParam(request, "is_config_check") === "true";
223
- if (isConfigCheck) {
224
- return Response.json(
225
- {
226
- hasPlayground: Boolean(options == null ? void 0 : options.playgroundPath),
227
- isUsingCustomFullPathResolver: false
228
- },
229
- {
230
- headers: {
231
- "Access-Control-Allow-Origin": process.env.UNIFORM_CLI_BASE_URL || "https://uniform.app"
232
- }
233
- }
234
- );
235
- }
236
- if (request.headers.get("sec-fetch-mode") === "no-cors") {
237
- return new Response(null, { status: 204 });
238
- }
239
- if (!process.env.UNIFORM_PREVIEW_SECRET) {
240
- return new Response("No preview secret is configured", { status: 401 });
241
- }
242
- const { searchParams } = new URL(request.url);
243
- const secret = searchParams.get("secret");
244
- const path = searchParams.get("path");
245
- const isPlayground = searchParams.get(IN_CONTEXT_EDITOR_PLAYGROUND_QUERY_STRING_PARAM) === "true";
246
- const id = searchParams.get("id");
247
- if (secret !== process.env.UNIFORM_PREVIEW_SECRET) {
248
- return new Response("Invalid preview secret", { status: 401 });
249
- }
250
- if (!isPlayground) {
251
- if (!path) {
252
- return new Response("Path not provided", { status: 401 });
253
- }
254
- const resolveResult = await retrieveRouteByPath({
255
- path,
256
- state: CANVAS_DRAFT_STATE2,
257
- searchParams: {
258
- // about to be in draft mode so pretend for now.
259
- [IN_CONTEXT_EDITOR_QUERY_STRING_PARAM2]: "true"
260
- }
261
- });
262
- draftMode2().enable();
263
- if (resolveResult.type === "redirect") {
264
- const href = resolveRedirectHref(resolveResult, path);
265
- if (resolveResult.redirect.targetProjectMapNodeId) {
266
- redirect(`${href}?${IN_CONTEXT_EDITOR_QUERY_STRING_PARAM2}=true`);
267
- } else {
268
- redirect(href);
269
- }
270
- }
271
- if (resolveResult.type === "notFound") {
272
- return new Response("Invalid path", { status: 401 });
273
- }
274
- redirect(`${path}?${IN_CONTEXT_EDITOR_QUERY_STRING_PARAM2}=true`);
275
- } else {
276
- if (!(options == null ? void 0 : options.playgroundPath)) {
277
- return new Response("No playground path is configured", { status: 401 });
278
- }
279
- draftMode2().enable();
280
- redirect(`${options.playgroundPath}?id=${id}&${IN_CONTEXT_EDITOR_QUERY_STRING_PARAM2}=true`);
281
- }
282
- };
283
235
  };
284
236
 
285
- // src/handler/createPreviewOPTIONSRouteHandler.ts
286
- var createPreviewOPTIONSRouteHandler = () => {
287
- return async () => {
288
- return new Response(null, {
289
- headers: {
290
- "Access-Control-Allow-Origin": process.env.UNIFORM_CLI_BASE_URL || "https://uniform.app",
291
- "Access-Control-Allow-Methods": "GET, POST, OPTIONS",
292
- "Access-Control-Allow-Headers": "*"
293
- }
294
- });
295
- };
296
- };
297
-
298
- // src/handler/createPreviewPOSTRouteHandler.ts
299
- import { revalidateTag } from "next/cache";
300
-
301
- // src/handler/helpers.ts
302
- import { getServerConfig as getServerConfig3 } from "@uniformdev/canvas-next-rsc-shared";
303
- import { parseConnectionString } from "@vercel/edge-config";
304
- import { Webhook } from "svix";
305
-
306
237
  // src/clients/projectMapClient.ts
307
238
  import { ProjectMapClient } from "@uniformdev/project-map";
308
239
  var getProjectMapClient = (options) => {
@@ -328,6 +259,53 @@ var getProjectMapClient = (options) => {
328
259
  return manifestClient;
329
260
  };
330
261
 
262
+ // src/clients/routeClient.ts
263
+ import { RouteClient } from "@uniformdev/canvas";
264
+ var getRouteClient = (options) => {
265
+ const client = new RouteClient({
266
+ projectId: env.getProjectId(),
267
+ apiKey: env.getApiKey(),
268
+ edgeApiHost: env.getEdgeApiHost(),
269
+ fetch: (req, init) => {
270
+ let requestedUrl;
271
+ if (typeof req === "string") {
272
+ requestedUrl = new URL(req);
273
+ } else if (req instanceof URL) {
274
+ requestedUrl = req;
275
+ } else {
276
+ requestedUrl = new URL(req.url);
277
+ }
278
+ const tags = ["route"];
279
+ if (requestedUrl) {
280
+ const pathKey = "path";
281
+ const path = requestedUrl.searchParams.get(pathKey);
282
+ if (path) {
283
+ const pieces = path.split("/");
284
+ for (let i = 0; i < pieces.length; i++) {
285
+ const segmentPieces = pieces.slice(0, i + 1);
286
+ const segment = segmentPieces.join("/");
287
+ tags.push(buildPathTag(segment));
288
+ }
289
+ }
290
+ }
291
+ const { cache, revalidate } = determineFetchCacheOptions(options.cache);
292
+ return fetch(req, {
293
+ ...init,
294
+ headers: {
295
+ ...init == null ? void 0 : init.headers,
296
+ "x-bypass-cache": "true"
297
+ },
298
+ cache,
299
+ next: {
300
+ revalidate,
301
+ tags: tags.length ? tags : void 0
302
+ }
303
+ });
304
+ }
305
+ });
306
+ return client;
307
+ };
308
+
331
309
  // src/handler/helpers.ts
332
310
  var isSvixMessage = async (request) => {
333
311
  const requiredHeaders = ["svix-id", "svix-timestamp", "svix-signature"];
@@ -362,10 +340,16 @@ var processCompositionChange = async (compositionId) => {
362
340
  type: "no-cache"
363
341
  }
364
342
  });
365
- const { nodes } = await projectMapClient.getNodes({
366
- compositionId
367
- });
343
+ const [{ nodes }, composition] = await Promise.all([
344
+ projectMapClient.getNodes({
345
+ compositionId
346
+ }),
347
+ getComposition({ compositionId })
348
+ ]);
368
349
  const tags = [];
350
+ if (composition == null ? void 0 : composition.pattern) {
351
+ tags.push("route");
352
+ }
369
353
  if (nodes) {
370
354
  for (let i = 0; i < nodes.length; i++) {
371
355
  const node = nodes[i];
@@ -380,6 +364,24 @@ var processCompositionChange = async (compositionId) => {
380
364
  tags
381
365
  };
382
366
  };
367
+ var getComposition = async ({ compositionId }) => {
368
+ const canvasClient = getCanvasClient({
369
+ cache: {
370
+ type: "no-cache"
371
+ }
372
+ });
373
+ try {
374
+ const composition = await canvasClient.getCompositionById({
375
+ compositionId
376
+ });
377
+ return composition;
378
+ } catch (err) {
379
+ if (err instanceof ApiClientError && err.statusCode === 404) {
380
+ return null;
381
+ }
382
+ throw err;
383
+ }
384
+ };
383
385
  var buildProjectMapNodePaths = (path) => {
384
386
  const tags = [];
385
387
  const isDynamic = path.includes(":");
@@ -422,7 +424,7 @@ var processEdgeConfigChange = async ({ source_url }) => {
422
424
  console.warn("UNIFORM_VERCEL_EDGE_CONFIG_TOKEN is not set, skipping edge redirect upsert");
423
425
  return;
424
426
  }
425
- const config = getServerConfig3();
427
+ const config = getServerConfig();
426
428
  const routeClient = getRouteClient({
427
429
  cache: {
428
430
  type: "no-cache"
@@ -512,7 +514,7 @@ var handleManifestPublished = async (body) => {
512
514
  };
513
515
 
514
516
  // src/handler/messages/handleProjectMapNodeDelete.ts
515
- import { getServerConfig as getServerConfig4 } from "@uniformdev/canvas-next-rsc-shared";
517
+ import { getServerConfig as getServerConfig2 } from "@uniformdev/canvas-next-rsc-shared";
516
518
  import { ProjectMapNodeDeleteDefinition } from "@uniformdev/webhooks";
517
519
  var handleProjectMapNodeDelete = async (body) => {
518
520
  var _a;
@@ -522,7 +524,7 @@ var handleProjectMapNodeDelete = async (body) => {
522
524
  }
523
525
  const tags = [];
524
526
  tags.push(...buildProjectMapNodePaths(parsed.data.path));
525
- const config = getServerConfig4();
527
+ const config = getServerConfig2();
526
528
  if ((_a = config.experimental) == null ? void 0 : _a.edgeCompositions) {
527
529
  await processEdgeConfigChange({
528
530
  source_url: parsed.data.path
@@ -534,7 +536,7 @@ var handleProjectMapNodeDelete = async (body) => {
534
536
  };
535
537
 
536
538
  // src/handler/messages/handleProjectMapNodeInsert.ts
537
- import { getServerConfig as getServerConfig5 } from "@uniformdev/canvas-next-rsc-shared";
539
+ import { getServerConfig as getServerConfig3 } from "@uniformdev/canvas-next-rsc-shared";
538
540
  import { ProjectMapNodeInsertDefinition } from "@uniformdev/webhooks";
539
541
  var handleProjectMapNodeInsert = async (body) => {
540
542
  var _a;
@@ -544,7 +546,7 @@ var handleProjectMapNodeInsert = async (body) => {
544
546
  }
545
547
  const tags = [];
546
548
  tags.push(...buildProjectMapNodePaths(parsed.data.path));
547
- const config = getServerConfig5();
549
+ const config = getServerConfig3();
548
550
  if ((_a = config.experimental) == null ? void 0 : _a.edgeCompositions) {
549
551
  await processEdgeConfigChange({
550
552
  source_url: parsed.data.path
@@ -556,7 +558,7 @@ var handleProjectMapNodeInsert = async (body) => {
556
558
  };
557
559
 
558
560
  // src/handler/messages/handleProjectMapNodeUpdate.ts
559
- import { getServerConfig as getServerConfig6 } from "@uniformdev/canvas-next-rsc-shared";
561
+ import { getServerConfig as getServerConfig4 } from "@uniformdev/canvas-next-rsc-shared";
560
562
  import { ProjectMapNodeUpdateDefinition } from "@uniformdev/webhooks";
561
563
  var handleProjectMapNodeUpdate = async (body) => {
562
564
  var _a;
@@ -567,7 +569,7 @@ var handleProjectMapNodeUpdate = async (body) => {
567
569
  const tags = [];
568
570
  tags.push(...buildProjectMapNodePaths(parsed.data.path));
569
571
  tags.push(...buildProjectMapNodePaths(parsed.data.previous_path));
570
- const config = getServerConfig6();
572
+ const config = getServerConfig4();
571
573
  if ((_a = config.experimental) == null ? void 0 : _a.edgeCompositions) {
572
574
  await processEdgeConfigChange({
573
575
  source_url: parsed.data.path
@@ -579,7 +581,7 @@ var handleProjectMapNodeUpdate = async (body) => {
579
581
  };
580
582
 
581
583
  // src/handler/messages/handleRedirectDelete.ts
582
- import { getServerConfig as getServerConfig7 } from "@uniformdev/canvas-next-rsc-shared";
584
+ import { getServerConfig as getServerConfig5 } from "@uniformdev/canvas-next-rsc-shared";
583
585
  import { RedirectDeleteDefinition } from "@uniformdev/webhooks";
584
586
  var handleRedirectDelete = async (body) => {
585
587
  var _a;
@@ -587,7 +589,7 @@ var handleRedirectDelete = async (body) => {
587
589
  if (!parsed.success) {
588
590
  return void 0;
589
591
  }
590
- const config = getServerConfig7();
592
+ const config = getServerConfig5();
591
593
  if ((_a = config.experimental) == null ? void 0 : _a.edgeRedirects) {
592
594
  await processEdgeConfigChange(parsed.data);
593
595
  }
@@ -595,7 +597,7 @@ var handleRedirectDelete = async (body) => {
595
597
  };
596
598
 
597
599
  // src/handler/messages/handleRedirectInsert.ts
598
- import { getServerConfig as getServerConfig8 } from "@uniformdev/canvas-next-rsc-shared";
600
+ import { getServerConfig as getServerConfig6 } from "@uniformdev/canvas-next-rsc-shared";
599
601
  import { RedirectInsertDefinition } from "@uniformdev/webhooks";
600
602
  var handleRedirectInsert = async (body) => {
601
603
  var _a;
@@ -603,7 +605,7 @@ var handleRedirectInsert = async (body) => {
603
605
  if (!parsed.success) {
604
606
  return void 0;
605
607
  }
606
- const config = getServerConfig8();
608
+ const config = getServerConfig6();
607
609
  if ((_a = config.experimental) == null ? void 0 : _a.edgeRedirects) {
608
610
  await processEdgeConfigChange(parsed.data);
609
611
  }
@@ -611,7 +613,7 @@ var handleRedirectInsert = async (body) => {
611
613
  };
612
614
 
613
615
  // src/handler/messages/handleRedirectUpdate.ts
614
- import { getServerConfig as getServerConfig9 } from "@uniformdev/canvas-next-rsc-shared";
616
+ import { getServerConfig as getServerConfig7 } from "@uniformdev/canvas-next-rsc-shared";
615
617
  import { RedirectUpdateDefinition } from "@uniformdev/webhooks";
616
618
  var handleRedirectUpdate = async (body) => {
617
619
  var _a;
@@ -619,7 +621,7 @@ var handleRedirectUpdate = async (body) => {
619
621
  if (!parsed.success) {
620
622
  return void 0;
621
623
  }
622
- const config = getServerConfig9();
624
+ const config = getServerConfig7();
623
625
  if ((_a = config.experimental) == null ? void 0 : _a.edgeRedirects) {
624
626
  await processEdgeConfigChange(parsed.data);
625
627
  }
@@ -629,6 +631,25 @@ var handleRedirectUpdate = async (body) => {
629
631
  // src/handler/createPreviewPOSTRouteHandler.ts
630
632
  var createPreviewPOSTRouteHandler = () => {
631
633
  return async (request) => {
634
+ let previewSecretPassed = false;
635
+ if (process.env.UNIFORM_PREVIEW_SECRET) {
636
+ const secretValue = request.nextUrl.searchParams.get(SECRET_QUERY_STRING_PARAM2);
637
+ if (secretValue !== process.env.UNIFORM_PREVIEW_SECRET) {
638
+ console.warn(
639
+ "The preview secret passed in the query string does not match the UNIFORM_PREVIEW_SECRET env var."
640
+ );
641
+ } else {
642
+ previewSecretPassed = true;
643
+ }
644
+ } else {
645
+ if (!process.env.UNIFORM_WEBHOOK_SECRET) {
646
+ console.warn("Both UNIFORM_PREVIEW_SECRET and UNIFORM_WEBHOOK_SECRET are not set.");
647
+ }
648
+ previewSecretPassed = true;
649
+ }
650
+ if (!previewSecretPassed) {
651
+ return new Response("The request could not be validated.", { status: 401 });
652
+ }
632
653
  const { looksLikeMessage, validation } = await isSvixMessage(request);
633
654
  if (looksLikeMessage) {
634
655
  if (!validation) {
@@ -653,7 +674,13 @@ var handleSvixMessage = async (request) => {
653
674
  handleManifestPublished
654
675
  ];
655
676
  let tags = void 0;
656
- const jsonBody = await request.json();
677
+ let jsonBody;
678
+ try {
679
+ jsonBody = await request.json();
680
+ } catch (err) {
681
+ console.error("Error parsing the request body as JSON.", err);
682
+ return new Response("Error parsing the request body as JSON.", { status: 400 });
683
+ }
657
684
  for (let i = 0; i < handlers.length; i++) {
658
685
  const handler = handlers[i];
659
686
  const result = await handler(jsonBody);