@conloca/cloud-astro 0.2.2 → 0.2.4
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 +378 -207
- package/dist/.build-inputs.json +69 -0
- package/dist/src/component-manifest.d.ts +5 -3
- package/dist/src/component-manifest.d.ts.map +1 -1
- package/dist/src/component-manifest.js +7 -7
- package/dist/src/component-manifest.js.map +1 -1
- package/dist/src/constants.d.ts +14 -13
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/constants.js +14 -13
- package/dist/src/constants.js.map +1 -1
- package/dist/src/d3-build.d.ts +70 -0
- package/dist/src/d3-build.d.ts.map +1 -0
- package/dist/src/d3-build.js +272 -0
- package/dist/src/d3-build.js.map +1 -0
- package/dist/src/descriptors.d.ts +2 -2
- package/dist/src/descriptors.d.ts.map +1 -1
- package/dist/src/descriptors.js +4 -2
- package/dist/src/descriptors.js.map +1 -1
- package/dist/src/doctor/checks-address.d.ts +18 -0
- package/dist/src/doctor/checks-address.d.ts.map +1 -0
- package/dist/src/doctor/checks-address.js +163 -0
- package/dist/src/doctor/checks-address.js.map +1 -0
- package/dist/src/doctor/checks-live.d.ts +8 -5
- package/dist/src/doctor/checks-live.d.ts.map +1 -1
- package/dist/src/doctor/checks-live.js +226 -188
- package/dist/src/doctor/checks-live.js.map +1 -1
- package/dist/src/doctor/checks-offline.d.ts +6 -25
- package/dist/src/doctor/checks-offline.d.ts.map +1 -1
- package/dist/src/doctor/checks-offline.js +30 -259
- package/dist/src/doctor/checks-offline.js.map +1 -1
- package/dist/src/doctor/checks-preview-limits.d.ts +11 -0
- package/dist/src/doctor/checks-preview-limits.d.ts.map +1 -0
- package/dist/src/doctor/checks-preview-limits.js +133 -0
- package/dist/src/doctor/checks-preview-limits.js.map +1 -0
- package/dist/src/doctor/checks-preview-live.d.ts +14 -0
- package/dist/src/doctor/checks-preview-live.d.ts.map +1 -0
- package/dist/src/doctor/checks-preview-live.js +72 -0
- package/dist/src/doctor/checks-preview-live.js.map +1 -0
- package/dist/src/doctor/checks-preview.d.ts +7 -0
- package/dist/src/doctor/checks-preview.d.ts.map +1 -0
- package/dist/src/doctor/checks-preview.js +287 -0
- package/dist/src/doctor/checks-preview.js.map +1 -0
- package/dist/src/doctor/config-scan.d.ts +12 -3
- package/dist/src/doctor/config-scan.d.ts.map +1 -1
- package/dist/src/doctor/config-scan.js +38 -11
- package/dist/src/doctor/config-scan.js.map +1 -1
- package/dist/src/doctor/run.d.ts.map +1 -1
- package/dist/src/doctor/run.js +16 -10
- package/dist/src/doctor/run.js.map +1 -1
- package/dist/src/doctor/types.d.ts +17 -7
- package/dist/src/doctor/types.d.ts.map +1 -1
- package/dist/src/doctor/types.js +17 -7
- package/dist/src/doctor/types.js.map +1 -1
- package/dist/src/editor-page/bundle.d.ts +90 -0
- package/dist/src/editor-page/bundle.d.ts.map +1 -0
- package/dist/src/editor-page/bundle.js +373 -0
- package/dist/src/editor-page/bundle.js.map +1 -0
- package/dist/src/editor-page/data-schemas.d.ts +16 -0
- package/dist/src/editor-page/data-schemas.d.ts.map +1 -0
- package/dist/src/editor-page/data-schemas.js +39 -0
- package/dist/src/editor-page/data-schemas.js.map +1 -0
- package/dist/src/editor-page/emit.d.ts +69 -0
- package/dist/src/editor-page/emit.d.ts.map +1 -0
- package/dist/src/editor-page/emit.js +254 -0
- package/dist/src/editor-page/emit.js.map +1 -0
- package/dist/src/editor-page/framing.d.ts +23 -0
- package/dist/src/editor-page/framing.d.ts.map +1 -0
- package/dist/src/editor-page/framing.js +39 -0
- package/dist/src/editor-page/framing.js.map +1 -0
- package/dist/src/editor-page/page.d.ts +9 -0
- package/dist/src/editor-page/page.d.ts.map +1 -0
- package/dist/src/editor-page/page.js +31 -0
- package/dist/src/editor-page/page.js.map +1 -0
- package/dist/src/editor-page/site-base.d.ts +4 -0
- package/dist/src/editor-page/site-base.d.ts.map +1 -0
- package/dist/src/editor-page/site-base.js +13 -0
- package/dist/src/editor-page/site-base.js.map +1 -0
- package/dist/src/git.d.ts +11 -8
- package/dist/src/git.d.ts.map +1 -1
- package/dist/src/git.js +25 -8
- package/dist/src/git.js.map +1 -1
- package/dist/src/index.d.ts +4 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +11 -6
- package/dist/src/index.js.map +1 -1
- package/dist/src/integration.d.ts +1 -1
- package/dist/src/integration.d.ts.map +1 -1
- package/dist/src/integration.js +377 -416
- package/dist/src/integration.js.map +1 -1
- package/dist/src/node.d.ts +3 -1
- package/dist/src/node.d.ts.map +1 -1
- package/dist/src/node.js +7 -2
- package/dist/src/node.js.map +1 -1
- package/dist/src/preview/_dispatch.d.ts +12 -0
- package/dist/src/preview/_dispatch.d.ts.map +1 -1
- package/dist/src/preview/_dispatch.js +31 -0
- package/dist/src/preview/_dispatch.js.map +1 -1
- package/dist/src/preview/_resolve-opts.d.ts +19 -10
- package/dist/src/preview/_resolve-opts.d.ts.map +1 -1
- package/dist/src/preview/_resolve-opts.js +24 -11
- package/dist/src/preview/_resolve-opts.js.map +1 -1
- package/dist/src/preview/banner.d.ts +12 -9
- package/dist/src/preview/banner.d.ts.map +1 -1
- package/dist/src/preview/banner.js +46 -43
- package/dist/src/preview/banner.js.map +1 -1
- package/dist/src/preview/cloudflare-adapter.d.ts +35 -0
- package/dist/src/preview/cloudflare-adapter.d.ts.map +1 -0
- package/dist/src/preview/cloudflare-adapter.js +92 -0
- package/dist/src/preview/cloudflare-adapter.js.map +1 -0
- package/dist/src/preview/cookie.d.ts +13 -14
- package/dist/src/preview/cookie.d.ts.map +1 -1
- package/dist/src/preview/cookie.js +19 -26
- package/dist/src/preview/cookie.js.map +1 -1
- package/dist/src/preview/dispatcher.d.ts +73 -0
- package/dist/src/preview/dispatcher.d.ts.map +1 -0
- package/dist/src/preview/dispatcher.js +329 -0
- package/dist/src/preview/dispatcher.js.map +1 -0
- package/dist/src/preview/entry-code.d.ts +33 -0
- package/dist/src/preview/entry-code.d.ts.map +1 -0
- package/dist/src/preview/entry-code.js +33 -0
- package/dist/src/preview/entry-code.js.map +1 -0
- package/dist/src/preview/headers.d.ts +24 -4
- package/dist/src/preview/headers.d.ts.map +1 -1
- package/dist/src/preview/headers.js +35 -7
- package/dist/src/preview/headers.js.map +1 -1
- package/dist/src/preview/label.d.ts +8 -0
- package/dist/src/preview/label.d.ts.map +1 -0
- package/dist/src/preview/label.js +14 -0
- package/dist/src/preview/label.js.map +1 -0
- package/dist/src/preview/landing.d.ts +23 -2
- package/dist/src/preview/landing.d.ts.map +1 -1
- package/dist/src/preview/landing.js +105 -37
- package/dist/src/preview/landing.js.map +1 -1
- package/dist/src/preview/middleware.d.ts +49 -22
- package/dist/src/preview/middleware.d.ts.map +1 -1
- package/dist/src/preview/middleware.js +259 -100
- package/dist/src/preview/middleware.js.map +1 -1
- package/dist/src/preview/preview-path.d.ts +47 -5
- package/dist/src/preview/preview-path.d.ts.map +1 -1
- package/dist/src/preview/preview-path.js +65 -8
- package/dist/src/preview/preview-path.js.map +1 -1
- package/dist/src/preview/redeem-url.d.ts +7 -0
- package/dist/src/preview/redeem-url.d.ts.map +1 -0
- package/dist/src/preview/redeem-url.js +14 -0
- package/dist/src/preview/redeem-url.js.map +1 -0
- package/dist/src/preview/site-middleware.d.ts +5 -5
- package/dist/src/preview/site-middleware.d.ts.map +1 -1
- package/dist/src/preview/site-middleware.js +53 -26
- package/dist/src/preview/site-middleware.js.map +1 -1
- package/dist/src/preview-build.d.ts +29 -15
- package/dist/src/preview-build.d.ts.map +1 -1
- package/dist/src/preview-build.js +66 -25
- package/dist/src/preview-build.js.map +1 -1
- package/dist/src/preview.d.ts +1 -1
- package/dist/src/preview.d.ts.map +1 -1
- package/dist/src/preview.js +1 -1
- package/dist/src/preview.js.map +1 -1
- package/dist/src/repository.d.ts +1 -1
- package/dist/src/repository.js +1 -1
- package/dist/src/resolve-options.d.ts +17 -0
- package/dist/src/resolve-options.d.ts.map +1 -0
- package/dist/src/resolve-options.js +21 -0
- package/dist/src/resolve-options.js.map +1 -0
- package/dist/src/site-id.d.ts +0 -2
- package/dist/src/site-id.d.ts.map +1 -1
- package/dist/src/site-id.js +2 -3
- package/dist/src/site-id.js.map +1 -1
- package/dist/src/skill.d.ts +11 -0
- package/dist/src/skill.d.ts.map +1 -1
- package/dist/src/skill.js +32 -9
- package/dist/src/skill.js.map +1 -1
- package/dist/src/types.d.ts +33 -78
- package/dist/src/types.d.ts.map +1 -1
- package/package.json +63 -13
- package/src/component-manifest.ts +11 -8
- package/src/constants.ts +15 -13
- package/src/d3-build.ts +315 -0
- package/src/descriptors.ts +6 -4
- package/src/doctor/checks-address.ts +190 -0
- package/src/doctor/checks-live.ts +262 -268
- package/src/doctor/checks-offline.ts +32 -279
- package/src/doctor/checks-preview-limits.ts +137 -0
- package/src/doctor/checks-preview-live.ts +77 -0
- package/src/doctor/checks-preview.ts +302 -0
- package/src/doctor/config-scan.ts +35 -9
- package/src/doctor/run.ts +18 -16
- package/src/doctor/types.ts +17 -7
- package/src/editor-page/bundle.ts +487 -0
- package/src/editor-page/data-schemas.ts +47 -0
- package/src/editor-page/emit.ts +362 -0
- package/src/editor-page/framing.ts +62 -0
- package/src/editor-page/page.ts +37 -0
- package/src/editor-page/site-base.ts +11 -0
- package/src/git.ts +29 -10
- package/src/index.ts +22 -6
- package/src/integration.ts +399 -469
- package/src/node.ts +18 -2
- package/src/preview/_dispatch.ts +36 -1
- package/src/preview/_resolve-opts.ts +35 -12
- package/src/preview/banner.ts +61 -43
- package/src/preview/cloudflare-adapter.ts +103 -0
- package/src/preview/cookie.ts +18 -24
- package/src/preview/dispatcher.ts +407 -0
- package/src/preview/entry-code.ts +51 -0
- package/src/preview/headers.ts +35 -7
- package/src/preview/label.ts +12 -0
- package/src/preview/landing.ts +117 -39
- package/src/preview/middleware.ts +302 -115
- package/src/preview/preview-path.ts +83 -8
- package/src/preview/redeem-url.ts +14 -0
- package/src/preview/site-middleware.ts +60 -30
- package/src/preview-build.ts +85 -26
- package/src/preview-handler.astro +12 -9
- package/src/preview.ts +0 -1
- package/src/repository.ts +1 -1
- package/src/resolve-options.ts +37 -0
- package/src/site-id.ts +2 -4
- package/src/skill/SKILL.md +63 -55
- package/src/skill.ts +36 -8
- package/src/types.ts +33 -79
- package/src/virtual-modules.d.ts +4 -1
- package/dist/src/build-commit.d.ts +0 -19
- package/dist/src/build-commit.d.ts.map +0 -1
- package/dist/src/build-commit.js +0 -6
- package/dist/src/build-commit.js.map +0 -1
- package/dist/src/bundle.d.ts +0 -181
- package/dist/src/bundle.d.ts.map +0 -1
- package/dist/src/bundle.js +0 -413
- package/dist/src/bundle.js.map +0 -1
- package/dist/src/discovery.d.ts +0 -58
- package/dist/src/discovery.d.ts.map +0 -1
- package/dist/src/discovery.js +0 -133
- package/dist/src/discovery.js.map +0 -1
- package/dist/src/register.d.ts +0 -98
- package/dist/src/register.d.ts.map +0 -1
- package/dist/src/register.js +0 -103
- package/dist/src/register.js.map +0 -1
- package/src/build-commit.ts +0 -22
- package/src/bundle.ts +0 -570
- package/src/discovery.ts +0 -183
- package/src/register.ts +0 -180
package/src/integration.ts
CHANGED
|
@@ -1,24 +1,30 @@
|
|
|
1
|
-
import { existsSync } from 'node:fs';
|
|
2
1
|
import { createRequire } from 'node:module';
|
|
3
2
|
import { fileURLToPath } from 'node:url';
|
|
3
|
+
import type { AstroI18nLocales } from '@conloca/astro-cms/checks';
|
|
4
4
|
import { readEditorDescriptors } from '@conloca/astro-cms/node';
|
|
5
|
-
import
|
|
5
|
+
import { DEFAULT_EDITOR_ENDPOINT, editorUiOrigin } from '@conloca/content-api/editor-host';
|
|
6
|
+
import type { AstroIntegration } from 'astro';
|
|
6
7
|
import type { UserConfig as ViteUserConfig } from 'vite';
|
|
7
|
-
import {
|
|
8
|
-
import
|
|
9
|
-
import {
|
|
10
|
-
import { type ComponentManifestArtifact, emitComponentManifest } from './component-manifest.js';
|
|
11
|
-
import { BUILD_TOKEN_ENV_VAR, PACKAGE_NAME, PREVIEW_BUILD_ENV_FLAG, PREVIEW_SECRET_ENV_FLAG } from './constants.js';
|
|
12
|
-
import { resolveEditorDescriptors } from './descriptors.js';
|
|
13
|
-
import type { DiscoveredStyleContext } from './discovery.js';
|
|
14
|
-
import { discoverStyleContext } from './discovery.js';
|
|
8
|
+
import { PACKAGE_NAME, PREVIEW_BUILD_ENV_FLAG } from './constants.js';
|
|
9
|
+
import { finishD3Build, onDemandPatterns, workerFirstPattern, writePreviewRoutes } from './d3-build.js';
|
|
10
|
+
import { emitEditorPage, emitPreviewServerInfo, mirrorEditorPage } from './editor-page/emit.js';
|
|
15
11
|
import { gitOutput, resolveBuildRef, resolveRepositoryUri } from './git.js';
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
12
|
+
import { messageOf } from './message-of.js';
|
|
13
|
+
import type { BakedPreviewOpts } from './preview/_resolve-opts.js';
|
|
14
|
+
import { cloudflareAdapter, resolveCloudflareAdapter } from './preview/cloudflare-adapter.js';
|
|
15
|
+
import type { DispatcherExit } from './preview/dispatcher.js';
|
|
16
|
+
import { normalizeTrailingSlash, PREVIEW_ROUTE_SEGMENT, type PreviewServeMode } from './preview/preview-path.js';
|
|
17
|
+
import {
|
|
18
|
+
prerenderedKey,
|
|
19
|
+
previewBuildMode,
|
|
20
|
+
previewChildMode,
|
|
21
|
+
previewChildPrerendered,
|
|
22
|
+
spawnPreviewBuild,
|
|
23
|
+
} from './preview-build.js';
|
|
24
|
+
import { type InheritedOptions, resolveInheritedOptions } from './resolve-options.js';
|
|
19
25
|
import { resolveBuildSiteId } from './site-id.js';
|
|
20
26
|
import { scaffoldSkill } from './skill.js';
|
|
21
|
-
import type { ConlocaCloudOptions } from './types.js';
|
|
27
|
+
import type { ConlocaCloudOptions, PreviewBuildOptions } from './types.js';
|
|
22
28
|
|
|
23
29
|
// Re-export the constants that moved out of this module when the preview
|
|
24
30
|
// wiring broke the circular import. Public callers (and tests) import them
|
|
@@ -27,226 +33,271 @@ export { PACKAGE_NAME, PREVIEW_BUILD_ENV_FLAG };
|
|
|
27
33
|
|
|
28
34
|
/**
|
|
29
35
|
* Returns the site's Astro integration that hooks into `astro:build:done`
|
|
30
|
-
* to
|
|
31
|
-
*
|
|
36
|
+
* to write the editor page and, when the site opts in, trigger a recursive
|
|
37
|
+
* preview build.
|
|
32
38
|
*
|
|
33
39
|
* The build:done pipeline:
|
|
34
40
|
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
* endpoint, authenticated by the build token. An ATTEMPTED
|
|
44
|
-
* registration that is rejected FAILS THE BUILD — the error carries the
|
|
45
|
-
* endpoint's exact status and body. Only a never-attempted registration (no
|
|
46
|
-
* build token, repo identity, git sha/branch, or public base URL) skips,
|
|
47
|
-
* ending the build with one summary block — or failing it when
|
|
48
|
-
* `requireRegistration` is set.
|
|
49
|
-
* 3. Spawn the recursive preview build with the preview secret
|
|
50
|
-
* returned by registration (or the env override).
|
|
41
|
+
* 0. Write the editor page to `dist/_conloca/editor/`: `index.html`, the
|
|
42
|
+
* site's compiled puck config and styles, the MDX sandbox frame and
|
|
43
|
+
* `conloca.json`. Every build does this; the page carries no credential.
|
|
44
|
+
* 1. Only when `previewBuild` is given as an object: D2 spawns the recursive preview build and mirrors the editor
|
|
45
|
+
* page into its client folder; D3 spawns it next to the static site and writes the dispatcher (`d3-build.ts`),
|
|
46
|
+
* failing the build when a failed preview build would leave the site's own on-demand routes unserved; D4 builds
|
|
47
|
+
* it in a separate deploy, not here. The preview build mounts `/_conloca/preview/` and bakes in the repository,
|
|
48
|
+
* site and branch it serves (every pass must match them), Astro's base and the commit it was built from.
|
|
51
49
|
*/
|
|
52
50
|
const VIRTUAL_PREVIEW_OPTS = 'virtual:conloca-preview-opts';
|
|
53
51
|
const RESOLVED_PREVIEW_OPTS = `\0${VIRTUAL_PREVIEW_OPTS}`;
|
|
54
52
|
|
|
55
|
-
|
|
53
|
+
/** Where the preview build writes its server and client, outside dist/ (see config:setup). */
|
|
54
|
+
function previewOutDir(previewBuild: PreviewBuildOptions | false | undefined, root: URL): URL {
|
|
55
|
+
return new URL(`${(previewBuild || undefined)?.outDir ?? '.conloca/preview-server'}/`, root);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function conlocaCloud(options: ConlocaCloudOptions = {}): AstroIntegration {
|
|
56
59
|
validateOptions(options);
|
|
57
60
|
const repoUri = resolveRepositoryUri(options.repoUri, process.env);
|
|
58
61
|
// Populated during the preview build's config:setup and read by the virtual
|
|
59
62
|
// module below. Server-only — never reaches a browser bundle.
|
|
60
|
-
let previewOptsModule:
|
|
63
|
+
let previewOptsModule: BakedPreviewOpts | undefined;
|
|
64
|
+
// The exit values the preview build baked (its config:setup), reported to the site's build for the dispatcher.
|
|
65
|
+
let childExit: DispatcherExit | undefined;
|
|
66
|
+
// The Astro command, recorded in config:setup: only `astro build` starts
|
|
67
|
+
// the preview build, so only it checks the preview adapter at config:done.
|
|
68
|
+
let astroCommand: string | undefined;
|
|
61
69
|
// The project root, captured in config:setup: astro:build:done only hands
|
|
62
70
|
// over the build's own outDir, but the siteStyles resolve against the root,
|
|
63
|
-
// and locating the CHILD build's client dir (for the editor
|
|
71
|
+
// and locating the CHILD build's client dir (for the editor page mirror)
|
|
64
72
|
// needs the root the child resolves against.
|
|
65
73
|
let projectRoot: URL | undefined;
|
|
66
|
-
// Adapter name, finalized only at config:done
|
|
67
|
-
//
|
|
68
|
-
// entry is a plain bootable process (see discovery.ts).
|
|
74
|
+
// Adapter name, finalized only at config:done: the preview build reads it
|
|
75
|
+
// when previewBuild names no adapter of its own.
|
|
69
76
|
let adapterName: string | undefined;
|
|
70
|
-
// The site's Vite config, finalized at config:done: the
|
|
71
|
-
// through it, as they do in conlocaCMS's dev editor.
|
|
77
|
+
// The site's Vite config, finalized at config:done: the editor page and the
|
|
78
|
+
// siteStyles compile through it, as they do in conlocaCMS's dev editor. It
|
|
79
|
+
// holds the site's and integrations' plugins only; Astro adds its own core
|
|
80
|
+
// plugins to a separate config when it builds, so none reach the editor page.
|
|
72
81
|
let viteConfig: ViteUserConfig | undefined;
|
|
82
|
+
// Astro's base, as written: the editor page's URLs sit below it.
|
|
83
|
+
let astroBase = '/';
|
|
84
|
+
// Where the site's pages live and how Astro builds them: conloca.json lists the prerendered ones.
|
|
85
|
+
let srcDir: URL | undefined;
|
|
86
|
+
let serverOutput = false;
|
|
87
|
+
let i18n: AstroI18nLocales | undefined;
|
|
88
|
+
// The editor options, settled at config:done: conlocaCMS publishes its
|
|
89
|
+
// channel from its own config:setup, which may run after ours.
|
|
90
|
+
let inherited: InheritedOptions | undefined;
|
|
91
|
+
// Whether the parent build starts the preview build, read with the adapter
|
|
92
|
+
// the child will use: previewBuild.adapter, else the project's own.
|
|
93
|
+
const previewMode = () =>
|
|
94
|
+
previewBuildMode(options.previewBuild, (options.previewBuild || undefined)?.adapter?.name ?? adapterName);
|
|
95
|
+
// Whether the project has no adapter of its own, read in config:setup before the preview build switches to
|
|
96
|
+
// server output: such a site can use no server islands, so its dispatcher never runs on their route.
|
|
97
|
+
let projectStatic = false;
|
|
98
|
+
// The routes the preview build renders on demand (astro:routes:resolved), as Astro patterns.
|
|
99
|
+
let onDemand: string[] = [];
|
|
100
|
+
// The site's build: the routes it prerendered (astro:routes:resolved), handed to a D3 child.
|
|
101
|
+
let sitePrerendered: string[] = [];
|
|
102
|
+
// A D3 child: the routes its site's build prerendered, which it keeps prerendered (config:setup).
|
|
103
|
+
let keepPrerendered = new Set<string>();
|
|
104
|
+
// The preview build's mode (config:setup): a D4 child of a static site has no site build to ask.
|
|
105
|
+
let childServeMode: PreviewServeMode = 'd4';
|
|
73
106
|
|
|
74
107
|
return {
|
|
75
108
|
name: PACKAGE_NAME,
|
|
76
109
|
hooks: {
|
|
77
|
-
'astro:config:setup': ({ logger, injectRoute, updateConfig, config, addMiddleware }) => {
|
|
110
|
+
'astro:config:setup': async ({ logger, injectRoute, updateConfig, config, addMiddleware, command }) => {
|
|
78
111
|
// Optional-chained: unit tests drive this hook with a bare { logger }.
|
|
79
112
|
projectRoot = config?.root;
|
|
113
|
+
astroCommand = command;
|
|
114
|
+
projectStatic = config?.adapter === undefined;
|
|
80
115
|
// The agent skill: write-if-not-exists, fail-soft, and
|
|
81
116
|
// never from the recursive preview child (it would race the parent).
|
|
82
117
|
if (process.env[PREVIEW_BUILD_ENV_FLAG] !== '1' && config?.root) {
|
|
83
118
|
scaffoldSkill(fileURLToPath(config.root), logger);
|
|
84
119
|
}
|
|
85
|
-
logger.info(
|
|
86
|
-
`integration registered (endpoint=${options.endpoint}, previewMode=${
|
|
87
|
-
options.previewDomain ? 'preview-domain' : 'same-domain-runtime'
|
|
88
|
-
})`,
|
|
89
|
-
);
|
|
120
|
+
logger.info(`integration registered (endpoint=${options.endpoint ?? DEFAULT_EDITOR_ENDPOINT})`);
|
|
90
121
|
|
|
91
122
|
// Only the recursive preview build (which sets PREVIEW_BUILD_ENV_FLAG)
|
|
92
|
-
// mounts the
|
|
93
|
-
//
|
|
94
|
-
//
|
|
95
|
-
// auth boundary). Without a secret there is nothing to scope, so skip.
|
|
123
|
+
// mounts the SSR catch-all that renders previews; a site's normal
|
|
124
|
+
// build never exposes it. The route is fixed and public: the signed
|
|
125
|
+
// pass, checked against what this build bakes in, is the lock.
|
|
96
126
|
if (process.env[PREVIEW_BUILD_ENV_FLAG] === '1') {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
//
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
127
|
+
if (!repoUri) {
|
|
128
|
+
throw new Error(
|
|
129
|
+
`${PACKAGE_NAME}: could not determine repository identity for the preview build ` +
|
|
130
|
+
'(build in a Git checkout or set repoUri as an advanced override)',
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
// The project's own checkout answers git, not the directory the
|
|
134
|
+
// build was started from.
|
|
135
|
+
const projectGit = (args: string[]) => gitOutput(args, fileURLToPath(config.root));
|
|
136
|
+
const buildRef = resolveBuildRef(process.env, projectGit);
|
|
137
|
+
if (!buildRef.branch) {
|
|
138
|
+
throw new Error(
|
|
139
|
+
`${PACKAGE_NAME}: could not determine the branch the preview build serves (set CF_PAGES_BRANCH, ` +
|
|
140
|
+
'WORKERS_CI_BRANCH, VERCEL_GIT_COMMIT_REF or GITHUB_REF_NAME, or build in a git checkout on a branch)',
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
const childMode = previewChildMode(process.env);
|
|
144
|
+
childServeMode = childMode;
|
|
145
|
+
keepPrerendered = previewChildPrerendered(process.env);
|
|
146
|
+
const siteId = resolveBuildSiteId({
|
|
147
|
+
contentRoot: process.env.CONLOCA_CONTENT_ROOT,
|
|
148
|
+
override: options.siteId,
|
|
149
|
+
});
|
|
150
|
+
injectRoute({
|
|
151
|
+
pattern: `${PREVIEW_ROUTE_SEGMENT}/[...path]`,
|
|
152
|
+
entrypoint: '@conloca/cloud-astro/preview-handler',
|
|
153
|
+
prerender: false,
|
|
154
|
+
});
|
|
155
|
+
// The route above is the ENTRY DOOR — the only URL the endpoint can
|
|
156
|
+
// link to before a cookie exists. This interceptor is the serving
|
|
157
|
+
// surface: once the init hop sets the `Path=/` cookie, the browser
|
|
158
|
+
// sends the credential on every same-origin request, so internal
|
|
159
|
+
// links keep rendering drafts instead of silently falling back to
|
|
160
|
+
// published content after one page view. Fail-open — an
|
|
161
|
+
// uncredentialed visitor gets the ordinary page.
|
|
162
|
+
addMiddleware({ entrypoint: '@conloca/cloud-astro/site-middleware', order: 'pre' });
|
|
163
|
+
// The commit this preview build's checkout holds: a PR build may
|
|
164
|
+
// check out a merge commit rather than the commit CI hands over.
|
|
165
|
+
// Page-status compares the branch's latest commit with it, so the
|
|
166
|
+
// endpoint diffs against the content actually serving, not a newer
|
|
167
|
+
// commit whose deploy never landed. A checkout commit the
|
|
168
|
+
// endpoint does not have makes it render from the branch instead.
|
|
169
|
+
const buildCommit = projectGit(['rev-parse', 'HEAD']) ?? buildRef.gitSha;
|
|
170
|
+
// Bake the middleware's configuration into the artifact. Every value
|
|
171
|
+
// is already known here — endpoint and audience are integration
|
|
172
|
+
// options; repository, site, branch, base and commit are what this
|
|
173
|
+
// build was made from, and every pass must match the first three —
|
|
174
|
+
// so a correctly built preview deployment needs NO env vars set on
|
|
175
|
+
// it. `saasIssuer` stays unbaked unless configured: it is a property
|
|
176
|
+
// of the endpoint, not of the site.
|
|
177
|
+
previewOptsModule = {
|
|
178
|
+
saasEndpoint: options.endpoint ?? DEFAULT_EDITOR_ENDPOINT,
|
|
179
|
+
// The token's `aud` is the repo URI this build resolved.
|
|
180
|
+
saasAudience: repoUri,
|
|
181
|
+
...(options.saasIssuer ? { saasIssuer: options.saasIssuer } : {}),
|
|
182
|
+
...(buildCommit ? { buildCommit } : {}),
|
|
183
|
+
repo: repoUri,
|
|
184
|
+
siteId,
|
|
185
|
+
branch: buildRef.branch,
|
|
186
|
+
base: config.base ?? '/',
|
|
187
|
+
mode: childMode,
|
|
188
|
+
};
|
|
189
|
+
// The dispatcher's exit route answers with these: the values every pass is checked against, already
|
|
190
|
+
// narrowed by the two throws above.
|
|
191
|
+
childExit = {
|
|
192
|
+
repo: repoUri,
|
|
193
|
+
siteId,
|
|
194
|
+
branch: buildRef.branch,
|
|
195
|
+
audience: repoUri,
|
|
196
|
+
...(options.saasIssuer ? { issuer: options.saasIssuer } : {}),
|
|
197
|
+
};
|
|
198
|
+
// Preview-build path: the child build must emit an SSR
|
|
199
|
+
// artifact even when the primary build is fully static. Switch to
|
|
200
|
+
// server output, apply the preview adapter (the project's own
|
|
201
|
+
// adapter stays in force when it already has one), and write to a
|
|
202
|
+
// SEPARATE outDir so the child never clobbers the primary dist/.
|
|
203
|
+
const previewBuild = options.previewBuild || {};
|
|
204
|
+
// A site that names no adapter builds its preview server for Cloudflare Workers: next to it (D3) or apart (D4).
|
|
205
|
+
const adapter =
|
|
206
|
+
previewBuild.adapter ??
|
|
207
|
+
config.adapter ??
|
|
208
|
+
(childMode === 'd2' ? undefined : await cloudflareAdapter(config.root, childMode));
|
|
209
|
+
// Throws for a refused adapter — the parent already checked, this keeps the child honest.
|
|
210
|
+
previewBuildMode(previewBuild, adapter?.name);
|
|
211
|
+
// Astro allows exactly one deployment integration. Adding
|
|
212
|
+
// previewBuild.adapter next to the project's own would make the
|
|
213
|
+
// child build die on Astro's adapter-conflict error, which names
|
|
214
|
+
// neither this integration nor the option — fail here with the
|
|
215
|
+
// actual cause. The child inherits the project adapter, so the
|
|
216
|
+
// option is unnecessary on SSR projects.
|
|
217
|
+
if (previewBuild.adapter && config.adapter) {
|
|
218
|
+
const projectAdapterName = config.adapter.name;
|
|
219
|
+
throw new Error(
|
|
220
|
+
`${PACKAGE_NAME}: previewBuild.adapter conflicts with the project's own adapter ` +
|
|
221
|
+
`(${projectAdapterName}) — Astro allows only one deployment integration per build. ` +
|
|
222
|
+
'Remove previewBuild.adapter; the preview build inherits the project adapter.',
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
// Trailing slash so URL-joining treats it as a directory. Astro
|
|
226
|
+
// derives build.client/build.server from outDir BEFORE hooks run,
|
|
227
|
+
// so overriding outDir alone still writes into the primary dist/ —
|
|
228
|
+
// all three must move together.
|
|
229
|
+
// OUTSIDE dist/ on purpose, against the original design: a
|
|
230
|
+
// wholesale static upload of dist/ must never publish the preview
|
|
231
|
+
// server — and Astro clears dist/ at the start of a build, which
|
|
232
|
+
// would wipe a sibling artifact parked inside it.
|
|
233
|
+
const previewOut = previewOutDir(previewBuild, config.root);
|
|
234
|
+
// The site's extra config goes through its OWN updateConfig
|
|
235
|
+
// call: Astro deep-merges successive calls (concatenating
|
|
236
|
+
// vite.plugins and integrations), whereas spreading it into the
|
|
237
|
+
// object below would shallow-replace whole keys — a user `vite`
|
|
238
|
+
// entry silently dropped the required virtual-module plugin.
|
|
239
|
+
// Ours comes second so the required scalars (output, outDir,
|
|
240
|
+
// build dirs) always win; previewBuild.outDir is the supported
|
|
241
|
+
// override for the artifact location.
|
|
242
|
+
if (previewBuild.config) {
|
|
243
|
+
updateConfig(previewBuild.config);
|
|
244
|
+
}
|
|
245
|
+
updateConfig({
|
|
246
|
+
output: 'server',
|
|
247
|
+
outDir: previewOut,
|
|
248
|
+
build: { client: new URL('client/', previewOut), server: new URL('server/', previewOut) },
|
|
249
|
+
vite: {
|
|
250
|
+
// The middleware dynamically imports Cloudflare's runtime
|
|
251
|
+
// module (binding transport). The Cloudflare adapter resolves
|
|
252
|
+
// the scheme itself; every other adapter's Rollup pass fails
|
|
253
|
+
// the child build on it unless externalized. Off Cloudflare
|
|
254
|
+
// Workers the runtime import rejects and the middleware falls
|
|
255
|
+
// back to the global fetch.
|
|
256
|
+
build: { rollupOptions: { external: [/^cloudflare:/] } },
|
|
257
|
+
// Starlight renders Steps, Tabs and FileTree with satteri's
|
|
258
|
+
// htmlToHast. On Cloudflare Workers satteri resolves to a
|
|
259
|
+
// binding that can be neither installed nor run on workerd, so the child
|
|
260
|
+
// build fails to resolve it — swap in a stand-in there. The
|
|
261
|
+
// target is an absolute path: a bare name would be looked up
|
|
262
|
+
// from the Starlight file, which cannot see this package in an
|
|
263
|
+
// isolated (non-hoisted) node_modules layout.
|
|
264
|
+
...(adapter?.name === '@astrojs/cloudflare'
|
|
265
|
+
? {
|
|
266
|
+
resolve: {
|
|
267
|
+
alias: [
|
|
268
|
+
{
|
|
269
|
+
find: /^satteri$/,
|
|
270
|
+
replacement: resolveSatteriStandIn(),
|
|
271
|
+
},
|
|
272
|
+
],
|
|
235
273
|
},
|
|
274
|
+
}
|
|
275
|
+
: {}),
|
|
276
|
+
plugins: [
|
|
277
|
+
{
|
|
278
|
+
name: 'conloca-preview-opts',
|
|
279
|
+
resolveId(id: string) {
|
|
280
|
+
return id === VIRTUAL_PREVIEW_OPTS ? RESOLVED_PREVIEW_OPTS : undefined;
|
|
236
281
|
},
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
282
|
+
load(id: string) {
|
|
283
|
+
if (id !== RESOLVED_PREVIEW_OPTS) return undefined;
|
|
284
|
+
// Read lazily so the values assigned above are captured.
|
|
285
|
+
return `export default ${JSON.stringify(previewOptsModule ?? {})};`;
|
|
286
|
+
},
|
|
287
|
+
},
|
|
288
|
+
],
|
|
289
|
+
},
|
|
290
|
+
// The adapter must ride in BOTH ways: as `adapter` because the
|
|
291
|
+
// build gate checks `config.adapter` before creating the SSR
|
|
292
|
+
// bundle, and as an integration because only the adapter's own
|
|
293
|
+
// astro:config:done hook calls setAdapter — which is what the
|
|
294
|
+
// build pipeline actually renders through. (Astro unshifts
|
|
295
|
+
// config.adapter into the integration list BEFORE any hook runs,
|
|
296
|
+
// so setting `adapter` alone here is too late for the hooks.)
|
|
297
|
+
// A project that is already SSR keeps its own adapter untouched.
|
|
298
|
+
// A static site's Cloudflare adapter rides in the same way.
|
|
299
|
+
...(adapter !== undefined && adapter !== config.adapter ? { adapter, integrations: [adapter] } : {}),
|
|
300
|
+
});
|
|
250
301
|
}
|
|
251
302
|
},
|
|
252
303
|
/**
|
|
@@ -268,19 +319,38 @@ export function conlocaCloud(options: ConlocaCloudOptions): AstroIntegration {
|
|
|
268
319
|
* record, runs AFTER the `export const prerender` source parse, and
|
|
269
320
|
* Astro reads `prerender` back after every integration has run.
|
|
270
321
|
*
|
|
271
|
-
* Gated on the
|
|
272
|
-
*
|
|
273
|
-
* static output would buy nothing.
|
|
322
|
+
* Gated on the flag, as the config hook is: only the preview build
|
|
323
|
+
* mounts the interceptor these routes must reach.
|
|
274
324
|
*/
|
|
275
325
|
'astro:route:setup': ({ route, logger }) => {
|
|
276
326
|
if (process.env[PREVIEW_BUILD_ENV_FLAG] !== '1') return;
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
// type
|
|
327
|
+
const cms = route.component.endsWith('/internal/page-handler.astro');
|
|
328
|
+
const entry = /preview-handler(\.astro)?$/u.test(route.component);
|
|
329
|
+
// `route` (the pattern) and `type` exist at runtime but are absent from the public RouteOptions type.
|
|
330
|
+
const pattern = 'route' in route && typeof route.route === 'string' ? route.route : '';
|
|
331
|
+
// A D3 child keeps prerendered what its static site's build prerendered, endpoints included: on demand, a
|
|
332
|
+
// getStaticPaths route would lose its props, and the dispatcher would have to run on it in production. Its
|
|
333
|
+
// own on-demand routes (and every route of a D2 child) keep their own setting.
|
|
334
|
+
if (!cms && keepPrerendered.has(prerenderedKey(pattern, route.component))) {
|
|
335
|
+
route.prerender = true;
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
// Endpoints keep their prerendered data files: they emit no HTML
|
|
280
339
|
// to intercept, and one built from getStaticPaths would break on demand.
|
|
281
340
|
const routeType = (route as { type?: string }).type;
|
|
341
|
+
// A static site's separate preview deploy (D4) has no site build to ask, but every page of the site was
|
|
342
|
+
// prerendered: they stay so, or a getStaticPaths page would render on demand without its props.
|
|
343
|
+
if (
|
|
344
|
+
projectStatic &&
|
|
345
|
+
childServeMode === 'd4' &&
|
|
346
|
+
!cms &&
|
|
347
|
+
!entry &&
|
|
348
|
+
(routeType === undefined || routeType === 'page')
|
|
349
|
+
) {
|
|
350
|
+
route.prerender = true;
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
282
353
|
if (routeType !== undefined && routeType !== 'page') return;
|
|
283
|
-
if (route.prerender === false) return;
|
|
284
354
|
// ONLY the CMS page routes go on demand. Forcing every route stripped
|
|
285
355
|
// the getStaticPaths path-space partitioning: two root catch-alls (the
|
|
286
356
|
// CMS pages route and e.g. Starlight's docs route) both match any path
|
|
@@ -289,127 +359,132 @@ export function conlocaCloud(options: ConlocaCloudOptions): AstroIntegration {
|
|
|
289
359
|
// keep their static output (served by the adapter before the app), and
|
|
290
360
|
// App.match falls through a prerendered dynamic match to the first
|
|
291
361
|
// on-demand route — exactly the CMS handler for CMS pathnames.
|
|
292
|
-
if (!route.
|
|
362
|
+
if (!cms || route.prerender === false) return;
|
|
293
363
|
route.prerender = false;
|
|
294
364
|
logger.debug(`preview build: forcing on-demand rendering for ${route.component}`);
|
|
295
365
|
},
|
|
296
|
-
'astro:
|
|
366
|
+
'astro:routes:resolved': ({ routes }) => {
|
|
367
|
+
if (process.env[PREVIEW_BUILD_ENV_FLAG] !== '1') {
|
|
368
|
+
sitePrerendered = routes
|
|
369
|
+
.filter((route) => route.isPrerendered)
|
|
370
|
+
.map((route) => prerenderedKey(route.pattern, route.entrypoint));
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
// Astro injects the server-islands route whenever an adapter is set, but refuses islands without one: a static
|
|
374
|
+
// site uses none, so its dispatcher must not run on that route.
|
|
375
|
+
const used = projectStatic ? routes.filter((route) => route.pattern !== '/_server-islands/[name]') : routes;
|
|
376
|
+
onDemand = onDemandPatterns(used);
|
|
377
|
+
},
|
|
378
|
+
'astro:config:done': ({ config, logger }) => {
|
|
297
379
|
adapterName = config.adapter?.name;
|
|
298
380
|
viteConfig = config.vite;
|
|
381
|
+
astroBase = config.base ?? '/';
|
|
382
|
+
srcDir = config.srcDir;
|
|
383
|
+
serverOutput = config.output === 'server';
|
|
384
|
+
i18n = config.i18n;
|
|
385
|
+
inherited = resolveInheritedOptions(options, readEditorDescriptors());
|
|
386
|
+
// A refused or missing preview adapter fails `astro build` here,
|
|
387
|
+
// before the site builds, rather than after it. `astro dev`, `sync`
|
|
388
|
+
// and `preview` never start the preview build, so they are left
|
|
389
|
+
// alone. The child build checks its own.
|
|
390
|
+
if (astroCommand === 'build' && process.env[PREVIEW_BUILD_ENV_FLAG] !== '1') {
|
|
391
|
+
const mode = previewMode();
|
|
392
|
+
// D3 builds its preview server with @astrojs/cloudflare: missing, the build fails here, before the site builds.
|
|
393
|
+
if (mode === 'd3' && projectRoot !== undefined) resolveCloudflareAdapter(fileURLToPath(projectRoot));
|
|
394
|
+
if (mode === 'd2' && adapterName === undefined) {
|
|
395
|
+
logger.warn(
|
|
396
|
+
'previewBuild.adapter on a static site serves drafts from the deployed preview server; remove ' +
|
|
397
|
+
'previewBuild.adapter to serve them next to the static site on Cloudflare Workers instead',
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
299
401
|
},
|
|
300
|
-
'astro:build:done': async ({ dir, logger }) => {
|
|
402
|
+
'astro:build:done': async ({ dir, logger, pages }) => {
|
|
301
403
|
if (process.env[PREVIEW_BUILD_ENV_FLAG] === '1') {
|
|
302
|
-
// The recursive preview build sets this flag on the child
|
|
303
|
-
//
|
|
304
|
-
|
|
305
|
-
|
|
404
|
+
// The recursive preview build sets this flag on the child Astro process. Writing the editor page there
|
|
405
|
+
// again would race the parent, which writes its own; the child reports only what the parent reads.
|
|
406
|
+
logger.info('skipping the editor page: recursive preview build detected');
|
|
407
|
+
if (previewChildMode(process.env) === 'd4') {
|
|
408
|
+
await emitPreviewServerInfo(fileURLToPath(dir), {
|
|
409
|
+
root: fileURLToPath(projectRoot ?? dir),
|
|
410
|
+
repoUri,
|
|
411
|
+
siteIdOverride: options.siteId,
|
|
412
|
+
logger,
|
|
413
|
+
});
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
if (projectRoot === undefined) return;
|
|
417
|
+
// config:setup records the exit values in the child (below), or throws before the build starts.
|
|
418
|
+
const exit = childExit;
|
|
419
|
+
if (exit === undefined) throw new Error(`${PACKAGE_NAME}: the preview build recorded no exit values`);
|
|
420
|
+
const root = astroBase.replace(/\/+$/u, '');
|
|
421
|
+
await writePreviewRoutes(fileURLToPath(previewOutDir(options.previewBuild, projectRoot)), {
|
|
422
|
+
prerendered: (pages ?? []).map(({ pathname }) => normalizeTrailingSlash(`${root}/${pathname}`)),
|
|
423
|
+
onDemand: onDemand.map((pattern) => workerFirstPattern(pattern, astroBase)),
|
|
424
|
+
exit,
|
|
425
|
+
});
|
|
306
426
|
return;
|
|
307
427
|
}
|
|
308
428
|
|
|
309
429
|
// Astro always runs the config hooks first. No fallback: compiling
|
|
310
430
|
// against the working directory would build the canvas stylesheet for
|
|
311
431
|
// the wrong project without a word.
|
|
312
|
-
if (projectRoot === undefined || viteConfig === undefined) {
|
|
432
|
+
if (projectRoot === undefined || viteConfig === undefined || inherited === undefined) {
|
|
313
433
|
throw new Error(
|
|
314
434
|
`${PACKAGE_NAME}: astro:build:done ran before the config hooks recorded the project root and Vite config`,
|
|
315
435
|
);
|
|
316
436
|
}
|
|
317
437
|
|
|
318
|
-
|
|
319
|
-
//
|
|
320
|
-
|
|
321
|
-
// editor at the endpoint has no dev server to serve `/api/registry` from.
|
|
322
|
-
const descriptors = await resolveEditorDescriptors(logger);
|
|
323
|
-
|
|
324
|
-
const bundle = await bundleEditorAssets({
|
|
438
|
+
const mode = previewMode();
|
|
439
|
+
// The editor page: every build writes it.
|
|
440
|
+
await emitEditorPage({
|
|
325
441
|
dir: fileURLToPath(dir),
|
|
326
442
|
root: fileURLToPath(projectRoot),
|
|
443
|
+
base: astroBase,
|
|
327
444
|
vite: viteConfig,
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
445
|
+
endpoint: options.endpoint,
|
|
446
|
+
inherited,
|
|
447
|
+
repoUri,
|
|
448
|
+
siteIdOverride: options.siteId,
|
|
449
|
+
previewMode: mode,
|
|
450
|
+
pagesDir: fileURLToPath(new URL('pages/', srcDir ?? new URL('src/', projectRoot))),
|
|
451
|
+
serverOutput,
|
|
452
|
+
i18n,
|
|
333
453
|
logger,
|
|
334
454
|
});
|
|
335
455
|
|
|
336
|
-
//
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
projectRoot: process.cwd(),
|
|
350
|
-
logger,
|
|
351
|
-
});
|
|
352
|
-
|
|
353
|
-
// Build-time style discovery: read the discovery
|
|
354
|
-
// route's rendered page, collect its CSS, run the shared wrapper
|
|
355
|
-
// heuristic; results ride registration below. Opt-in via the option —
|
|
356
|
-
// and fail-soft throughout: a build must never die over preview
|
|
357
|
-
// styling.
|
|
358
|
-
let discovery: Awaited<ReturnType<typeof discoverStyleContext>>;
|
|
359
|
-
let routeCssArtifact: Awaited<ReturnType<typeof emitRouteCssArtifact>>;
|
|
360
|
-
if (options.fragmentPreviewRoute) {
|
|
361
|
-
const clientDir = fileURLToPath(dir);
|
|
362
|
-
const nodeEntry = fileURLToPath(new URL('../server/entry.mjs', dir));
|
|
363
|
-
discovery = await discoverStyleContext({
|
|
364
|
-
clientDir,
|
|
365
|
-
serverEntry: adapterName === '@astrojs/node' && existsSync(nodeEntry) ? nodeEntry : undefined,
|
|
366
|
-
route: options.fragmentPreviewRoute,
|
|
367
|
-
logger,
|
|
368
|
-
});
|
|
369
|
-
if (discovery?.routeCss) {
|
|
370
|
-
routeCssArtifact = await emitRouteCssArtifact(
|
|
371
|
-
clientDir,
|
|
372
|
-
discovery.routeCss,
|
|
373
|
-
resolveDeploymentUrl(options),
|
|
374
|
-
logger,
|
|
375
|
-
);
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
const registration = await registerBuild(options, repoUri, bundle, logger, {
|
|
380
|
-
hostWrappers: discovery?.hostWrappers,
|
|
381
|
-
routeCssArtifact: routeCssArtifact?.stylesheet,
|
|
382
|
-
componentManifest,
|
|
383
|
-
});
|
|
384
|
-
let registeredSecret: string | undefined;
|
|
385
|
-
if ('missing' in registration) reportSkippedRegistration(registration.missing, options, logger);
|
|
386
|
-
else registeredSecret = registration.previewSecret;
|
|
387
|
-
|
|
388
|
-
if (options.previewBuild === false) {
|
|
389
|
-
// The site explicitly opted out of the recursive preview build.
|
|
456
|
+
// Strict opt-in: only a previewBuild given as an object starts the second build. D4 builds it elsewhere.
|
|
457
|
+
const previewBuild = options.previewBuild || undefined;
|
|
458
|
+
if (previewBuild === undefined || mode === 'off' || mode === 'd4') return;
|
|
459
|
+
const previewOut = previewOutDir(previewBuild, projectRoot);
|
|
460
|
+
if (mode === 'd2') {
|
|
461
|
+
await spawnPreviewBuild(previewBuild, 'd2');
|
|
462
|
+
// The child build cannot write the editor files itself (it
|
|
463
|
+
// short-circuits this hook — see the recursion guard above), but in
|
|
464
|
+
// a deployment that serves the site and the preview from one
|
|
465
|
+
// Cloudflare Worker its client dir is THE asset set serving the site,
|
|
466
|
+
// so the editor files must be there too. Mirror them from this build's dist.
|
|
467
|
+
await mirrorEditorPage(fileURLToPath(dir), fileURLToPath(new URL('client/', previewOut)));
|
|
468
|
+
logger.info('editor page mirrored into the preview client dir');
|
|
390
469
|
return;
|
|
391
470
|
}
|
|
392
|
-
//
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
471
|
+
// D3: a failed preview build is isolated unless its server also serves the live site (finishD3Build decides).
|
|
472
|
+
let built = true;
|
|
473
|
+
try {
|
|
474
|
+
await spawnPreviewBuild({ ...previewBuild, required: true }, 'd3', sitePrerendered);
|
|
475
|
+
} catch (error) {
|
|
476
|
+
built = false;
|
|
477
|
+
logger.warn(`the preview build failed: ${messageOf(error)}`);
|
|
397
478
|
}
|
|
398
|
-
await
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
const previewOut = new URL(`${options.previewBuild?.outDir ?? '.conloca/preview-server'}/`, projectRoot);
|
|
408
|
-
await mirrorEditorAssets(fileURLToPath(dir), fileURLToPath(new URL('client/', previewOut)), [
|
|
409
|
-
...bundle.assetCorsPaths,
|
|
410
|
-
...(routeCssArtifact?.assetCorsPaths ?? []),
|
|
411
|
-
]);
|
|
412
|
-
logger.info('editor assets mirrored into the preview client dir');
|
|
479
|
+
await finishD3Build({
|
|
480
|
+
root: fileURLToPath(projectRoot),
|
|
481
|
+
dist: fileURLToPath(dir),
|
|
482
|
+
previewOut: fileURLToPath(previewOut),
|
|
483
|
+
base: astroBase,
|
|
484
|
+
endpoint: options.endpoint ?? DEFAULT_EDITOR_ENDPOINT,
|
|
485
|
+
built,
|
|
486
|
+
logger,
|
|
487
|
+
});
|
|
413
488
|
},
|
|
414
489
|
},
|
|
415
490
|
};
|
|
@@ -428,180 +503,35 @@ function resolveSatteriStandIn(): string {
|
|
|
428
503
|
}
|
|
429
504
|
}
|
|
430
505
|
|
|
431
|
-
/**
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
}
|
|
441
|
-
const previewUrl = new URL(
|
|
442
|
-
options.previewDomain.includes('://') ? options.previewDomain : `https://${options.previewDomain}`,
|
|
443
|
-
);
|
|
444
|
-
return previewUrl.origin;
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
/**
|
|
448
|
-
* ONE end-of-build block for a skipped registration — every prerequisite that
|
|
449
|
-
* was missing and what it costs — the last thing the integration prints.
|
|
450
|
-
* With `requireRegistration` it fails the build instead.
|
|
451
|
-
*/
|
|
452
|
-
function reportSkippedRegistration(
|
|
453
|
-
missing: string[],
|
|
454
|
-
options: ConlocaCloudOptions,
|
|
455
|
-
logger: AstroIntegrationLogger,
|
|
456
|
-
): void {
|
|
457
|
-
const summary = [
|
|
458
|
-
`editor at ${options.endpoint} NOT updated by this build`,
|
|
459
|
-
...missing.map((prerequisite) => ` missing: ${prerequisite}`),
|
|
460
|
-
" consequence: registration was skipped, so the editor keeps serving the previous build of this branch (or the default branch's, or nothing, if neither was ever registered)",
|
|
461
|
-
' check: conloca doctor --live',
|
|
462
|
-
].join('\n');
|
|
463
|
-
if (options.requireRegistration) {
|
|
464
|
-
throw new Error(`${PACKAGE_NAME}: ${summary}\n requireRegistration is on, so this build fails.`);
|
|
465
|
-
}
|
|
466
|
-
logger.warn(summary);
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
/** What `registerBuild` ended with: the preview secret, or every prerequisite whose absence made it skip. */
|
|
470
|
-
type RegistrationOutcome = { previewSecret: string } | { missing: string[] };
|
|
471
|
-
|
|
472
|
-
/**
|
|
473
|
-
* Registers the freshly built editor bundle at the endpoint. Soft-skip ONLY
|
|
474
|
-
* when registration was never attempted: the pre-request guards (no build
|
|
475
|
-
* token, no repo identity, no sha/branch, no base URL) each warn, and every
|
|
476
|
-
* missing prerequisite is returned at once, so one build names all that setup
|
|
477
|
-
* still lacks and an unregistered deploy still completes — the hook turns
|
|
478
|
-
* that into one end-of-build summary (or a failure under
|
|
479
|
-
* `requireRegistration`). Once a registration IS attempted, its failure
|
|
480
|
-
* FAILS THE BUILD: postRegistration throws on any
|
|
481
|
-
* deterministic 4xx and after exhausting the ≥500 retry backoff, and that
|
|
482
|
-
* error — carrying the HTTP status and response body — propagates. A green
|
|
483
|
-
* deploy whose registration silently went missing is the "deployed but
|
|
484
|
-
* editor unregistered" class this project exists to kill.
|
|
485
|
-
*/
|
|
486
|
-
async function registerBuild(
|
|
487
|
-
options: ConlocaCloudOptions,
|
|
488
|
-
repoUri: string | undefined,
|
|
489
|
-
bundle: BundleEditorAssetsResult,
|
|
490
|
-
logger: AstroIntegrationLogger,
|
|
491
|
-
discovery: {
|
|
492
|
-
hostWrappers?: DiscoveredStyleContext['hostWrappers'];
|
|
493
|
-
routeCssArtifact?: BundledStylesheet;
|
|
494
|
-
componentManifest?: ComponentManifestArtifact;
|
|
495
|
-
} = {},
|
|
496
|
-
): Promise<RegistrationOutcome> {
|
|
497
|
-
const missing: string[] = [];
|
|
498
|
-
const buildToken = options.buildToken ?? process.env[BUILD_TOKEN_ENV_VAR];
|
|
499
|
-
if (!buildToken) {
|
|
500
|
-
logger.warn(`skipping registration: no build token (set buildToken or ${BUILD_TOKEN_ENV_VAR})`);
|
|
501
|
-
missing.push(`build token (set buildToken or ${BUILD_TOKEN_ENV_VAR})`);
|
|
502
|
-
}
|
|
503
|
-
|
|
504
|
-
if (!repoUri) {
|
|
505
|
-
logger.warn(
|
|
506
|
-
'skipping registration: could not determine repository identity ' +
|
|
507
|
-
'(build in a supported CI environment or Git checkout, or set repoUri)',
|
|
508
|
-
);
|
|
509
|
-
missing.push('repository identity (build in a supported CI environment or Git checkout, or set repoUri)');
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
const { branch } = resolveBuildRef();
|
|
513
|
-
// CI may hand over the branch's latest commit to register; the checkout is what was built.
|
|
514
|
-
const commits = registrationCommits({
|
|
515
|
-
eventCommitSha: process.env.CF_PAGES_COMMIT_SHA,
|
|
516
|
-
checkoutCommitSha: gitOutput(['rev-parse', 'HEAD']),
|
|
517
|
-
});
|
|
518
|
-
const gitSha = commits.gitSha;
|
|
519
|
-
if (!gitSha || !branch) {
|
|
520
|
-
logger.warn(
|
|
521
|
-
'skipping registration: could not resolve the git sha/branch ' +
|
|
522
|
-
'(set CF_PAGES_COMMIT_SHA and CF_PAGES_BRANCH, or build inside a git checkout)',
|
|
523
|
-
);
|
|
524
|
-
missing.push('git sha/branch (set CF_PAGES_COMMIT_SHA and CF_PAGES_BRANCH, or build inside a git checkout)');
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
const deploymentUrl = resolveDeploymentUrl(options);
|
|
528
|
-
const baseUrl = deploymentUrl;
|
|
529
|
-
if (!baseUrl) {
|
|
530
|
-
logger.warn(
|
|
531
|
-
'skipping registration: no public base URL for the editor bundle (set previewDomain, publicBaseUrl, or CF_PAGES_URL)',
|
|
532
|
-
);
|
|
533
|
-
missing.push('public base URL for the editor bundle (set previewDomain, publicBaseUrl, or CF_PAGES_URL)');
|
|
534
|
-
}
|
|
535
|
-
// Restates the guards above, so the values below are typed as set.
|
|
536
|
-
if (!buildToken || !repoUri || !gitSha || !branch || !baseUrl) return { missing };
|
|
537
|
-
|
|
538
|
-
// Resolve the deployable-site id (STRICT): a throw here FAILS THE BUILD —
|
|
539
|
-
// after the soft skip guards above, so only a build that is actually
|
|
540
|
-
// registering can die on it. A silently mis-attached registration points
|
|
541
|
-
// the editor at the wrong site, which is the same miss as never having
|
|
542
|
-
// registered at all. The channel is CONLOCA_CONTENT_ROOT (published by
|
|
543
|
-
// conlocaCMS); options.siteId is the CMS-less-build override.
|
|
544
|
-
const siteId = resolveBuildSiteId({
|
|
545
|
-
contentRoot: process.env.CONLOCA_CONTENT_ROOT,
|
|
546
|
-
override: options.siteId,
|
|
547
|
-
});
|
|
548
|
-
|
|
549
|
-
// NO catch: from here on the registration is ATTEMPTED, and an attempted
|
|
550
|
-
// registration's failure fails the build. postRegistration already retried
|
|
551
|
-
// transient (≥500 / network) failures with backoff; what escapes is either
|
|
552
|
-
// a deterministic 4xx (unknown siteId, bad token — fix and rebuild) or an
|
|
553
|
-
// exhausted retry budget, and the error message carries the HTTP status +
|
|
554
|
-
// response body verbatim.
|
|
555
|
-
logger.info(
|
|
556
|
-
'registering at the endpoint (transient failures retry, with up to ~2.5 minutes of waiting between attempts)',
|
|
557
|
-
);
|
|
558
|
-
const { previewSecret } = await postRegistration({
|
|
559
|
-
endpoint: options.endpoint,
|
|
560
|
-
buildToken,
|
|
561
|
-
repoUri,
|
|
562
|
-
gitSha,
|
|
563
|
-
siteId,
|
|
564
|
-
branch,
|
|
565
|
-
editorBundleUrl: new URL(bundle.url, baseUrl).toString(),
|
|
566
|
-
bundleHash: bundle.hash,
|
|
567
|
-
...(bundle.stylesheet === undefined
|
|
568
|
-
? {}
|
|
569
|
-
: {
|
|
570
|
-
stylesheetUrl: new URL(bundle.stylesheet.url, baseUrl).toString(),
|
|
571
|
-
stylesheetHash: bundle.stylesheet.hash,
|
|
572
|
-
}),
|
|
573
|
-
...(discovery.hostWrappers === undefined ? {} : { hostWrappers: discovery.hostWrappers }),
|
|
574
|
-
...(discovery.routeCssArtifact === undefined
|
|
575
|
-
? {}
|
|
576
|
-
: {
|
|
577
|
-
routeCssUrl: new URL(discovery.routeCssArtifact.url, baseUrl).toString(),
|
|
578
|
-
routeCssHash: discovery.routeCssArtifact.hash,
|
|
579
|
-
}),
|
|
580
|
-
...(discovery.componentManifest === undefined
|
|
581
|
-
? {}
|
|
582
|
-
: {
|
|
583
|
-
componentManifestUrl: new URL(discovery.componentManifest.url, baseUrl).toString(),
|
|
584
|
-
componentManifestHash: discovery.componentManifest.hash,
|
|
585
|
-
}),
|
|
586
|
-
deploymentUrl,
|
|
587
|
-
buildId: commits.buildId,
|
|
588
|
-
clientVersion: packageVersion(),
|
|
589
|
-
});
|
|
590
|
-
logger.info(`registration complete (gitSha=${gitSha}, branch=${branch}, buildId=${commits.buildId})`);
|
|
591
|
-
return { previewSecret };
|
|
592
|
-
}
|
|
506
|
+
/** The options the editor page made obsolete; naming one fails the build so the config gets updated. */
|
|
507
|
+
const REMOVED_OPTIONS = [
|
|
508
|
+
'buildToken',
|
|
509
|
+
'requireRegistration',
|
|
510
|
+
'publicBaseUrl',
|
|
511
|
+
'previewDomain',
|
|
512
|
+
'editorBundlePlugins',
|
|
513
|
+
'fragmentPreviewRoute',
|
|
514
|
+
] as const;
|
|
593
515
|
|
|
516
|
+
/** Refuses a removed option, a malformed endpoint, or one that is not an `app.` address, at config load. */
|
|
594
517
|
function validateOptions(options: ConlocaCloudOptions): void {
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
518
|
+
for (const name of REMOVED_OPTIONS) {
|
|
519
|
+
if (name in options) {
|
|
520
|
+
throw new Error(
|
|
521
|
+
`${PACKAGE_NAME}: option "${name}" was removed — run \`npx @conloca/cli connect\` to update this config`,
|
|
522
|
+
);
|
|
523
|
+
}
|
|
600
524
|
}
|
|
525
|
+
if (options.endpoint === undefined) return;
|
|
601
526
|
try {
|
|
602
527
|
// Throws on malformed URLs; we don't keep the parsed value.
|
|
603
528
|
new URL(options.endpoint);
|
|
604
529
|
} catch {
|
|
605
530
|
throw new Error(`${PACKAGE_NAME}: endpoint must be a valid URL, got ${options.endpoint}`);
|
|
606
531
|
}
|
|
532
|
+
if (editorUiOrigin(options.endpoint) === null) {
|
|
533
|
+
throw new Error(
|
|
534
|
+
`${PACKAGE_NAME}: endpoint must be the workspace editor's app. address (for example https://app.conloca.com), got ${options.endpoint}`,
|
|
535
|
+
);
|
|
536
|
+
}
|
|
607
537
|
}
|