@407dev/cms-astro 0.1.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/README.md +69 -0
- package/dist/chunk-2TKIQIOE.js +118 -0
- package/dist/chunk-2TKIQIOE.js.map +1 -0
- package/dist/chunk-53OQ5RXJ.js +912 -0
- package/dist/chunk-53OQ5RXJ.js.map +1 -0
- package/dist/chunk-56OMNQLH.js +1013 -0
- package/dist/chunk-56OMNQLH.js.map +1 -0
- package/dist/chunk-5AOOMQ7K.js +1031 -0
- package/dist/chunk-5AOOMQ7K.js.map +1 -0
- package/dist/chunk-5XRUCFDC.js +743 -0
- package/dist/chunk-5XRUCFDC.js.map +1 -0
- package/dist/chunk-62ZHCOLZ.js +25 -0
- package/dist/chunk-62ZHCOLZ.js.map +1 -0
- package/dist/chunk-6AZLRSYA.js +126 -0
- package/dist/chunk-6AZLRSYA.js.map +1 -0
- package/dist/chunk-6L3ZTCFB.js +14 -0
- package/dist/chunk-6L3ZTCFB.js.map +1 -0
- package/dist/chunk-6O7ZNNDT.js +697 -0
- package/dist/chunk-6O7ZNNDT.js.map +1 -0
- package/dist/chunk-6PQIXTMK.js +697 -0
- package/dist/chunk-6PQIXTMK.js.map +1 -0
- package/dist/chunk-7AW6M7QW.js +1013 -0
- package/dist/chunk-7AW6M7QW.js.map +1 -0
- package/dist/chunk-7E3XJLKX.js +73 -0
- package/dist/chunk-7E3XJLKX.js.map +1 -0
- package/dist/chunk-7LM54OTA.js +1031 -0
- package/dist/chunk-7LM54OTA.js.map +1 -0
- package/dist/chunk-7LMUFCWX.js +126 -0
- package/dist/chunk-7LMUFCWX.js.map +1 -0
- package/dist/chunk-A3OGL4OX.js +706 -0
- package/dist/chunk-A3OGL4OX.js.map +1 -0
- package/dist/chunk-AO4UAWOV.js +1030 -0
- package/dist/chunk-AO4UAWOV.js.map +1 -0
- package/dist/chunk-B4YYLS7Y.js +14 -0
- package/dist/chunk-B4YYLS7Y.js.map +1 -0
- package/dist/chunk-BDFVGXM2.js +697 -0
- package/dist/chunk-BDFVGXM2.js.map +1 -0
- package/dist/chunk-CXM7NZVX.js +1031 -0
- package/dist/chunk-CXM7NZVX.js.map +1 -0
- package/dist/chunk-DMLOBKYE.js +96 -0
- package/dist/chunk-DMLOBKYE.js.map +1 -0
- package/dist/chunk-DXJSYGS6.js +743 -0
- package/dist/chunk-DXJSYGS6.js.map +1 -0
- package/dist/chunk-EFUSAQZM.js +72 -0
- package/dist/chunk-EFUSAQZM.js.map +1 -0
- package/dist/chunk-F7KDGPBN.js +1013 -0
- package/dist/chunk-F7KDGPBN.js.map +1 -0
- package/dist/chunk-FBSQT45X.js +126 -0
- package/dist/chunk-FBSQT45X.js.map +1 -0
- package/dist/chunk-FEPDOMVH.js +697 -0
- package/dist/chunk-FEPDOMVH.js.map +1 -0
- package/dist/chunk-FQDOL2QW.js +25 -0
- package/dist/chunk-FQDOL2QW.js.map +1 -0
- package/dist/chunk-GB3LGQJM.js +109 -0
- package/dist/chunk-GB3LGQJM.js.map +1 -0
- package/dist/chunk-HCNP6R3P.js +736 -0
- package/dist/chunk-HCNP6R3P.js.map +1 -0
- package/dist/chunk-J6WEJDRI.js +743 -0
- package/dist/chunk-J6WEJDRI.js.map +1 -0
- package/dist/chunk-JHEAPNJ2.js +117 -0
- package/dist/chunk-JHEAPNJ2.js.map +1 -0
- package/dist/chunk-JOJDJ6GW.js +25 -0
- package/dist/chunk-JOJDJ6GW.js.map +1 -0
- package/dist/chunk-KTDV6WYB.js +202 -0
- package/dist/chunk-KTDV6WYB.js.map +1 -0
- package/dist/chunk-KZWJBKPC.js +734 -0
- package/dist/chunk-KZWJBKPC.js.map +1 -0
- package/dist/chunk-LEAQBGL6.js +109 -0
- package/dist/chunk-LEAQBGL6.js.map +1 -0
- package/dist/chunk-LGZJL2Q7.js +697 -0
- package/dist/chunk-LGZJL2Q7.js.map +1 -0
- package/dist/chunk-LQPBU46R.js +109 -0
- package/dist/chunk-LQPBU46R.js.map +1 -0
- package/dist/chunk-M55NNUTY.js +1013 -0
- package/dist/chunk-M55NNUTY.js.map +1 -0
- package/dist/chunk-M6NCYCC3.js +1013 -0
- package/dist/chunk-M6NCYCC3.js.map +1 -0
- package/dist/chunk-MGN7OZAU.js +109 -0
- package/dist/chunk-MGN7OZAU.js.map +1 -0
- package/dist/chunk-MGYZOX4K.js +1013 -0
- package/dist/chunk-MGYZOX4K.js.map +1 -0
- package/dist/chunk-MY4IEKJS.js +1031 -0
- package/dist/chunk-MY4IEKJS.js.map +1 -0
- package/dist/chunk-NCQRGAB4.js +705 -0
- package/dist/chunk-NCQRGAB4.js.map +1 -0
- package/dist/chunk-NI4NHXI4.js +697 -0
- package/dist/chunk-NI4NHXI4.js.map +1 -0
- package/dist/chunk-NPI5N7AU.js +14 -0
- package/dist/chunk-NPI5N7AU.js.map +1 -0
- package/dist/chunk-QN4CPMJF.js +126 -0
- package/dist/chunk-QN4CPMJF.js.map +1 -0
- package/dist/chunk-QYUVAKMC.js +1013 -0
- package/dist/chunk-QYUVAKMC.js.map +1 -0
- package/dist/chunk-RAAT6PDT.js +25 -0
- package/dist/chunk-RAAT6PDT.js.map +1 -0
- package/dist/chunk-RDPO5VPS.js +1013 -0
- package/dist/chunk-RDPO5VPS.js.map +1 -0
- package/dist/chunk-RQHIW2PF.js +25 -0
- package/dist/chunk-RQHIW2PF.js.map +1 -0
- package/dist/chunk-RREVHA52.js +722 -0
- package/dist/chunk-RREVHA52.js.map +1 -0
- package/dist/chunk-S7PT4PZN.js +25 -0
- package/dist/chunk-S7PT4PZN.js.map +1 -0
- package/dist/chunk-SUXV5M32.js +109 -0
- package/dist/chunk-SUXV5M32.js.map +1 -0
- package/dist/chunk-SZEORUY6.js +696 -0
- package/dist/chunk-SZEORUY6.js.map +1 -0
- package/dist/chunk-T7NGGWGY.js +25 -0
- package/dist/chunk-T7NGGWGY.js.map +1 -0
- package/dist/chunk-UEVWP5VE.js +696 -0
- package/dist/chunk-UEVWP5VE.js.map +1 -0
- package/dist/chunk-UJNKNP7U.js +748 -0
- package/dist/chunk-UJNKNP7U.js.map +1 -0
- package/dist/chunk-V4XVFWFI.js +1013 -0
- package/dist/chunk-V4XVFWFI.js.map +1 -0
- package/dist/chunk-VJU2CIKZ.js +729 -0
- package/dist/chunk-VJU2CIKZ.js.map +1 -0
- package/dist/chunk-VRQUP5L3.js +202 -0
- package/dist/chunk-VRQUP5L3.js.map +1 -0
- package/dist/chunk-WBJ5E7CL.js +742 -0
- package/dist/chunk-WBJ5E7CL.js.map +1 -0
- package/dist/chunk-XJPVYMZM.js +24 -0
- package/dist/chunk-XJPVYMZM.js.map +1 -0
- package/dist/chunk-Y3Q65C7R.js +14 -0
- package/dist/chunk-Y3Q65C7R.js.map +1 -0
- package/dist/chunk-YHEGFPTR.js +1030 -0
- package/dist/chunk-YHEGFPTR.js.map +1 -0
- package/dist/chunk-ZDLBN46C.js +697 -0
- package/dist/chunk-ZDLBN46C.js.map +1 -0
- package/dist/dev/index.d.ts +122 -0
- package/dist/dev/index.js +662 -0
- package/dist/dev/index.js.map +1 -0
- package/dist/fields/collections.d.ts +18 -0
- package/dist/fields/collections.js +37 -0
- package/dist/fields/collections.js.map +1 -0
- package/dist/index.d.ts +235 -0
- package/dist/index.js +956 -0
- package/dist/index.js.map +1 -0
- package/dist/middleware/index.d.ts +8 -0
- package/dist/middleware/index.js +13 -0
- package/dist/middleware/index.js.map +1 -0
- package/dist/preview-BEkLMD6N.d.ts +23 -0
- package/dist/preview-D9yewK8x.d.ts +24 -0
- package/dist/preview-DJEA4fj7.d.ts +23 -0
- package/dist/preview-DeD1Jn7U.d.ts +23 -0
- package/dist/toolbar/app.d.ts +28 -0
- package/dist/toolbar/app.js +1176 -0
- package/dist/toolbar/app.js.map +1 -0
- package/dist/types-15jfCQKb.d.ts +141 -0
- package/dist/types-BREdL9Gx.d.ts +140 -0
- package/dist/types-C6sUZNa2.d.ts +104 -0
- package/dist/types-CEYS5Fzc.d.ts +112 -0
- package/dist/types-DpeHpobR.d.ts +104 -0
- package/dist/types-NaKJe6Pg.d.ts +140 -0
- package/package.json +90 -0
- package/src/CmsHead.astro +25 -0
- package/src/CmsImage.astro +62 -0
- package/src/CmsVideo.astro +185 -0
- package/src/RichText.astro +32 -0
- package/src/client.test.ts +227 -0
- package/src/client.ts +164 -0
- package/src/dev/config.test.ts +258 -0
- package/src/dev/config.ts +319 -0
- package/src/dev/index.ts +5 -0
- package/src/dev/preview-token.ts +39 -0
- package/src/dev/routes.test.ts +354 -0
- package/src/dev/routes.ts +245 -0
- package/src/dev/token.test.ts +280 -0
- package/src/dev/token.ts +201 -0
- package/src/dev/verifier.test.ts +63 -0
- package/src/dev/verifier.ts +161 -0
- package/src/fields/attrs.ts +24 -0
- package/src/fields/collection.ts +72 -0
- package/src/fields/collections.ts +35 -0
- package/src/fields/entryScope.test.ts +62 -0
- package/src/fields/entryScope.ts +240 -0
- package/src/fields/group.ts +51 -0
- package/src/fields/helpers.test.ts +51 -0
- package/src/fields/helpers.ts +214 -0
- package/src/fields/index.ts +46 -0
- package/src/fields/scope.ts +21 -0
- package/src/fields/types.ts +119 -0
- package/src/head.test.ts +74 -0
- package/src/head.ts +123 -0
- package/src/image.test.ts +42 -0
- package/src/image.ts +64 -0
- package/src/index.test.ts +76 -0
- package/src/index.ts +13 -0
- package/src/integration.ts +218 -0
- package/src/jwt.ts +131 -0
- package/src/middleware/index.ts +4 -0
- package/src/middleware/preview.test.ts +184 -0
- package/src/middleware/preview.ts +206 -0
- package/src/preview/bridge-script.test.ts +683 -0
- package/src/preview/bridge-script.ts +763 -0
- package/src/preview/origins.ts +29 -0
- package/src/preview.ts +49 -0
- package/src/richtext/RichTextNode.astro +130 -0
- package/src/richtext/render.ts +248 -0
- package/src/richtext.test.ts +139 -0
- package/src/runtime/bundle.ts +28 -0
- package/src/runtime/store.ts +130 -0
- package/src/siteFiles.test.ts +93 -0
- package/src/siteFiles.ts +111 -0
- package/src/toolbar/app.test.ts +191 -0
- package/src/toolbar/app.ts +1091 -0
- package/src/toolbar/icon.ts +5 -0
- package/src/toolbar/widgets.test.ts +153 -0
- package/src/toolbar/widgets.ts +365 -0
- package/src/types.test.ts +154 -0
- package/src/types.ts +163 -0
- package/src/version.ts +3 -0
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { createCmsClient, getClient } from '../client';
|
|
2
|
+
import { buildBridgeScript } from '../preview/bridge-script';
|
|
3
|
+
import { isDraftMode, runWithDraftBundle, setActiveDraftBundle } from '../runtime/store';
|
|
4
|
+
import type { CmsClient, ContentBundle } from '../types';
|
|
5
|
+
|
|
6
|
+
export interface PreviewMiddlewareOptions {
|
|
7
|
+
client?: CmsClient;
|
|
8
|
+
siteId?: string;
|
|
9
|
+
contentUrl?: string;
|
|
10
|
+
/** @deprecated Site hosts no longer verify preview tokens. Ignored outside astro dev. */
|
|
11
|
+
previewSecret?: string;
|
|
12
|
+
editorOrigins?: string[];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface PreviewContextLocals {
|
|
16
|
+
__cms_is_draft?: boolean;
|
|
17
|
+
__cms_draft_bundle?: ContentBundle;
|
|
18
|
+
[key: string]: unknown;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface PreviewMiddlewareContext {
|
|
22
|
+
request: Request;
|
|
23
|
+
url?: URL;
|
|
24
|
+
locals?: PreviewContextLocals;
|
|
25
|
+
rewrite?: (path: string) => Promise<Response> | undefined;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function createPreviewMiddleware(options: PreviewMiddlewareOptions = {}) {
|
|
29
|
+
let client = options.client;
|
|
30
|
+
|
|
31
|
+
return async (
|
|
32
|
+
context: PreviewMiddlewareContext,
|
|
33
|
+
next: () => Promise<Response>,
|
|
34
|
+
): Promise<Response> => {
|
|
35
|
+
const globalWithCms = globalThis as typeof globalThis & {
|
|
36
|
+
__CMS_IS_REWRITING__?: boolean;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// If already running within an active draft bundle context or internal rewrite, pass through
|
|
40
|
+
if (
|
|
41
|
+
(typeof globalThis !== 'undefined' && globalWithCms.__CMS_IS_REWRITING__) ||
|
|
42
|
+
isDraftMode() ||
|
|
43
|
+
context.locals?.__cms_is_draft
|
|
44
|
+
) {
|
|
45
|
+
return next();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const rawUrl = context.url;
|
|
49
|
+
const url =
|
|
50
|
+
rawUrl && typeof rawUrl.href === 'string'
|
|
51
|
+
? new URL(rawUrl.href)
|
|
52
|
+
: rawUrl && typeof (rawUrl as unknown as { pathname?: string }).pathname === 'string'
|
|
53
|
+
? rawUrl
|
|
54
|
+
: new URL(context.request?.url || '/', 'http://localhost');
|
|
55
|
+
const pathname = url.pathname || '/';
|
|
56
|
+
const isPreviewRoute = pathname === '/_preview' || pathname.startsWith('/_preview/');
|
|
57
|
+
|
|
58
|
+
const previewToken =
|
|
59
|
+
(rawUrl?.searchParams?.get ? rawUrl.searchParams.get('preview_token') : null) ||
|
|
60
|
+
(url?.searchParams?.get ? url.searchParams.get('preview_token') : null) ||
|
|
61
|
+
(context.request?.headers?.get ? context.request.headers.get('x-preview-token') : null);
|
|
62
|
+
|
|
63
|
+
if (!isPreviewRoute && !previewToken) {
|
|
64
|
+
return next();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (!client) {
|
|
68
|
+
client = getClient() || undefined;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (!client) {
|
|
72
|
+
const metaEnv = (import.meta as unknown as { env?: Record<string, string | undefined> }).env;
|
|
73
|
+
const siteId =
|
|
74
|
+
options.siteId ||
|
|
75
|
+
(typeof process !== 'undefined' && process.env?.CMS_SITE_ID) ||
|
|
76
|
+
(typeof import.meta !== 'undefined' && metaEnv?.CMS_SITE_ID) ||
|
|
77
|
+
'';
|
|
78
|
+
const contentUrl =
|
|
79
|
+
options.contentUrl ||
|
|
80
|
+
(typeof process !== 'undefined' && process.env?.CMS_CONTENT_URL) ||
|
|
81
|
+
(typeof import.meta !== 'undefined' && metaEnv?.CMS_CONTENT_URL) ||
|
|
82
|
+
'';
|
|
83
|
+
if (siteId && contentUrl) {
|
|
84
|
+
client = createCmsClient({
|
|
85
|
+
siteId,
|
|
86
|
+
contentUrl,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (!previewToken) {
|
|
92
|
+
return new Response('Preview token is missing', {
|
|
93
|
+
status: 400,
|
|
94
|
+
headers: {
|
|
95
|
+
'Cache-Control': 'private, no-store',
|
|
96
|
+
'X-Robots-Tag': 'noindex, nofollow',
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (!client) {
|
|
102
|
+
return new Response('CMS client is missing or not configured', {
|
|
103
|
+
status: 500,
|
|
104
|
+
headers: {
|
|
105
|
+
'Cache-Control': 'private, no-store',
|
|
106
|
+
'X-Robots-Tag': 'noindex, nofollow',
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
let draftBundle: ContentBundle;
|
|
112
|
+
try {
|
|
113
|
+
draftBundle = await client.getBundle({ previewToken });
|
|
114
|
+
} catch (_err) {
|
|
115
|
+
return new Response('Preview token is not valid for this site', {
|
|
116
|
+
status: 404,
|
|
117
|
+
headers: {
|
|
118
|
+
'Cache-Control': 'private, no-store',
|
|
119
|
+
'X-Robots-Tag': 'noindex, nofollow',
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Run downstream request within the draft bundle context
|
|
125
|
+
if (context.locals) {
|
|
126
|
+
context.locals.__cms_is_draft = true;
|
|
127
|
+
context.locals.__cms_draft_bundle = draftBundle;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
let finalResponse: Response;
|
|
131
|
+
setActiveDraftBundle(draftBundle);
|
|
132
|
+
try {
|
|
133
|
+
finalResponse = await runWithDraftBundle(draftBundle, async () => {
|
|
134
|
+
let response: Response;
|
|
135
|
+
if (isPreviewRoute) {
|
|
136
|
+
const basePath = pathname.replace(/^\/_preview/, '') || '/';
|
|
137
|
+
if (typeof context.rewrite === 'function') {
|
|
138
|
+
if (typeof globalThis !== 'undefined') {
|
|
139
|
+
globalWithCms.__CMS_IS_REWRITING__ = true;
|
|
140
|
+
}
|
|
141
|
+
try {
|
|
142
|
+
const rewritten = await context.rewrite(basePath);
|
|
143
|
+
if (rewritten) {
|
|
144
|
+
response = rewritten as Response;
|
|
145
|
+
} else {
|
|
146
|
+
response = await next();
|
|
147
|
+
}
|
|
148
|
+
} finally {
|
|
149
|
+
if (typeof globalThis !== 'undefined') {
|
|
150
|
+
globalWithCms.__CMS_IS_REWRITING__ = false;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
} else {
|
|
154
|
+
response = await next();
|
|
155
|
+
}
|
|
156
|
+
} else {
|
|
157
|
+
response = await next();
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const contentType = response?.headers?.get ? response.headers.get('content-type') : '';
|
|
161
|
+
const isStaticAsset = Boolean(
|
|
162
|
+
pathname.match(/\.(js|css|png|jpg|jpeg|gif|svg|ico|webp|woff|woff2|ttf|json|map)$/i),
|
|
163
|
+
);
|
|
164
|
+
const isHtml =
|
|
165
|
+
(typeof contentType === 'string' && contentType.includes('text/html')) ||
|
|
166
|
+
(!contentType && !isStaticAsset);
|
|
167
|
+
|
|
168
|
+
if (isHtml) {
|
|
169
|
+
const html = await response.text();
|
|
170
|
+
const bridgeScript = buildBridgeScript({ editorOrigins: options.editorOrigins });
|
|
171
|
+
const scriptTag = `<script>${bridgeScript}</script>`;
|
|
172
|
+
const modifiedHtml = html.includes('</body>')
|
|
173
|
+
? html.replace('</body>', `${scriptTag}</body>`)
|
|
174
|
+
: `${html}${scriptTag}`;
|
|
175
|
+
|
|
176
|
+
const newHeaders = new Headers(response.headers);
|
|
177
|
+
if (!newHeaders.has('content-type')) {
|
|
178
|
+
newHeaders.set('content-type', 'text/html; charset=utf-8');
|
|
179
|
+
}
|
|
180
|
+
newHeaders.delete('content-length');
|
|
181
|
+
newHeaders.set('Cache-Control', 'private, no-store, no-cache, must-revalidate');
|
|
182
|
+
newHeaders.set('X-Robots-Tag', 'noindex, nofollow');
|
|
183
|
+
|
|
184
|
+
return new Response(modifiedHtml, {
|
|
185
|
+
status: response.status,
|
|
186
|
+
statusText: response.statusText,
|
|
187
|
+
headers: newHeaders,
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const nonHtmlHeaders = new Headers(response.headers);
|
|
192
|
+
nonHtmlHeaders.set('Cache-Control', 'private, no-store, no-cache, must-revalidate');
|
|
193
|
+
nonHtmlHeaders.set('X-Robots-Tag', 'noindex, nofollow');
|
|
194
|
+
return new Response(response.body, {
|
|
195
|
+
status: response.status,
|
|
196
|
+
statusText: response.statusText,
|
|
197
|
+
headers: nonHtmlHeaders,
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
} finally {
|
|
201
|
+
setActiveDraftBundle(null);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return finalResponse;
|
|
205
|
+
};
|
|
206
|
+
}
|