@conloca/cloud-astro 0.2.6 → 0.3.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 +6 -2
- package/dist/.build-inputs.json +18 -13
- package/dist/src/component-manifest.d.ts +11 -0
- package/dist/src/component-manifest.d.ts.map +1 -1
- package/dist/src/component-manifest.js +20 -2
- package/dist/src/component-manifest.js.map +1 -1
- package/dist/src/descriptors.d.ts.map +1 -1
- package/dist/src/descriptors.js +6 -2
- package/dist/src/descriptors.js.map +1 -1
- package/dist/src/doctor/ancestor-chain.d.ts +14 -0
- package/dist/src/doctor/ancestor-chain.d.ts.map +1 -0
- package/dist/src/doctor/ancestor-chain.js +57 -0
- package/dist/src/doctor/ancestor-chain.js.map +1 -0
- package/dist/src/doctor/checks-frames.d.ts +5 -0
- package/dist/src/doctor/checks-frames.d.ts.map +1 -0
- package/dist/src/doctor/checks-frames.js +78 -0
- package/dist/src/doctor/checks-frames.js.map +1 -0
- package/dist/src/doctor/checks-live.d.ts +5 -14
- package/dist/src/doctor/checks-live.d.ts.map +1 -1
- package/dist/src/doctor/checks-live.js +8 -18
- package/dist/src/doctor/checks-live.js.map +1 -1
- package/dist/src/doctor/request.d.ts +14 -0
- package/dist/src/doctor/request.d.ts.map +1 -0
- package/dist/src/doctor/request.js +17 -0
- package/dist/src/doctor/request.js.map +1 -0
- package/dist/src/doctor/types.d.ts +1 -0
- package/dist/src/doctor/types.d.ts.map +1 -1
- package/dist/src/doctor/types.js +1 -0
- package/dist/src/doctor/types.js.map +1 -1
- package/dist/src/editor-page/bundle.d.ts +4 -0
- package/dist/src/editor-page/bundle.d.ts.map +1 -1
- package/dist/src/editor-page/bundle.js +55 -7
- package/dist/src/editor-page/bundle.js.map +1 -1
- package/dist/src/editor-page/data-schemas.d.ts +19 -5
- package/dist/src/editor-page/data-schemas.d.ts.map +1 -1
- package/dist/src/editor-page/data-schemas.js +17 -13
- package/dist/src/editor-page/data-schemas.js.map +1 -1
- package/dist/src/editor-page/emit.d.ts +3 -0
- package/dist/src/editor-page/emit.d.ts.map +1 -1
- package/dist/src/editor-page/emit.js +27 -10
- package/dist/src/editor-page/emit.js.map +1 -1
- package/dist/src/editor-page/frame-chains.d.ts +51 -0
- package/dist/src/editor-page/frame-chains.d.ts.map +1 -0
- package/dist/src/editor-page/frame-chains.js +121 -0
- package/dist/src/editor-page/frame-chains.js.map +1 -0
- package/dist/src/editor-page/page.d.ts +1 -3
- package/dist/src/editor-page/page.d.ts.map +1 -1
- package/dist/src/editor-page/page.js +3 -5
- package/dist/src/editor-page/page.js.map +1 -1
- package/dist/src/integration.d.ts.map +1 -1
- package/dist/src/integration.js +3 -0
- package/dist/src/integration.js.map +1 -1
- package/dist/src/preview/banner.d.ts +3 -2
- package/dist/src/preview/banner.d.ts.map +1 -1
- package/dist/src/preview/banner.js +15 -6
- package/dist/src/preview/banner.js.map +1 -1
- package/dist/src/preview/content-api.d.ts +8 -1
- package/dist/src/preview/content-api.d.ts.map +1 -1
- package/dist/src/preview/content-api.js +16 -1
- package/dist/src/preview/content-api.js.map +1 -1
- package/dist/src/preview/dispatcher.d.ts.map +1 -1
- package/dist/src/preview/dispatcher.js +10 -4
- package/dist/src/preview/dispatcher.js.map +1 -1
- package/dist/src/preview/site-middleware.d.ts.map +1 -1
- package/dist/src/preview/site-middleware.js +9 -4
- package/dist/src/preview/site-middleware.js.map +1 -1
- package/dist/src/preview/unready-pages.d.ts +21 -0
- package/dist/src/preview/unready-pages.d.ts.map +1 -0
- package/dist/src/preview/unready-pages.js +54 -0
- package/dist/src/preview/unready-pages.js.map +1 -0
- package/package.json +5 -5
- package/src/component-manifest.ts +30 -4
- package/src/descriptors.ts +6 -2
- package/src/doctor/ancestor-chain.ts +58 -0
- package/src/doctor/checks-frames.ts +84 -0
- package/src/doctor/checks-live.ts +9 -37
- package/src/doctor/request.ts +27 -0
- package/src/doctor/types.ts +1 -0
- package/src/editor-page/bundle.ts +65 -7
- package/src/editor-page/data-schemas.ts +30 -16
- package/src/editor-page/emit.ts +29 -10
- package/src/editor-page/frame-chains.ts +143 -0
- package/src/editor-page/page.ts +3 -4
- package/src/integration.ts +3 -0
- package/src/preview/banner.ts +17 -4
- package/src/preview/content-api.ts +18 -1
- package/src/preview/dispatcher.ts +10 -5
- package/src/preview/site-middleware.ts +9 -4
- package/src/preview/unready-pages.ts +65 -0
- package/src/skill/SKILL.md +7 -2
|
@@ -60,6 +60,10 @@ export interface EditorBundleDescriptors {
|
|
|
60
60
|
dataBindings?: EditorDataBindings;
|
|
61
61
|
/** The URL the site serves its media folder at (conlocaCMS `assetRefBase`), which every picked image link starts with. */
|
|
62
62
|
assetRefBase?: string;
|
|
63
|
+
/** Absolute path of the fragment components render module (conlocaCMS `fragmentComponentsPath`). */
|
|
64
|
+
fragmentComponentsPath?: string;
|
|
65
|
+
/** Route name → absolute path of that route's editor frame module (conlocaCMS routing `editorFrame`). */
|
|
66
|
+
editorFrames?: Record<string, string>;
|
|
63
67
|
}
|
|
64
68
|
|
|
65
69
|
export interface EditorPageBundleInput {
|
|
@@ -164,7 +168,12 @@ export async function bundleEditorPage(input: EditorPageBundleInput): Promise<Ed
|
|
|
164
168
|
const entryPath = resolve(input.root, input.puckConfigPath);
|
|
165
169
|
const descriptors = input.descriptors;
|
|
166
170
|
const discovered = descriptors?.discoveredMdxComponents ?? [];
|
|
167
|
-
const
|
|
171
|
+
const renderPath = descriptors?.fragmentComponentsPath && resolve(descriptors.fragmentComponentsPath);
|
|
172
|
+
const frames = Object.entries(descriptors?.editorFrames ?? {}).map(([route, path]) => ({
|
|
173
|
+
route,
|
|
174
|
+
path: resolve(path),
|
|
175
|
+
}));
|
|
176
|
+
const staticEntry = [
|
|
168
177
|
`export { default } from ${JSON.stringify(entryPath)};`,
|
|
169
178
|
// Copy the namespace before selecting optional names: a direct named
|
|
170
179
|
// re-export would reject schemas modules that declare only some of them.
|
|
@@ -181,7 +190,22 @@ export async function bundleEditorPage(input: EditorPageBundleInput): Promise<Ed
|
|
|
181
190
|
...(descriptors?.assetRefBase === undefined
|
|
182
191
|
? []
|
|
183
192
|
: [`export const assetRefBase = ${JSON.stringify(descriptors.assetRefBase)};`]),
|
|
184
|
-
|
|
193
|
+
// Namespace copies, like the schemas module's: a missing export reaches generateBundle's checks.
|
|
194
|
+
...(renderPath
|
|
195
|
+
? [
|
|
196
|
+
`import * as __fc from ${JSON.stringify(renderPath)};`,
|
|
197
|
+
'export const fragmentComponents = { ...__fc }.fragmentComponents;',
|
|
198
|
+
]
|
|
199
|
+
: []),
|
|
200
|
+
...(frames.length > 0
|
|
201
|
+
? [
|
|
202
|
+
...frames.map(({ path }, index) => `import * as __f${index} from ${JSON.stringify(path)};`),
|
|
203
|
+
`export const editorFrames = { ${frames
|
|
204
|
+
.map(({ route }, index) => `${JSON.stringify(route)}: { ...__f${index} }.default`)
|
|
205
|
+
.join(', ')} };`,
|
|
206
|
+
]
|
|
207
|
+
: []),
|
|
208
|
+
];
|
|
185
209
|
|
|
186
210
|
const jsAssets: EditorAsset[] = [];
|
|
187
211
|
const entryId = '\0conloca-editor-page-entry.js';
|
|
@@ -190,14 +214,48 @@ export async function bundleEditorPage(input: EditorPageBundleInput): Promise<Ed
|
|
|
190
214
|
resolveId(id) {
|
|
191
215
|
return id === entryId ? id : null;
|
|
192
216
|
},
|
|
193
|
-
load(id) {
|
|
194
|
-
|
|
217
|
+
async load(id) {
|
|
218
|
+
if (id !== entryId) return null;
|
|
219
|
+
if (!descriptors) return staticEntry.join('\n');
|
|
220
|
+
// The renderer publish runs: the @conloca/mdx copy the site's @conloca/astro-cms resolves.
|
|
221
|
+
// A resolve that throws (an exports map without `./render`, as in @conloca/mdx ≤0.2.6) counts as unresolved.
|
|
222
|
+
const tryResolve = (specifier: string, importer: string) =>
|
|
223
|
+
this.resolve(specifier, importer).then(
|
|
224
|
+
(resolved) => resolved?.id,
|
|
225
|
+
() => undefined,
|
|
226
|
+
);
|
|
227
|
+
const astroCms = await tryResolve('@conloca/astro-cms', join(input.root, 'package.json'));
|
|
228
|
+
const renderer = astroCms && (await tryResolve('@conloca/mdx/render', astroCms));
|
|
229
|
+
if (!renderer) {
|
|
230
|
+
input.logger.warn(
|
|
231
|
+
`${PACKAGE_NAME}: @conloca/mdx/render does not resolve from ` +
|
|
232
|
+
`${astroCms ?? `@conloca/astro-cms (looked up from ${input.root})`}; ` +
|
|
233
|
+
"the editor renders fragments with its own renderer instead of the site's.",
|
|
234
|
+
);
|
|
235
|
+
return staticEntry.join('\n');
|
|
236
|
+
}
|
|
237
|
+
return [...staticEntry, `export { evaluateMDXToComponent } from ${JSON.stringify(renderer)};`].join('\n');
|
|
195
238
|
},
|
|
196
239
|
async generateBundle() {
|
|
240
|
+
// Vite resolves symlinks (macOS's /var → /private/var), so look modules up by their resolved ids.
|
|
241
|
+
const exportsOf = async (path: string) => {
|
|
242
|
+
const id = (await this.resolve(path))?.id;
|
|
243
|
+
return (id && this.getModuleInfo(id)?.exports) || [];
|
|
244
|
+
};
|
|
245
|
+
if (renderPath && !(await exportsOf(renderPath)).includes('fragmentComponents')) {
|
|
246
|
+
input.logger.warn(
|
|
247
|
+
`${renderPath}: the fragment components module does not export fragmentComponents; ` +
|
|
248
|
+
'the editor renders fragments with no site components.',
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
for (const { route, path } of frames) {
|
|
252
|
+
// oxlint-disable-next-line eslint/no-await-in-loop -- in order, so the first frame without one is reported.
|
|
253
|
+
if (!(await exportsOf(path)).includes('default')) {
|
|
254
|
+
this.error(`${PACKAGE_NAME}: the editor frame for route "${route}" (${path}) has no default export.`);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
197
257
|
if (!descriptors?.schemasPath) return;
|
|
198
|
-
|
|
199
|
-
const schemasId = (await this.resolve(resolve(descriptors.schemasPath)))?.id;
|
|
200
|
-
const exports = (schemasId && this.getModuleInfo(schemasId)?.exports) || [];
|
|
258
|
+
const exports = await exportsOf(resolve(descriptors.schemasPath));
|
|
201
259
|
if (!SCHEMA_MODULE_EXPORTS.some((name) => exports.includes(name))) {
|
|
202
260
|
input.logger.warn(
|
|
203
261
|
`${PACKAGE_NAME}: the schemas module at ${descriptors.schemasPath} exports none of ` +
|
|
@@ -6,34 +6,48 @@ import { runnerImport, type UserConfig } from 'vite';
|
|
|
6
6
|
import { PACKAGE_NAME } from '../constants.js';
|
|
7
7
|
import { messageOf } from '../message-of.js';
|
|
8
8
|
|
|
9
|
+
/** The schemas module's exports the editor page uses, or why it failed to load. */
|
|
10
|
+
export type LoadedSchemasModule = { dataSchemas: unknown; mdxComponents: unknown } | { error: string };
|
|
11
|
+
|
|
9
12
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* schema generators such as typia's transform apply. A module that fails to load costs the file, not the build.
|
|
13
|
+
* Loads the schemas module through the site's own Vite plugins, as the content config does, so schema generators such
|
|
14
|
+
* as typia's transform apply. Never throws: a failed load is returned as its message.
|
|
13
15
|
*/
|
|
14
|
-
export async function
|
|
15
|
-
folder: string;
|
|
16
|
+
export async function loadSchemasModule(input: {
|
|
16
17
|
root: string;
|
|
17
18
|
vite: UserConfig;
|
|
18
19
|
schemasPath: string;
|
|
19
|
-
|
|
20
|
-
}): Promise<void> {
|
|
21
|
-
let dataSchemas: unknown;
|
|
20
|
+
}): Promise<LoadedSchemasModule> {
|
|
22
21
|
try {
|
|
23
22
|
// shortcut: runnerImport resolves plugins in serve mode, so an apply: 'build' schema transform is skipped (warning, no file); upgrade if a site needs one.
|
|
24
|
-
const { module } = await runnerImport<{ dataSchemas?: unknown }>(
|
|
25
|
-
|
|
26
|
-
configFile: false,
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
});
|
|
30
|
-
dataSchemas = module.dataSchemas;
|
|
23
|
+
const { module } = await runnerImport<{ dataSchemas?: unknown; mdxComponents?: unknown }>(
|
|
24
|
+
resolve(input.root, input.schemasPath),
|
|
25
|
+
{ ...input.vite, configFile: false, root: input.root, logLevel: 'warn' },
|
|
26
|
+
);
|
|
27
|
+
return { dataSchemas: module.dataSchemas, mdxComponents: module.mdxComponents };
|
|
31
28
|
} catch (error) {
|
|
29
|
+
return { error: messageOf(error) };
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Writes `<folder>/data-schemas.json`: the JSON schema `metadata` of each `dataSchemas` collection in the loaded
|
|
35
|
+
* schemas module, in the module's order. A module that failed to load costs the file, not the build.
|
|
36
|
+
*/
|
|
37
|
+
export async function emitDataSchemas(input: {
|
|
38
|
+
folder: string;
|
|
39
|
+
schemasPath: string;
|
|
40
|
+
loaded: LoadedSchemasModule;
|
|
41
|
+
logger: { warn(message: string): void };
|
|
42
|
+
}): Promise<void> {
|
|
43
|
+
const { loaded } = input;
|
|
44
|
+
if ('error' in loaded) {
|
|
32
45
|
input.logger.warn(
|
|
33
|
-
`${PACKAGE_NAME}: the schemas module at ${input.schemasPath} failed to load, so ${DATA_SCHEMAS_FILE} is not written: ${
|
|
46
|
+
`${PACKAGE_NAME}: the schemas module at ${input.schemasPath} failed to load, so ${DATA_SCHEMAS_FILE} is not written: ${loaded.error}`,
|
|
34
47
|
);
|
|
35
48
|
return;
|
|
36
49
|
}
|
|
50
|
+
const { dataSchemas } = loaded;
|
|
37
51
|
if (!isRecord(dataSchemas)) return;
|
|
38
52
|
const collections = Object.fromEntries(
|
|
39
53
|
Object.entries(dataSchemas).flatMap(([name, entry]) => {
|
package/src/editor-page/emit.ts
CHANGED
|
@@ -42,7 +42,8 @@ import { packageVersion } from '../package-version.js';
|
|
|
42
42
|
import type { InheritedOptions } from '../resolve-options.js';
|
|
43
43
|
import { resolveBuildSiteId } from '../site-id.js';
|
|
44
44
|
import { bundleEditorPage } from './bundle.js';
|
|
45
|
-
import { emitDataSchemas } from './data-schemas.js';
|
|
45
|
+
import { emitDataSchemas, loadSchemasModule } from './data-schemas.js';
|
|
46
|
+
import { emitFrameChains } from './frame-chains.js';
|
|
46
47
|
import { findFramingBlockers } from './framing.js';
|
|
47
48
|
import { renderEditorPage } from './page.js';
|
|
48
49
|
import { baseMismatch } from './site-base.js';
|
|
@@ -71,6 +72,9 @@ export interface EmitEditorPageInput {
|
|
|
71
72
|
serverOutput: boolean;
|
|
72
73
|
/** Astro's `i18n`, whose non-default locale folders hold no pages to offer. */
|
|
73
74
|
i18n: AstroI18nLocales | undefined;
|
|
75
|
+
/** Astro's `trailingSlash` and `build.format`: whether a built page is served with a trailing slash. */
|
|
76
|
+
trailingSlash: 'always' | 'never' | 'ignore';
|
|
77
|
+
buildFormat: 'directory' | 'file' | 'preserve';
|
|
74
78
|
logger: { info(message: string): void; warn(message: string): void };
|
|
75
79
|
}
|
|
76
80
|
|
|
@@ -117,15 +121,22 @@ export async function emitEditorPage(input: EmitEditorPageInput): Promise<void>
|
|
|
117
121
|
...(descriptors === undefined ? {} : { descriptors }),
|
|
118
122
|
logger,
|
|
119
123
|
});
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
124
|
+
const schemasPath = descriptors?.schemasPath;
|
|
125
|
+
const schemas = schemasPath
|
|
126
|
+
? await loadSchemasModule({ root: input.root, vite: input.vite, schemasPath })
|
|
127
|
+
: undefined;
|
|
128
|
+
if (schemasPath && schemas) await emitDataSchemas({ folder, schemasPath, loaded: schemas, logger });
|
|
129
|
+
await emitFrameChains({
|
|
130
|
+
folder,
|
|
131
|
+
root: input.root,
|
|
132
|
+
vite: input.vite,
|
|
133
|
+
editorFrames: descriptors?.editorFrames,
|
|
134
|
+
dataBindings: descriptors?.dataBindings,
|
|
135
|
+
contentRoot,
|
|
136
|
+
trailingSlash: input.trailingSlash,
|
|
137
|
+
buildFormat: input.buildFormat,
|
|
138
|
+
logger,
|
|
139
|
+
});
|
|
129
140
|
|
|
130
141
|
let editorCssFile: string | null = null;
|
|
131
142
|
if (inherited.editorCss) {
|
|
@@ -145,6 +156,14 @@ export async function emitEditorPage(input: EmitEditorPageInput): Promise<void>
|
|
|
145
156
|
folder: EDITOR_DIR,
|
|
146
157
|
puckConfigPath: inherited.puckConfigPath,
|
|
147
158
|
projectRoot: input.root,
|
|
159
|
+
vite: input.vite,
|
|
160
|
+
...(descriptors?.fragmentComponentsPath
|
|
161
|
+
? { fragmentComponentsPath: descriptors.fragmentComponentsPath }
|
|
162
|
+
: {}),
|
|
163
|
+
...(schemas ? { schemas } : {}),
|
|
164
|
+
...(descriptors?.discoveredMdxComponents
|
|
165
|
+
? { discoveredMdxComponents: descriptors.discoveredMdxComponents }
|
|
166
|
+
: {}),
|
|
148
167
|
logger,
|
|
149
168
|
});
|
|
150
169
|
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { writeFile } from 'node:fs/promises';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { type EditorDataBindings, isContentPublished } from '@conloca/content-api';
|
|
4
|
+
import { FileSystemContentAPI, type Site } from '@conloca/content-api/node';
|
|
5
|
+
import { runnerImport, type UserConfig } from 'vite';
|
|
6
|
+
import { PACKAGE_NAME } from '../constants.js';
|
|
7
|
+
import { type AncestorElement, ancestorChain } from '../doctor/ancestor-chain.js';
|
|
8
|
+
import { messageOf } from '../message-of.js';
|
|
9
|
+
import { cleanPagePath } from '../preview/preview-path.js';
|
|
10
|
+
|
|
11
|
+
const VIRTUAL_ID = 'virtual:conloca-frame-chains';
|
|
12
|
+
const RESOLVED_ID = `\0${VIRTUAL_ID}`;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* What `_conloca/editor/frames.json` holds: each framed route's published page, at the path the site serves it at,
|
|
16
|
+
* and the elements its frame wraps it in.
|
|
17
|
+
*/
|
|
18
|
+
export interface FramesFile {
|
|
19
|
+
version: 1;
|
|
20
|
+
entries: { route: string; pathname: string; chain: AncestorElement[] }[];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Renders each route's frame around a page-root marker with the site's React, through the site's Vite plugins, and
|
|
25
|
+
* reads the elements between `<body>` and the marker. Each frame loads in its own runner (the runner closes once the
|
|
26
|
+
* import settles), so a frame that fails costs only its route, under `failures`.
|
|
27
|
+
*/
|
|
28
|
+
export async function renderFrameChains(input: {
|
|
29
|
+
root: string;
|
|
30
|
+
vite: UserConfig;
|
|
31
|
+
frames: Record<string, string>;
|
|
32
|
+
pathnames: Record<string, string>;
|
|
33
|
+
}): Promise<{ chains: Record<string, AncestorElement[]>; failures: Record<string, string> }> {
|
|
34
|
+
const chains: Record<string, AncestorElement[]> = {};
|
|
35
|
+
const failures: Record<string, string> = {};
|
|
36
|
+
for (const [route, path] of Object.entries(input.frames)) {
|
|
37
|
+
const code = [
|
|
38
|
+
"import { createElement } from 'react';",
|
|
39
|
+
"import { renderToStaticMarkup } from 'react-dom/server';",
|
|
40
|
+
`import Frame from ${JSON.stringify(path)};`,
|
|
41
|
+
'export function render(route, pathname) {',
|
|
42
|
+
" return renderToStaticMarkup(createElement(Frame, { pathname }, createElement('div', { 'data-conloca-page-root': '' })));",
|
|
43
|
+
'}',
|
|
44
|
+
'',
|
|
45
|
+
].join('\n');
|
|
46
|
+
try {
|
|
47
|
+
// One frame at a time: each runner loads the site's Vite plugins, and a failure must cost only its route.
|
|
48
|
+
// oxlint-disable-next-line eslint/no-await-in-loop -- see above.
|
|
49
|
+
const { module } = await runnerImport<{ render: (route: string, pathname: string) => string }>(VIRTUAL_ID, {
|
|
50
|
+
...input.vite,
|
|
51
|
+
configFile: false,
|
|
52
|
+
root: input.root,
|
|
53
|
+
logLevel: 'warn',
|
|
54
|
+
plugins: [
|
|
55
|
+
...(input.vite.plugins ?? []),
|
|
56
|
+
{
|
|
57
|
+
name: 'conloca-frame-chains',
|
|
58
|
+
resolveId: (id) => (id === VIRTUAL_ID ? RESOLVED_ID : undefined),
|
|
59
|
+
load: (id) => (id === RESOLVED_ID ? code : undefined),
|
|
60
|
+
},
|
|
61
|
+
],
|
|
62
|
+
});
|
|
63
|
+
const chain = ancestorChain(`<body>${module.render(route, input.pathnames[route] ?? '/')}</body>`);
|
|
64
|
+
if (chain === undefined) failures[route] = 'the frame did not render its children';
|
|
65
|
+
else chains[route] = chain;
|
|
66
|
+
} catch (error) {
|
|
67
|
+
failures[route] = messageOf(error);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return { chains, failures };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Writes `<folder>/frames.json` when a routing entry declares an editor frame: per framed entry, the first page of its
|
|
75
|
+
* collection that is published at build time in the default locale, and its frame's chain. The page's path gets the
|
|
76
|
+
* trailing slash Astro's `trailingSlash` and `build.format` give it, so CC30 fetches it without a redirect. Never
|
|
77
|
+
* fails the build.
|
|
78
|
+
*/
|
|
79
|
+
export async function emitFrameChains(input: {
|
|
80
|
+
folder: string;
|
|
81
|
+
root: string;
|
|
82
|
+
vite: UserConfig;
|
|
83
|
+
editorFrames: Record<string, string> | undefined;
|
|
84
|
+
dataBindings: EditorDataBindings | undefined;
|
|
85
|
+
contentRoot: string | undefined;
|
|
86
|
+
buildTime?: Date;
|
|
87
|
+
trailingSlash: 'always' | 'never' | 'ignore';
|
|
88
|
+
buildFormat: 'directory' | 'file' | 'preserve';
|
|
89
|
+
logger: { info(message: string): void; warn(message: string): void };
|
|
90
|
+
}): Promise<void> {
|
|
91
|
+
const { dataBindings, logger } = input;
|
|
92
|
+
const framed = (dataBindings?.entries ?? []).flatMap((entry) => {
|
|
93
|
+
const path = entry.frame === undefined ? undefined : input.editorFrames?.[entry.frame];
|
|
94
|
+
return path === undefined ? [] : [{ route: entry.name, collection: entry.collection, path }];
|
|
95
|
+
});
|
|
96
|
+
if (framed.length === 0 || dataBindings === undefined) return;
|
|
97
|
+
if (input.contentRoot === undefined) {
|
|
98
|
+
logger.info('no content root, so frames.json is not written');
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
const buildTime = input.buildTime ?? new Date();
|
|
102
|
+
let site: Site | null;
|
|
103
|
+
try {
|
|
104
|
+
site = (await FileSystemContentAPI.create({ contentRoot: input.contentRoot })).getSite(dataBindings.siteName);
|
|
105
|
+
} catch (error) {
|
|
106
|
+
logger.warn(`${PACKAGE_NAME}: the content could not be read, so frames.json is not written: ${messageOf(error)}`);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
const frames: Record<string, string> = {};
|
|
110
|
+
const pathnames: Record<string, string> = {};
|
|
111
|
+
for (const { route, collection, path } of framed) {
|
|
112
|
+
// Scheduled pages are left out: frames.json is public.
|
|
113
|
+
let pathname: string | undefined;
|
|
114
|
+
for (const manifest of site?.listContent({ collection, locales: [dataBindings.locale] }) ?? []) {
|
|
115
|
+
const localized = manifest.locales[dataBindings.locale];
|
|
116
|
+
if (localized && !localized.unpublished && isContentPublished(localized, buildTime)) {
|
|
117
|
+
pathname = localized.pathname;
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
if (pathname === undefined) {
|
|
122
|
+
logger.info(`route ${route} has no published page, so frames.json leaves its editor frame out`);
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
frames[route] = path;
|
|
126
|
+
pathnames[route] = pathname;
|
|
127
|
+
}
|
|
128
|
+
if (Object.keys(frames).length === 0) return;
|
|
129
|
+
const { chains, failures } = await renderFrameChains({ root: input.root, vite: input.vite, frames, pathnames });
|
|
130
|
+
for (const [route, message] of Object.entries(failures)) {
|
|
131
|
+
logger.warn(
|
|
132
|
+
`${PACKAGE_NAME}: the editor frame of route ${route} (${frames[route]}) failed to render, so frames.json leaves it out: ${message}`,
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
const entries = Object.keys(chains).map((route) => ({
|
|
136
|
+
route,
|
|
137
|
+
pathname: cleanPagePath(pathnames[route], input.trailingSlash, input.buildFormat),
|
|
138
|
+
chain: chains[route],
|
|
139
|
+
}));
|
|
140
|
+
if (entries.length === 0) return;
|
|
141
|
+
const file: FramesFile = { version: 1, entries };
|
|
142
|
+
await writeFile(join(input.folder, 'frames.json'), `${JSON.stringify(file, null, 2)}\n`);
|
|
143
|
+
}
|
package/src/editor-page/page.ts
CHANGED
|
@@ -3,13 +3,12 @@ import {
|
|
|
3
3
|
EDITOR_HOST_PROTOCOL,
|
|
4
4
|
EDITOR_UI_MODULE_FILES,
|
|
5
5
|
type EditorHostPageData,
|
|
6
|
+
editorPageContentSecurityPolicy,
|
|
6
7
|
} from '@conloca/content-api/editor-host';
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* The editor page's `index.html`: the editor UI from `<editorUiOrigin>/host/v<protocol>/`, its `importmap` script, and
|
|
10
|
-
* the page data as a JSON block that never executes
|
|
11
|
-
* and the `importmap` script's hash; it leaves styles and fonts alone, so the editor UI's stylesheet may load its web
|
|
12
|
-
* font.
|
|
11
|
+
* the page data as a JSON block that never executes, under `editorPageContentSecurityPolicy`.
|
|
13
12
|
*/
|
|
14
13
|
export function renderEditorPage(data: EditorHostPageData, editorUiOrigin: string): string {
|
|
15
14
|
const uiPath = `${editorUiOrigin}/host/v${EDITOR_HOST_PROTOCOL}/`;
|
|
@@ -24,7 +23,7 @@ export function renderEditorPage(data: EditorHostPageData, editorUiOrigin: strin
|
|
|
24
23
|
'<head>',
|
|
25
24
|
'<meta charset="utf-8">',
|
|
26
25
|
'<meta name="robots" content="noindex, nofollow">',
|
|
27
|
-
`<meta http-equiv="Content-Security-Policy" content="
|
|
26
|
+
`<meta http-equiv="Content-Security-Policy" content="${editorPageContentSecurityPolicy(uiPath, importMapHash)}">`,
|
|
28
27
|
'<title>Conloca editor</title>',
|
|
29
28
|
`<script type="importmap">${importMapJson}</script>`,
|
|
30
29
|
// `<` escaped so no value can close the element; JSON.parse reads `\u003c` back as `<`.
|
package/src/integration.ts
CHANGED
|
@@ -590,6 +590,9 @@ export function conlocaCloud(options: ConlocaCloudOptions = {}): AstroIntegratio
|
|
|
590
590
|
pagesDir: fileURLToPath(new URL('pages/', srcDir ?? new URL('src/', projectRoot))),
|
|
591
591
|
serverOutput,
|
|
592
592
|
i18n,
|
|
593
|
+
// Optional-chained: unit tests drive the hooks with a partial config.
|
|
594
|
+
trailingSlash: astroConfig?.trailingSlash ?? 'ignore',
|
|
595
|
+
buildFormat: astroConfig?.build?.format ?? 'directory',
|
|
593
596
|
logger,
|
|
594
597
|
});
|
|
595
598
|
// After the editor page: on Cloudflare the list goes in its folder, which the editor page must create.
|
package/src/preview/banner.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
import type { PreviewContext } from './middleware.js';
|
|
2
|
+
import type { UnreadyPage } from './unready-pages.js';
|
|
3
|
+
|
|
4
|
+
/** The banner's second line: the pages a push would leave out, and what clears each. */
|
|
5
|
+
function unreadyLine(held: readonly UnreadyPage[]): string {
|
|
6
|
+
const pages = held.map((p) => `${p.title} (${p.reason === 'lfs' ? 'ask your developer' : 'a clash to resolve'})`);
|
|
7
|
+
return `Not ready to push: ${pages.join(', ')}`;
|
|
8
|
+
}
|
|
2
9
|
|
|
3
10
|
/**
|
|
4
11
|
* Returns the body of the script tag that renders the floating preview
|
|
@@ -21,6 +28,7 @@ export function getBannerScriptText(
|
|
|
21
28
|
exitPath?: string,
|
|
22
29
|
shareUrl?: string,
|
|
23
30
|
pagePath?: string,
|
|
31
|
+
held: readonly UnreadyPage[] = [],
|
|
24
32
|
): string {
|
|
25
33
|
// Serialize then patch the `<` character that JSON.stringify does NOT
|
|
26
34
|
// escape. Browsers terminate inline scripts on `</script>` regardless of
|
|
@@ -31,14 +39,17 @@ export function getBannerScriptText(
|
|
|
31
39
|
// Only the branch name and public addresses reach the page, never the pass:
|
|
32
40
|
// every script on the page, third-party tags included, could read it. A
|
|
33
41
|
// share viewer gets who shared the draft and when the link expires instead
|
|
34
|
-
// of the editor links. `undefined` values drop out of the JSON.
|
|
42
|
+
// of the editor links. `undefined` values drop out of the JSON. Both see the
|
|
43
|
+
// pages a push would hold back, by title only.
|
|
44
|
+
const unreadyText = held.length > 0 ? unreadyLine(held) : undefined;
|
|
35
45
|
const embedded =
|
|
36
46
|
ctx.grant === undefined
|
|
37
|
-
? { branch: ctx.branch, endpoint, exitPath, shareUrl, path: pagePath }
|
|
47
|
+
? { branch: ctx.branch, endpoint, exitPath, shareUrl, path: pagePath, held: unreadyText }
|
|
38
48
|
: {
|
|
39
49
|
branch: ctx.branch,
|
|
40
50
|
exitPath,
|
|
41
51
|
path: pagePath,
|
|
52
|
+
held: unreadyText,
|
|
42
53
|
viewer: { sharedBy: ctx.grant.sharedBy, expiresAt: ctx.grant.expiresAt },
|
|
43
54
|
};
|
|
44
55
|
const ctxJson = JSON.stringify(embedded).replace(/</g, '\\u003c');
|
|
@@ -50,7 +61,7 @@ export function getBannerScriptText(
|
|
|
50
61
|
'var __conlocaRender = function () {',
|
|
51
62
|
" var host = document.createElement('div');",
|
|
52
63
|
" host.id = 'conloca-preview-banner';",
|
|
53
|
-
" host.style.cssText = 'position:fixed;bottom:16px;right:16px;z-index:2147483647;font-family:system-ui,sans-serif;background:#111;color:#fff;border-radius:8px;box-shadow:0 4px 12px rgba(0,0,0,0.35);padding:8px 12px;font-size:13px;display:flex;gap:12px;align-items:center;';",
|
|
64
|
+
" host.style.cssText = 'position:fixed;bottom:16px;right:16px;z-index:2147483647;font-family:system-ui,sans-serif;background:#111;color:#fff;border-radius:8px;box-shadow:0 4px 12px rgba(0,0,0,0.35);padding:8px 12px;font-size:13px;display:flex;flex-wrap:wrap;gap:12px;align-items:center;';",
|
|
54
65
|
" var label = document.createElement('span');",
|
|
55
66
|
' if (__conlocaPreview.viewer) {',
|
|
56
67
|
' var days = Math.max(1, Math.ceil((__conlocaPreview.viewer.expiresAt * 1000 - Date.now()) / 86400000));',
|
|
@@ -72,6 +83,7 @@ export function getBannerScriptText(
|
|
|
72
83
|
" var collapse = document.createElement('button'); collapse.type = 'button'; collapse.textContent = __conlocaIsCollapsed() ? '+' : '-'; collapse.style.cssText = 'background:transparent;color:#fff;border:none;cursor:pointer;font-size:16px;line-height:1;'; host.appendChild(collapse);",
|
|
73
84
|
" collapse.addEventListener('click', function () { var c = !__conlocaIsCollapsed(); __conlocaSetCollapsed(c); host.style.display = c ? 'none' : ''; });",
|
|
74
85
|
" if (__conlocaIsCollapsed()) host.style.display = 'none';",
|
|
86
|
+
" if (__conlocaPreview.held) { var held = document.createElement('div'); held.style.flexBasis = '100%'; held.textContent = __conlocaPreview.held; host.appendChild(held); }",
|
|
75
87
|
' document.body.appendChild(host);',
|
|
76
88
|
'};',
|
|
77
89
|
"if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', __conlocaRender); } else { __conlocaRender(); }",
|
|
@@ -102,8 +114,9 @@ export function injectPreviewBanner(
|
|
|
102
114
|
exitPath?: string,
|
|
103
115
|
shareUrl?: string,
|
|
104
116
|
pagePath?: string,
|
|
117
|
+
held: readonly UnreadyPage[] = [],
|
|
105
118
|
): string {
|
|
106
|
-
const script = `<script>${getBannerScriptText(ctx, endpoint, exitPath, shareUrl, pagePath)}</script>`;
|
|
119
|
+
const script = `<script>${getBannerScriptText(ctx, endpoint, exitPath, shareUrl, pagePath, held)}</script>`;
|
|
107
120
|
let replaced = false;
|
|
108
121
|
const page = html.replace(SITE_ROBOTS_META_RE, () => {
|
|
109
122
|
const first = !replaced;
|
|
@@ -19,6 +19,7 @@ import type {
|
|
|
19
19
|
} from '@conloca/content-api';
|
|
20
20
|
import { DRAFT_MEDIA_REF_BASE, mediaRefPath } from '@conloca/content-api';
|
|
21
21
|
import type { PreviewContext } from './middleware.js';
|
|
22
|
+
import { fetchUnreadyKeys, titleUnreadyPages, type UnreadyKey, type UnreadyPage } from './unready-pages.js';
|
|
22
23
|
|
|
23
24
|
/**
|
|
24
25
|
* `Data` is not re-exported from `@conloca/content-api`'s package index; we
|
|
@@ -90,6 +91,8 @@ export class PreviewContentAPI implements ContentAPI {
|
|
|
90
91
|
private readonly fetchImpl: typeof fetch;
|
|
91
92
|
/** The branch's page set, once {@link loadBranchManifests} has run. */
|
|
92
93
|
private branchManifests?: ContentManifest[];
|
|
94
|
+
/** The pages a push would hold back, once {@link loadBranchManifests} has run. */
|
|
95
|
+
private held: UnreadyKey[] = [];
|
|
93
96
|
private readonly localizedReads = new Map<string, Promise<LocalizedEntry | null>>();
|
|
94
97
|
/** Embedded entries rewritten once per render, not on every read. */
|
|
95
98
|
private readonly embeddedRewritten = new Map<string, LocalizedEntry>();
|
|
@@ -197,7 +200,8 @@ export class PreviewContentAPI implements ContentAPI {
|
|
|
197
200
|
}
|
|
198
201
|
|
|
199
202
|
/**
|
|
200
|
-
* Prefetch the branch's own manifest listing
|
|
203
|
+
* Prefetch the branch's own manifest listing, and beside it the pages a push
|
|
204
|
+
* would hold back (see {@link unreadyPages}).
|
|
201
205
|
*
|
|
202
206
|
* The baseline is a snapshot of the last PRODUCTION build, so it cannot know
|
|
203
207
|
* about a page created on the branch and still contains pages the branch
|
|
@@ -211,6 +215,19 @@ export class PreviewContentAPI implements ContentAPI {
|
|
|
211
215
|
* something, an exception renders nothing.
|
|
212
216
|
*/
|
|
213
217
|
async loadBranchManifests(): Promise<void> {
|
|
218
|
+
const [, held] = await Promise.all([
|
|
219
|
+
this.loadManifests(),
|
|
220
|
+
fetchUnreadyKeys(this.fetchImpl, this.opts.saasEndpoint, this.opts.ctx.jwt),
|
|
221
|
+
]);
|
|
222
|
+
this.held = held;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** The pages a push would hold back, titled from the branch listing; `[]` when the fetch failed. */
|
|
226
|
+
unreadyPages(): UnreadyPage[] {
|
|
227
|
+
return titleUnreadyPages(this.held, this.branchManifests ?? []);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
private async loadManifests(): Promise<void> {
|
|
214
231
|
try {
|
|
215
232
|
// /preview/manifests admits the preview scope; the editor listing at
|
|
216
233
|
// /api/v1/content/manifests filters a preview JWT to `*`-prefixed ids
|
|
@@ -24,6 +24,7 @@ import {
|
|
|
24
24
|
serveDraftMedia,
|
|
25
25
|
} from './prefix-router.js';
|
|
26
26
|
import { normalizeTrailingSlash, PREVIEW_CODE_PARAM, PREVIEW_SLUG_HEADER, previewRoutePrefix } from './preview-path.js';
|
|
27
|
+
import { loadUnreadyPages, type UnreadyPage } from './unready-pages.js';
|
|
27
28
|
|
|
28
29
|
/** What the build writes next to the dispatcher (`.conloca/dispatcher/dispatcher.json`). */
|
|
29
30
|
export interface DispatcherConfig {
|
|
@@ -191,6 +192,7 @@ export function createDispatcher(config: DispatcherConfig, loadServer: (() => Pr
|
|
|
191
192
|
page: URL,
|
|
192
193
|
route: string,
|
|
193
194
|
published: Pick<PreviewContext, 'branch' | 'grant'> | null,
|
|
195
|
+
held: readonly UnreadyPage[] = [],
|
|
194
196
|
): Promise<Response> {
|
|
195
197
|
const out = finishEntry(answer, page, route, root, hosts);
|
|
196
198
|
if (!(out.headers.get('content-type') ?? '').includes('text/html')) return out;
|
|
@@ -206,6 +208,7 @@ export function createDispatcher(config: DispatcherConfig, loadServer: (() => Pr
|
|
|
206
208
|
`${route}/__exit`,
|
|
207
209
|
undefined,
|
|
208
210
|
page.pathname,
|
|
211
|
+
held,
|
|
209
212
|
);
|
|
210
213
|
return rewriter.transform(new Response(html, out));
|
|
211
214
|
}
|
|
@@ -298,13 +301,15 @@ export function createDispatcher(config: DispatcherConfig, loadServer: (() => Pr
|
|
|
298
301
|
if (prerendered.has(normalizeTrailingSlash(inner))) {
|
|
299
302
|
// The preview server never sees a prerendered page, so its pass is checked here, as the server checks every
|
|
300
303
|
// other: one that fails gets the landing page (or the share link's renewal), never the banner.
|
|
301
|
-
const
|
|
302
|
-
|
|
303
|
-
passOptions(config, baked, env, slug),
|
|
304
|
-
);
|
|
304
|
+
const options = passOptions(config, baked, env, slug);
|
|
305
|
+
const verdict = await handlePreviewRequest(new Request(page.href, { headers }), options);
|
|
305
306
|
if (verdict.kind !== 'preview')
|
|
306
307
|
return finishEntry(verdict.kind === 'response' ? verdict.response : landing(), page, route, root, hosts);
|
|
307
|
-
|
|
308
|
+
const [asset, held] = await Promise.all([
|
|
309
|
+
env.ASSETS.fetch(new Request(page.href, { headers })),
|
|
310
|
+
loadUnreadyPages(options.fetchFn ?? fetch.bind(globalThis), options.saasEndpoint, verdict.ctx.jwt),
|
|
311
|
+
]);
|
|
312
|
+
return finish(asset, page, route, verdict.ctx, held);
|
|
308
313
|
}
|
|
309
314
|
return finish(await guarded(page, slug), page, route, null);
|
|
310
315
|
},
|
|
@@ -46,6 +46,7 @@ import {
|
|
|
46
46
|
stripPreviewPrefix,
|
|
47
47
|
} from './preview-path.js';
|
|
48
48
|
import { NO_SITE_PAGES, type SitePageSource } from './site-pages.js';
|
|
49
|
+
import { loadUnreadyPages, type UnreadyPage } from './unready-pages.js';
|
|
49
50
|
|
|
50
51
|
/** The subset of Astro's middleware context this middleware touches. */
|
|
51
52
|
export interface PreviewMiddlewareContext {
|
|
@@ -198,7 +199,7 @@ export function createPreviewSiteMiddleware(resolveOpts: () => PreviewMiddleware
|
|
|
198
199
|
}
|
|
199
200
|
|
|
200
201
|
// The banner the draft and a site-authored page both carry.
|
|
201
|
-
const banner = (html: string): string => {
|
|
202
|
+
const banner = (html: string, held: readonly UnreadyPage[] = []): string => {
|
|
202
203
|
let shareUrl: string | undefined;
|
|
203
204
|
if (verdict.ctx.editorUrl !== undefined) {
|
|
204
205
|
const url = new URL(verdict.ctx.editorUrl);
|
|
@@ -206,7 +207,7 @@ export function createPreviewSiteMiddleware(resolveOpts: () => PreviewMiddleware
|
|
|
206
207
|
shareUrl = url.toString();
|
|
207
208
|
}
|
|
208
209
|
// The page's own path: under D3 and D8 the address bar holds the route, which Edit and Exit must not carry.
|
|
209
|
-
return injectPreviewBanner(html, verdict.ctx, opts.saasEndpoint, exitPath, shareUrl, pagePath);
|
|
210
|
+
return injectPreviewBanner(html, verdict.ctx, opts.saasEndpoint, exitPath, shareUrl, pagePath, held);
|
|
210
211
|
};
|
|
211
212
|
|
|
212
213
|
if (status.kind === 'redirect') {
|
|
@@ -230,7 +231,11 @@ export function createPreviewSiteMiddleware(resolveOpts: () => PreviewMiddleware
|
|
|
230
231
|
);
|
|
231
232
|
}
|
|
232
233
|
// Only the type carries over: production's encoding, length, cookies and validators don't fit the rewritten bytes.
|
|
233
|
-
|
|
234
|
+
const [html, held] = await Promise.all([
|
|
235
|
+
page.text(),
|
|
236
|
+
loadUnreadyPages(seams.fetchFn ?? fetch.bind(globalThis), opts.saasEndpoint, verdict.ctx.jwt),
|
|
237
|
+
]);
|
|
238
|
+
return stay(new Response(null, { headers: { 'content-type': type } }), banner(html, held));
|
|
234
239
|
}
|
|
235
240
|
|
|
236
241
|
if (status.kind === 'unchanged') {
|
|
@@ -276,7 +281,7 @@ export function createPreviewSiteMiddleware(resolveOpts: () => PreviewMiddleware
|
|
|
276
281
|
// through with the preview headers only.
|
|
277
282
|
return stay(rendered);
|
|
278
283
|
}
|
|
279
|
-
return stay(rendered, banner(await rendered.text()));
|
|
284
|
+
return stay(rendered, banner(await rendered.text(), previewApi.unreadyPages()));
|
|
280
285
|
};
|
|
281
286
|
}
|
|
282
287
|
|