@cmssy/next 4.7.2 → 5.0.0

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/preset.d.cts DELETED
@@ -1,81 +0,0 @@
1
- import { NextRequest, NextResponse } from 'next/server';
2
- import { C as CmssyNextConfig } from './config-DNaPqq1f.cjs';
3
- import * as react_jsx_runtime from 'react/jsx-runtime';
4
- import { ComponentType } from 'react';
5
- import { BlockDefinition, CmssyLayoutGroup } from '@cmssy/react';
6
- import '@cmssy/types';
7
-
8
- interface CmssyProxyOptions {
9
- /**
10
- * Strip the language prefix before the app sees it, so static routes like
11
- * `/shop/cart` serve `/no/shop/cart` too. Leave it off for a catch-all app
12
- * that reads the language off the path itself.
13
- */
14
- stripLocalePrefix?: boolean;
15
- }
16
- /**
17
- * The whole middleware a cmssy app needs, in the order it has to happen:
18
- *
19
- * 1. resolve the language and pass it on (a route cannot read the prefix it
20
- * never sees);
21
- * 2. send a VERIFIED editor request to /cmssy-edit, carrying that language and
22
- * the edit flag - the public pages are static, and a static page never sees
23
- * the query string that would put it in edit mode;
24
- * 3. strip the language prefix for everything else, if asked.
25
- *
26
- * The order is not a detail: resolve the locale after the rewrite and the editor
27
- * preview renders in the wrong language; forget the edit flag and the header and
28
- * footer become markup the editor can select but not fill. Both mistakes shipped
29
- * before this existed.
30
- */
31
- declare function createCmssyProxy(config: CmssyNextConfig, options?: CmssyProxyOptions): (request: NextRequest) => Promise<NextResponse>;
32
- /**
33
- * The matcher a cmssy app wants: everything except Next internals, API routes
34
- * and files with an extension.
35
- *
36
- * Next parses `export const config` at COMPILE time, so it rejects an imported
37
- * constant - copy this value into your proxy literally:
38
- *
39
- * export const config = { matcher: ["/((?!_next/|api/|.*\\..*).*)"] };
40
- */
41
- declare const cmssyProxyMatcher: string[];
42
-
43
- interface CmssyChromeProps {
44
- config: CmssyNextConfig;
45
- blocks: BlockDefinition[];
46
- position: string;
47
- /**
48
- * The page whose layout to render. Defaults to "/" - the site chrome.
49
- */
50
- page?: string;
51
- /**
52
- * The client wrapper around CmssyLazyLayout, rendered in edit mode. Without it
53
- * the header and the footer are markup the editor can select and cannot fill.
54
- *
55
- * A COMPONENT, not a function to call: it lives on the client, and the server
56
- * can only render it.
57
- */
58
- editable?: ComponentType<{
59
- groups: CmssyLayoutGroup[];
60
- position: string;
61
- locale: string;
62
- defaultLocale: string;
63
- enabledLocales: string[];
64
- edit: {
65
- editorOrigin: string;
66
- };
67
- }>;
68
- }
69
- /**
70
- * The site chrome, rendered the way each mode needs it:
71
- *
72
- * - published traffic: server-rendered layout blocks (static, no client cost);
73
- * - the editor: the same blocks through the edit bridge, fetched with the
74
- * preview secret, so what you see is the draft you are editing.
75
- *
76
- * Getting this wrong is invisible - the site looks right and the editor shows a
77
- * header it cannot edit, or the published version of one. Both shipped before.
78
- */
79
- declare function CmssyChrome({ config, blocks, position, page, editable, }: CmssyChromeProps): Promise<react_jsx_runtime.JSX.Element>;
80
-
81
- export { CmssyChrome, type CmssyChromeProps, type CmssyProxyOptions, cmssyProxyMatcher, createCmssyProxy };
package/dist/preset.d.ts DELETED
@@ -1,81 +0,0 @@
1
- import { NextRequest, NextResponse } from 'next/server';
2
- import { C as CmssyNextConfig } from './config-DNaPqq1f.js';
3
- import * as react_jsx_runtime from 'react/jsx-runtime';
4
- import { ComponentType } from 'react';
5
- import { BlockDefinition, CmssyLayoutGroup } from '@cmssy/react';
6
- import '@cmssy/types';
7
-
8
- interface CmssyProxyOptions {
9
- /**
10
- * Strip the language prefix before the app sees it, so static routes like
11
- * `/shop/cart` serve `/no/shop/cart` too. Leave it off for a catch-all app
12
- * that reads the language off the path itself.
13
- */
14
- stripLocalePrefix?: boolean;
15
- }
16
- /**
17
- * The whole middleware a cmssy app needs, in the order it has to happen:
18
- *
19
- * 1. resolve the language and pass it on (a route cannot read the prefix it
20
- * never sees);
21
- * 2. send a VERIFIED editor request to /cmssy-edit, carrying that language and
22
- * the edit flag - the public pages are static, and a static page never sees
23
- * the query string that would put it in edit mode;
24
- * 3. strip the language prefix for everything else, if asked.
25
- *
26
- * The order is not a detail: resolve the locale after the rewrite and the editor
27
- * preview renders in the wrong language; forget the edit flag and the header and
28
- * footer become markup the editor can select but not fill. Both mistakes shipped
29
- * before this existed.
30
- */
31
- declare function createCmssyProxy(config: CmssyNextConfig, options?: CmssyProxyOptions): (request: NextRequest) => Promise<NextResponse>;
32
- /**
33
- * The matcher a cmssy app wants: everything except Next internals, API routes
34
- * and files with an extension.
35
- *
36
- * Next parses `export const config` at COMPILE time, so it rejects an imported
37
- * constant - copy this value into your proxy literally:
38
- *
39
- * export const config = { matcher: ["/((?!_next/|api/|.*\\..*).*)"] };
40
- */
41
- declare const cmssyProxyMatcher: string[];
42
-
43
- interface CmssyChromeProps {
44
- config: CmssyNextConfig;
45
- blocks: BlockDefinition[];
46
- position: string;
47
- /**
48
- * The page whose layout to render. Defaults to "/" - the site chrome.
49
- */
50
- page?: string;
51
- /**
52
- * The client wrapper around CmssyLazyLayout, rendered in edit mode. Without it
53
- * the header and the footer are markup the editor can select and cannot fill.
54
- *
55
- * A COMPONENT, not a function to call: it lives on the client, and the server
56
- * can only render it.
57
- */
58
- editable?: ComponentType<{
59
- groups: CmssyLayoutGroup[];
60
- position: string;
61
- locale: string;
62
- defaultLocale: string;
63
- enabledLocales: string[];
64
- edit: {
65
- editorOrigin: string;
66
- };
67
- }>;
68
- }
69
- /**
70
- * The site chrome, rendered the way each mode needs it:
71
- *
72
- * - published traffic: server-rendered layout blocks (static, no client cost);
73
- * - the editor: the same blocks through the edit bridge, fetched with the
74
- * preview secret, so what you see is the draft you are editing.
75
- *
76
- * Getting this wrong is invisible - the site looks right and the editor shows a
77
- * header it cannot edit, or the published version of one. Both shipped before.
78
- */
79
- declare function CmssyChrome({ config, blocks, position, page, editable, }: CmssyChromeProps): Promise<react_jsx_runtime.JSX.Element>;
80
-
81
- export { CmssyChrome, type CmssyChromeProps, type CmssyProxyOptions, cmssyProxyMatcher, createCmssyProxy };
package/dist/preset.js DELETED
@@ -1,231 +0,0 @@
1
- import { NextResponse } from 'next/server';
2
- import { resolveSiteLocales, fetchLayouts, CmssyServerLayout, splitLocaleFromPath } from '@cmssy/react';
3
- import { headers } from 'next/headers';
4
- import { jsx } from 'react/jsx-runtime';
5
-
6
- // src/preset/proxy.ts
7
-
8
- // src/config.ts
9
- var DEFAULT_CMSSY_EDITOR_ORIGINS = [
10
- "https://cmssy.io",
11
- "https://www.cmssy.io"
12
- ];
13
- function parseEditorOriginEnv(raw) {
14
- if (!raw) return void 0;
15
- const parts = raw.split(",").map((o) => o.trim()).filter((o) => o.length > 0);
16
- if (parts.length === 0) return void 0;
17
- return parts.length === 1 ? parts[0] : parts;
18
- }
19
- function isDevelopment() {
20
- return typeof process !== "undefined" && process.env.NODE_ENV === "development";
21
- }
22
- function resolveEditorOrigin(editorOrigin) {
23
- const value = editorOrigin ?? (typeof process !== "undefined" ? parseEditorOriginEnv(process.env.CMSSY_EDITOR_ORIGIN) : void 0);
24
- if (value === void 0) {
25
- return isDevelopment() ? "*" : DEFAULT_CMSSY_EDITOR_ORIGINS;
26
- }
27
- if (Array.isArray(value)) {
28
- const cleaned = value.filter((o) => o && o.trim().length > 0);
29
- return cleaned.length > 0 ? cleaned : DEFAULT_CMSSY_EDITOR_ORIGINS;
30
- }
31
- return value.trim().length > 0 ? value : DEFAULT_CMSSY_EDITOR_ORIGINS;
32
- }
33
-
34
- // src/csp.ts
35
- function toCspOrigin(origin) {
36
- if (origin === "*") return "*";
37
- let parsed;
38
- try {
39
- parsed = new URL(origin);
40
- } catch {
41
- throw new Error(`cmssy: invalid editorOrigin "${origin}"`);
42
- }
43
- if (parsed.origin === "null") {
44
- throw new Error(`cmssy: editorOrigin "${origin}" has no usable origin`);
45
- }
46
- return parsed.origin;
47
- }
48
- function frameAncestors(editorOrigin) {
49
- const resolved = resolveEditorOrigin(editorOrigin);
50
- const origins = Array.isArray(resolved) ? resolved : [resolved];
51
- if (origins.length === 0) {
52
- throw new Error(
53
- "cmssy: editorOrigin must contain at least one valid origin"
54
- );
55
- }
56
- const normalized = origins.map((origin) => toCspOrigin(origin.trim()));
57
- return `frame-ancestors ${normalized.join(" ")}`;
58
- }
59
- function mergeFrameAncestors(existing, directive) {
60
- if (!existing) return directive;
61
- const kept = existing.split(";").map((part) => part.trim()).filter((part) => part.length > 0 && !/^frame-ancestors\b/i.test(part));
62
- kept.push(directive);
63
- return kept.join("; ");
64
- }
65
- function applyCmssyCsp(response, options) {
66
- const merged = mergeFrameAncestors(
67
- response.headers.get("Content-Security-Policy"),
68
- frameAncestors(options.editorOrigin)
69
- );
70
- response.headers.set("Content-Security-Policy", merged);
71
- response.headers.delete("X-Frame-Options");
72
- return response;
73
- }
74
-
75
- // src/secret-match.ts
76
- var MAX_SECRET_LENGTH = 256;
77
- async function cmssySecretsMatch(a, b) {
78
- if (a.length > MAX_SECRET_LENGTH || b.length > MAX_SECRET_LENGTH) {
79
- return false;
80
- }
81
- const encoder = new TextEncoder();
82
- const [ha, hb] = await Promise.all([
83
- crypto.subtle.digest("SHA-256", encoder.encode(a)),
84
- crypto.subtle.digest("SHA-256", encoder.encode(b))
85
- ]);
86
- const va = new Uint8Array(ha);
87
- const vb = new Uint8Array(hb);
88
- let diff = 0;
89
- for (let i = 0; i < va.length; i += 1) {
90
- diff |= (va[i] ?? 0) ^ (vb[i] ?? 0);
91
- }
92
- return diff === 0;
93
- }
94
-
95
- // src/edit-mode.ts
96
- var CMSSY_EDIT_HEADER = "x-cmssy-edit";
97
- var CMSSY_EDIT_QUERY_PARAM = "cmssyEdit";
98
- var CMSSY_SECRET_QUERY_PARAM = "cmssySecret";
99
- async function isCmssyEditMode() {
100
- const h = await headers();
101
- return h.get(CMSSY_EDIT_HEADER) === "1";
102
- }
103
- var CMSSY_EDIT_PATH_PREFIX = "/cmssy-edit";
104
- async function cmssyEditRewrite(request, config, options = {}) {
105
- const { pathname, searchParams } = request.nextUrl;
106
- if (pathname.startsWith(CMSSY_EDIT_PATH_PREFIX)) return null;
107
- if (!searchParams.getAll(CMSSY_EDIT_QUERY_PARAM).includes("1")) return null;
108
- const provided = searchParams.get(CMSSY_SECRET_QUERY_PARAM);
109
- if (!provided || !config.draftSecret) return null;
110
- if (!await cmssySecretsMatch(provided, config.draftSecret)) return null;
111
- const url = request.nextUrl.clone();
112
- url.pathname = `${CMSSY_EDIT_PATH_PREFIX}${pathname === "/" ? "" : pathname}`;
113
- warnIfEditRouteMissing(url);
114
- return NextResponse.rewrite(
115
- url,
116
- options.requestHeaders ? { request: { headers: options.requestHeaders } } : void 0
117
- );
118
- }
119
- var probed = false;
120
- function warnIfEditRouteMissing(url) {
121
- if (process.env.NODE_ENV === "production" || probed) return;
122
- probed = true;
123
- void fetch(url, { method: "HEAD" }).then((response) => {
124
- if (response.status !== 404) return;
125
- console.error(
126
- `[cmssy] The editor request was rewritten to ${url.pathname}, but nothing is mounted there (404). Add the edit route:
127
-
128
- // app/cmssy-edit/[[...path]]/page.tsx
129
- export const dynamic = "force-dynamic";
130
- export default createCmssyEditPage(cmssy, blocks, { editor: CmssyEditor });
131
-
132
- Until then the editor preview stays blank.`
133
- );
134
- }).catch(() => {
135
- });
136
- }
137
- var CMSSY_LOCALE_HEADER = "x-cmssy-locale";
138
- async function localeForPathname(config, pathname) {
139
- const siteLocales = await resolveSiteLocales(config);
140
- const segments = pathname.split("/").filter(Boolean);
141
- return splitLocaleFromPath(segments, siteLocales).locale;
142
- }
143
- async function getCmssyLocale(config, options) {
144
- const { headers: headers2 } = await import('next/headers');
145
- const headerList = await headers2();
146
- const fromHeader = headerList.get(CMSSY_LOCALE_HEADER);
147
- if (fromHeader) return fromHeader;
148
- const { defaultLocale } = await resolveSiteLocales(config);
149
- return defaultLocale;
150
- }
151
-
152
- // src/preset/proxy.ts
153
- function createCmssyProxy(config, options = {}) {
154
- return async function cmssyProxy(request) {
155
- const { pathname } = request.nextUrl;
156
- const requestHeaders = new Headers(request.headers);
157
- requestHeaders.delete(CMSSY_EDIT_HEADER);
158
- requestHeaders.delete(CMSSY_LOCALE_HEADER);
159
- const locale = await localeForPathname(config, pathname);
160
- requestHeaders.set(CMSSY_LOCALE_HEADER, locale);
161
- const editHeaders = new Headers(requestHeaders);
162
- editHeaders.set(CMSSY_EDIT_HEADER, "1");
163
- const editRewrite = await cmssyEditRewrite(request, config, {
164
- requestHeaders: editHeaders
165
- });
166
- if (editRewrite) {
167
- applyCmssyCsp(editRewrite, { editorOrigin: config.editorOrigin });
168
- return editRewrite;
169
- }
170
- if (options.stripLocalePrefix && pathname.startsWith(`/${locale}`)) {
171
- const { defaultLocale } = await resolveSiteLocales(config);
172
- if (locale !== defaultLocale) {
173
- const url = request.nextUrl.clone();
174
- url.pathname = pathname.slice(locale.length + 1) || "/";
175
- return NextResponse.rewrite(url, {
176
- request: { headers: requestHeaders }
177
- });
178
- }
179
- }
180
- return NextResponse.next({ request: { headers: requestHeaders } });
181
- };
182
- }
183
- var cmssyProxyMatcher = ["/((?!_next/|api/|.*\\..*).*)"];
184
- async function CmssyChrome({
185
- config,
186
- blocks,
187
- position,
188
- page = "/",
189
- editable
190
- }) {
191
- const editMode = await isCmssyEditMode();
192
- const [groups, locale, siteLocales] = await Promise.all([
193
- fetchLayouts(
194
- config,
195
- page,
196
- editMode ? { previewSecret: config.draftSecret } : void 0
197
- ),
198
- getCmssyLocale(config),
199
- resolveSiteLocales(config)
200
- ]);
201
- if (editMode && editable) {
202
- const origin = resolveEditorOrigin(config.editorOrigin);
203
- const Editable = editable;
204
- return /* @__PURE__ */ jsx(
205
- Editable,
206
- {
207
- groups,
208
- position,
209
- locale,
210
- defaultLocale: siteLocales.defaultLocale,
211
- enabledLocales: siteLocales.locales,
212
- edit: {
213
- editorOrigin: (Array.isArray(origin) ? origin[0] : origin) ?? ""
214
- }
215
- }
216
- );
217
- }
218
- return /* @__PURE__ */ jsx(
219
- CmssyServerLayout,
220
- {
221
- groups,
222
- blocks,
223
- position,
224
- locale,
225
- defaultLocale: siteLocales.defaultLocale,
226
- enabledLocales: siteLocales.locales
227
- }
228
- );
229
- }
230
-
231
- export { CmssyChrome, cmssyProxyMatcher, createCmssyProxy };