@conloca/astro-cms 0.1.13 → 0.1.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/collections.d.mts +3 -2
- package/dist/collections.d.mts.map +1 -1
- package/dist/collections.mjs +15 -6
- package/dist/collections.mjs.map +1 -1
- package/dist/components.d.mts +4 -24
- package/dist/components.d.mts.map +1 -1
- package/dist/components.mjs +99 -40
- package/dist/components.mjs.map +1 -1
- package/dist/hydration-scanner-B0NjYkZ_.mjs +109 -0
- package/dist/hydration-scanner-B0NjYkZ_.mjs.map +1 -0
- package/dist/hydration.d.mts +2 -2
- package/dist/hydration.mjs +1 -1
- package/dist/index.d.mts +9 -54
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +17 -778
- package/dist/index.mjs.map +1 -1
- package/dist/internal/acorn-default.mjs +29 -0
- package/dist/{cms-handler.d.mts → internal/cms-handler.d.mts} +5 -1
- package/dist/internal/cms-handler.d.mts.map +1 -0
- package/dist/{cms-handler.mjs → internal/cms-handler.mjs} +63 -20
- package/dist/internal/cms-handler.mjs.map +1 -0
- package/dist/internal/page-handler.astro +541 -0
- package/dist/lib/hydration-script.d.mts +1 -1
- package/dist/{loader-W3i9kf3p.mjs → loader-By-AQWHc.mjs} +10 -3
- package/dist/loader-By-AQWHc.mjs.map +1 -0
- package/dist/node/content.d.mts +19 -0
- package/dist/node/content.d.mts.map +1 -0
- package/dist/node/content.mjs +4 -0
- package/dist/node/index.d.mts +52 -0
- package/dist/node/index.d.mts.map +1 -0
- package/dist/node/index.mjs +753 -0
- package/dist/node/index.mjs.map +1 -0
- package/dist/{serialize-props-DqfDQeFl.mjs → serialize-props-Dhv_MnkY.mjs} +3 -3
- package/dist/{serialize-props-DqfDQeFl.mjs.map → serialize-props-Dhv_MnkY.mjs.map} +1 -1
- package/dist/{types-Bsd_qENn.d.mts → types-CVQNmPxF.d.mts} +1 -1
- package/dist/{types-Bsd_qENn.d.mts.map → types-CVQNmPxF.d.mts.map} +1 -1
- package/dist/{withHydration-692fMySA.mjs → withHydration-EzyzBQ-Y.mjs} +5 -1
- package/dist/withHydration-EzyzBQ-Y.mjs.map +1 -0
- package/dist/{withHydration-BGgQDE-H.d.mts → withHydration-HIvO9ysY.d.mts} +5 -1
- package/dist/withHydration-HIvO9ysY.d.mts.map +1 -0
- package/package.json +37 -27
- package/dist/cms-handler.d.mts.map +0 -1
- package/dist/cms-handler.mjs.map +0 -1
- package/dist/handlers/page-handler.astro +0 -294
- package/dist/loader-W3i9kf3p.mjs.map +0 -1
- package/dist/withHydration-692fMySA.mjs.map +0 -1
- package/dist/withHydration-BGgQDE-H.d.mts.map +0 -1
|
@@ -1,294 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
/**
|
|
3
|
-
* Page Handler Component
|
|
4
|
-
*
|
|
5
|
-
* This Astro component is injected via injectRoute() to handle content pages.
|
|
6
|
-
* It implements getStaticPaths() for static page generation and renders
|
|
7
|
-
* content using the Puck Render component with an optional Layout wrapper.
|
|
8
|
-
*
|
|
9
|
-
* MDX blocks are evaluated at build time and rendered as React components.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
import { Render, resolveAllData } from '@puckeditor/core';
|
|
13
|
-
import type { ComponentType } from 'react';
|
|
14
|
-
|
|
15
|
-
// MDX evaluation imports
|
|
16
|
-
import { createContentAPI } from '@conloca/content-api/node';
|
|
17
|
-
import { evaluateMDXBlocks } from '@conloca/mdx/node';
|
|
18
|
-
import { createPageRendererWithBlocks, RenderWithHydration } from '@conloca/astro-cms/components';
|
|
19
|
-
|
|
20
|
-
// Hydration support
|
|
21
|
-
import { hasHydratableComponents, extractSlugFromPathname } from '@conloca/astro-cms';
|
|
22
|
-
import type { DataCollectionEntry, DataContext, PageData, PageReference, ResolvedRoutingConfig } from '@conloca/astro-cms';
|
|
23
|
-
|
|
24
|
-
// Virtual modules configured by the Vite plugin
|
|
25
|
-
import routingConfig from 'virtual:conloca-routing-config';
|
|
26
|
-
import Layout from 'virtual:conloca-layout';
|
|
27
|
-
import { getAllPages, getPage, getDataCollection, getPagesByPrefix, contentOptions } from 'virtual:conloca-page-api';
|
|
28
|
-
import puckConfig from 'virtual:conloca-puck-config';
|
|
29
|
-
|
|
30
|
-
import type { GetStaticPaths } from 'astro';
|
|
31
|
-
|
|
32
|
-
// Default to prerender: true for static site generation
|
|
33
|
-
export const prerender = true;
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Generate static paths for all content pages.
|
|
37
|
-
*
|
|
38
|
-
* Iterates through all configured routes and their collections
|
|
39
|
-
* to discover pages and generate the params/props for each.
|
|
40
|
-
*/
|
|
41
|
-
export const getStaticPaths: GetStaticPaths = async () => {
|
|
42
|
-
// Import config inside getStaticPaths to ensure it's available at build time
|
|
43
|
-
// (Astro hoists getStaticPaths and runs it before module initialization)
|
|
44
|
-
const config = routingConfig as ResolvedRoutingConfig;
|
|
45
|
-
|
|
46
|
-
const paths: {
|
|
47
|
-
params: { slug: string | undefined };
|
|
48
|
-
props: { routeName: string; collection: string; pathname: string };
|
|
49
|
-
}[] = [];
|
|
50
|
-
|
|
51
|
-
// Skip if routing is disabled
|
|
52
|
-
if (!config.enabled) {
|
|
53
|
-
return paths;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// For each configured route
|
|
57
|
-
for (const [routeName, routeConfig] of Object.entries(config.routes)) {
|
|
58
|
-
try {
|
|
59
|
-
// Get all pages from this route's collection
|
|
60
|
-
const pages = await getAllPages(routeConfig.collection);
|
|
61
|
-
|
|
62
|
-
for (const page of pages) {
|
|
63
|
-
// Convert pathname to slug params
|
|
64
|
-
// Pattern: /blog/[...slug] + pathname: /blog/post-1 = slug: 'post-1'
|
|
65
|
-
const slug = extractSlugFromPathname(page.pathname, routeConfig.pattern);
|
|
66
|
-
|
|
67
|
-
paths.push({
|
|
68
|
-
params: { slug },
|
|
69
|
-
props: {
|
|
70
|
-
routeName,
|
|
71
|
-
collection: routeConfig.collection,
|
|
72
|
-
pathname: page.pathname,
|
|
73
|
-
},
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
} catch (error) {
|
|
77
|
-
console.error(`[Conloca] Error loading pages for route '${routeName}':`, error);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
return paths;
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
// Props passed from getStaticPaths
|
|
85
|
-
interface Props {
|
|
86
|
-
routeName: string;
|
|
87
|
-
collection: string;
|
|
88
|
-
pathname: string;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
const { routeName, collection, pathname } = Astro.props as Props;
|
|
92
|
-
|
|
93
|
-
// Cast routing config for component body
|
|
94
|
-
const config = routingConfig as ResolvedRoutingConfig;
|
|
95
|
-
|
|
96
|
-
// Load page data
|
|
97
|
-
let page: PageData | undefined;
|
|
98
|
-
let loadError: Error | undefined;
|
|
99
|
-
|
|
100
|
-
try {
|
|
101
|
-
page = await getPage(pathname, collection);
|
|
102
|
-
|
|
103
|
-
// Enrich with route information
|
|
104
|
-
if (page && config.routes[routeName]) {
|
|
105
|
-
const routeConfig = config.routes[routeName];
|
|
106
|
-
page.route = {
|
|
107
|
-
name: routeName,
|
|
108
|
-
pattern: routeConfig.pattern,
|
|
109
|
-
meta: routeConfig.meta,
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
} catch (error) {
|
|
113
|
-
loadError = error as Error;
|
|
114
|
-
|
|
115
|
-
// Handle 404 based on fallback configuration
|
|
116
|
-
if ((error as { code?: string }).code === 'PAGE_NOT_FOUND') {
|
|
117
|
-
if (config.fallback === 'passthrough') {
|
|
118
|
-
// Let Astro continue to next route
|
|
119
|
-
return Astro.redirect('/404', 307);
|
|
120
|
-
}
|
|
121
|
-
// Return 404 response
|
|
122
|
-
return new Response('Page not found', { status: 404 });
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// Re-throw other errors
|
|
126
|
-
throw error;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
// Build metadata with collection data for resolvers
|
|
130
|
-
let dataContext: DataContext | undefined;
|
|
131
|
-
|
|
132
|
-
const routeConfig = config.routes[routeName];
|
|
133
|
-
const hasCollections = routeConfig?.dataBindings?.collections?.length;
|
|
134
|
-
const hasPages = routeConfig?.dataBindings?.pages?.prefix;
|
|
135
|
-
|
|
136
|
-
if (hasCollections || hasPages) {
|
|
137
|
-
const collections: Record<string, DataCollectionEntry[]> = {};
|
|
138
|
-
let pages: PageReference[] | undefined;
|
|
139
|
-
|
|
140
|
-
// Fetch collections
|
|
141
|
-
if (hasCollections) {
|
|
142
|
-
for (const collectionName of routeConfig.dataBindings.collections) {
|
|
143
|
-
try {
|
|
144
|
-
const locale = routeConfig.dataBindings.locale ?? contentOptions.locale;
|
|
145
|
-
collections[collectionName] = await getDataCollection(collectionName, locale);
|
|
146
|
-
} catch (error) {
|
|
147
|
-
console.error(`[Conloca] Failed to fetch collection '${collectionName}':`, error);
|
|
148
|
-
collections[collectionName] = [];
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
// Fetch pages by prefix
|
|
154
|
-
if (hasPages) {
|
|
155
|
-
try {
|
|
156
|
-
const pagesConfig = routeConfig.dataBindings.pages;
|
|
157
|
-
pages = await getPagesByPrefix(pagesConfig.prefix, {
|
|
158
|
-
sort: pagesConfig.sort,
|
|
159
|
-
limit: pagesConfig.limit,
|
|
160
|
-
});
|
|
161
|
-
} catch (error) {
|
|
162
|
-
console.error(`[Conloca] Failed to fetch pages with prefix '${routeConfig.dataBindings.pages.prefix}':`, error);
|
|
163
|
-
pages = [];
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
dataContext = {
|
|
168
|
-
collections,
|
|
169
|
-
pages,
|
|
170
|
-
locale: routeConfig.dataBindings.locale ?? contentOptions.locale,
|
|
171
|
-
siteName: contentOptions.siteName,
|
|
172
|
-
};
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
// Handle missing puckData gracefully
|
|
176
|
-
const hasPuckData = page?.puckData && Object.keys(page.puckData).length > 0;
|
|
177
|
-
|
|
178
|
-
// Resolve component data if we have bindings
|
|
179
|
-
let resolvedPuckData = page?.puckData;
|
|
180
|
-
|
|
181
|
-
if (hasPuckData && page && dataContext) {
|
|
182
|
-
try {
|
|
183
|
-
resolvedPuckData = await resolveAllData(page.puckData, puckConfig, {
|
|
184
|
-
metadata: dataContext,
|
|
185
|
-
});
|
|
186
|
-
} catch (error) {
|
|
187
|
-
console.error('[Conloca] resolveAllData failed:', error);
|
|
188
|
-
// Fall back to original puckData if resolution fails
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
// Check if page has hydratable components (for conditional script injection)
|
|
193
|
-
const needsHydration = hasPuckData && resolvedPuckData && hasHydratableComponents(resolvedPuckData, puckConfig);
|
|
194
|
-
|
|
195
|
-
// Create PageRenderer with MDX blocks support
|
|
196
|
-
// This evaluates all MDX blocks at build time and creates a renderer with them baked in
|
|
197
|
-
let PageRenderer: ComponentType | undefined;
|
|
198
|
-
|
|
199
|
-
if (hasPuckData && page && resolvedPuckData) {
|
|
200
|
-
try {
|
|
201
|
-
// Create ContentAPI for MDX evaluation using options from virtual module
|
|
202
|
-
const contentApi = await createContentAPI({
|
|
203
|
-
contentRoot: contentOptions.contentRoot,
|
|
204
|
-
canvasDir: contentOptions.canvasDir,
|
|
205
|
-
});
|
|
206
|
-
|
|
207
|
-
// Evaluate MDX blocks - converts MDX content to React components
|
|
208
|
-
const mdxComponents = await evaluateMDXBlocks(contentApi, contentOptions.locale);
|
|
209
|
-
|
|
210
|
-
// Create renderer with MDX components baked in via closure
|
|
211
|
-
PageRenderer = createPageRendererWithBlocks(puckConfig, resolvedPuckData, mdxComponents);
|
|
212
|
-
} catch (error) {
|
|
213
|
-
// If MDX evaluation fails, log and fall back to plain Render
|
|
214
|
-
console.error('[Conloca] MDX evaluation failed, falling back to plain render:', error);
|
|
215
|
-
PageRenderer = undefined;
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
---
|
|
219
|
-
|
|
220
|
-
{Layout && page ? (
|
|
221
|
-
<Layout title={page.title ?? 'Page'} description={page.description}>
|
|
222
|
-
{hasPuckData && PageRenderer ? (
|
|
223
|
-
<PageRenderer />
|
|
224
|
-
) : hasPuckData && resolvedPuckData ? (
|
|
225
|
-
needsHydration ? (
|
|
226
|
-
<RenderWithHydration config={puckConfig} data={resolvedPuckData} />
|
|
227
|
-
) : (
|
|
228
|
-
<Render config={puckConfig} data={resolvedPuckData} />
|
|
229
|
-
)
|
|
230
|
-
) : (
|
|
231
|
-
<div class="conloca-empty-page">
|
|
232
|
-
<p>This page has no content yet.</p>
|
|
233
|
-
</div>
|
|
234
|
-
)}
|
|
235
|
-
</Layout>
|
|
236
|
-
) : page ? (
|
|
237
|
-
<html lang="en">
|
|
238
|
-
<head>
|
|
239
|
-
<meta charset="UTF-8" />
|
|
240
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
241
|
-
<title>{page.title || 'Untitled'}</title>
|
|
242
|
-
{page.description && <meta name="description" content={page.description} />}
|
|
243
|
-
{needsHydration && <script>import 'virtual:conloca-hydration-entry'</script>}
|
|
244
|
-
</head>
|
|
245
|
-
<body>
|
|
246
|
-
{hasPuckData && PageRenderer ? (
|
|
247
|
-
<PageRenderer />
|
|
248
|
-
) : hasPuckData && resolvedPuckData ? (
|
|
249
|
-
needsHydration ? (
|
|
250
|
-
<RenderWithHydration config={puckConfig} data={resolvedPuckData} />
|
|
251
|
-
) : (
|
|
252
|
-
<Render config={puckConfig} data={resolvedPuckData} />
|
|
253
|
-
)
|
|
254
|
-
) : (
|
|
255
|
-
<div class="conloca-empty-page">
|
|
256
|
-
<p>This page has no content yet.</p>
|
|
257
|
-
</div>
|
|
258
|
-
)}
|
|
259
|
-
</body>
|
|
260
|
-
</html>
|
|
261
|
-
) : (
|
|
262
|
-
<html lang="en">
|
|
263
|
-
<head>
|
|
264
|
-
<meta charset="UTF-8" />
|
|
265
|
-
<title>Error</title>
|
|
266
|
-
</head>
|
|
267
|
-
<body>
|
|
268
|
-
<div class="conloca-error">
|
|
269
|
-
<h1>Error Loading Page</h1>
|
|
270
|
-
<p>{loadError?.message || 'An unknown error occurred.'}</p>
|
|
271
|
-
</div>
|
|
272
|
-
</body>
|
|
273
|
-
</html>
|
|
274
|
-
)}
|
|
275
|
-
|
|
276
|
-
{/* Hydration script - conditionally loaded only on pages with hydratable components */}
|
|
277
|
-
{Layout && needsHydration && <script>import 'virtual:conloca-hydration-entry'</script>}
|
|
278
|
-
|
|
279
|
-
<style>
|
|
280
|
-
.conloca-empty-page {
|
|
281
|
-
padding: 2rem;
|
|
282
|
-
text-align: center;
|
|
283
|
-
color: #666;
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
.conloca-error {
|
|
287
|
-
padding: 2rem;
|
|
288
|
-
text-align: center;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
.conloca-error h1 {
|
|
292
|
-
color: #c00;
|
|
293
|
-
}
|
|
294
|
-
</style>
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"loader-W3i9kf3p.mjs","names":[],"sources":["../src/loader.ts"],"sourcesContent":["import { createContentAPI } from '@conloca/content-api/node';\nimport type { Loader, LoaderContext } from 'astro/loaders';\n\nexport interface ConlocaLoaderOptions {\n collection: string;\n contentRoot: string;\n site?: string;\n /** Collection kind. If not provided, defaults to 'block' for 'blocks' collection, 'page' otherwise. */\n kind?: 'page' | 'block' | 'data';\n}\n\n/**\n * Custom Astro content loader for Conloca CMS.\n * Loads content from the Conloca content directory into Astro collections.\n */\nexport function conlocaLoader(options: ConlocaLoaderOptions): Loader {\n return {\n name: 'conloca-loader',\n async load(context: LoaderContext): Promise<void> {\n const { store, parseData, generateDigest, logger } = context;\n\n // Create content API instance\n const api = await createContentAPI({ contentRoot: options.contentRoot });\n\n // Use provided kind, or fall back to inferring from collection name\n const kind = options.kind ?? (options.collection === 'blocks' ? 'block' : 'page');\n\n // List all content for this collection\n // Note: blocks and data don't have a site, so only pass site for pages\n const content = Array.from(\n api.listAllContent({\n collection: options.collection,\n kind,\n site: kind === 'page' ? options.site : undefined,\n }),\n );\n\n store.clear();\n\n for (const manifest of content) {\n for (const [locale, localeVersion] of Object.entries(manifest.locales)) {\n if (!localeVersion) continue;\n\n const entryId = `${manifest.id}/${locale}`;\n\n // For data and page collections, fetch full content to get the actual content fields\n let dataContent: Record<string, unknown> | undefined;\n let puckData: unknown | undefined;\n\n if (kind === 'data') {\n const fullContent = await api.getLocalized(manifest.id, locale);\n if (fullContent?.localized.content?.data) {\n dataContent = fullContent.localized.content.data;\n }\n } else if (kind === 'page') {\n // For page collections, fetch full content to get puckData\n // This enables getCollection('pages') to return entries with puckData included\n const fullContent = await api.getLocalized(manifest.id, locale);\n if (fullContent?.localized.content?.puckData) {\n puckData = fullContent.localized.content.puckData;\n }\n }\n\n const data: Record<string, unknown> = {\n id: manifest.id,\n locale,\n ...(localeVersion.pathname && { pathname: localeVersion.pathname }),\n ...(localeVersion.name && { name: localeVersion.name }),\n ...localeVersion.meta,\n publishAt: localeVersion.publishAt,\n unpublishAt: localeVersion.unpublishAt,\n // Include the data content for data collections\n ...(dataContent && { data: dataContent }),\n };\n\n // Include puckData for page collections (added separately to avoid spread type issues)\n if (puckData !== undefined) {\n data.puckData = puckData;\n }\n\n const parsedData = await parseData({ id: entryId, data });\n\n store.set({\n id: entryId,\n data: parsedData,\n digest: generateDigest(localeVersion.etag),\n });\n }\n }\n\n logger.info(`Loaded ${store.keys().length} entries from ${options.collection}`);\n },\n };\n}\n"],"mappings":";;;;;;;AAeA,SAAgB,cAAc,SAAuC;AACnE,QAAO;EACL,MAAM;EACN,MAAM,KAAK,SAAuC;GAChD,MAAM,EAAE,OAAO,WAAW,gBAAgB,WAAW;GAGrD,MAAM,MAAM,MAAM,iBAAiB,EAAE,aAAa,QAAQ,aAAa,CAAC;GAGxE,MAAM,OAAO,QAAQ,SAAS,QAAQ,eAAe,WAAW,UAAU;GAI1E,MAAM,UAAU,MAAM,KACpB,IAAI,eAAe;IACjB,YAAY,QAAQ;IACpB;IACA,MAAM,SAAS,SAAS,QAAQ,OAAO;IACxC,CAAC,CACH;AAED,SAAM,OAAO;AAEb,QAAK,MAAM,YAAY,QACrB,MAAK,MAAM,CAAC,QAAQ,kBAAkB,OAAO,QAAQ,SAAS,QAAQ,EAAE;AACtE,QAAI,CAAC,cAAe;IAEpB,MAAM,UAAU,GAAG,SAAS,GAAG,GAAG;IAGlC,IAAI;IACJ,IAAI;AAEJ,QAAI,SAAS,QAAQ;KACnB,MAAM,cAAc,MAAM,IAAI,aAAa,SAAS,IAAI,OAAO;AAC/D,SAAI,aAAa,UAAU,SAAS,KAClC,eAAc,YAAY,UAAU,QAAQ;eAErC,SAAS,QAAQ;KAG1B,MAAM,cAAc,MAAM,IAAI,aAAa,SAAS,IAAI,OAAO;AAC/D,SAAI,aAAa,UAAU,SAAS,SAClC,YAAW,YAAY,UAAU,QAAQ;;IAI7C,MAAM,OAAgC;KACpC,IAAI,SAAS;KACb;KACA,GAAI,cAAc,YAAY,EAAE,UAAU,cAAc,UAAU;KAClE,GAAI,cAAc,QAAQ,EAAE,MAAM,cAAc,MAAM;KACtD,GAAG,cAAc;KACjB,WAAW,cAAc;KACzB,aAAa,cAAc;KAE3B,GAAI,eAAe,EAAE,MAAM,aAAa;KACzC;AAGD,QAAI,aAAa,OACf,MAAK,WAAW;IAGlB,MAAM,aAAa,MAAM,UAAU;KAAE,IAAI;KAAS;KAAM,CAAC;AAEzD,UAAM,IAAI;KACR,IAAI;KACJ,MAAM;KACN,QAAQ,eAAe,cAAc,KAAK;KAC3C,CAAC;;AAIN,UAAO,KAAK,UAAU,MAAM,MAAM,CAAC,OAAO,gBAAgB,QAAQ,aAAa;;EAElF"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"withHydration-692fMySA.mjs","names":[],"sources":["../src/lib/withHydration.ts"],"sourcesContent":["import type React from 'react'\n\n/**\n * Metadata attached to components marked for hydration.\n * Used by the build-time scanner and runtime hydration system.\n */\nexport interface HydrationMeta {\n strategy: 'load' | 'visible' | 'idle'\n __isHydratable: true\n}\n\n/**\n * Hydration strategy for the withHydration wrapper.\n * Does not include 'none' since wrapping implies hydration is desired.\n */\nexport type WithHydrationStrategy = 'load' | 'visible' | 'idle'\n\n/**\n * Marks a component's render function for client-side hydration.\n *\n * This wrapper attaches metadata to the component function, enabling:\n * 1. Build-time discovery via static analysis of source files\n * 2. Runtime hydration strategy determination\n *\n * The wrapper does NOT create a new component or add runtime overhead.\n * It simply attaches a `__hydration` property to the existing function.\n *\n * @param Component - The React component to mark for hydration\n * @param strategy - When to hydrate: 'load', 'visible', or 'idle'\n * @returns The same component with hydration metadata attached\n *\n * @example\n * ```tsx\n * // In a Puck component file (e.g., TestimonialGrid.tsx)\n * import { withHydration } from '@conloca/astro-cms'\n *\n * const TestimonialGridComponent = ({ title, testimonials }) => {\n * const [currentPage, setCurrentPage] = useState(0)\n * // ... interactive logic\n * }\n *\n * export const TestimonialGrid: ComponentConfig<Props> = {\n * fields: { ... },\n * defaultProps: { ... },\n * render: withHydration(TestimonialGridComponent, 'visible'),\n * }\n * ```\n */\nexport function withHydration<T extends (props: any) => React.ReactNode>(\n Component: T,\n strategy: WithHydrationStrategy\n): T {\n // Environment variable escape hatch for debugging\n if (typeof process !== 'undefined' && process.env?.CONLOCA_DISABLE_HYDRATION === 'true') {\n return Component\n }\n\n // Attach metadata directly to the function object\n // This avoids creating a wrapper component and keeps bundle size minimal\n const HydratableComponent = Component as T & { __hydration: HydrationMeta }\n HydratableComponent.__hydration = {\n strategy,\n __isHydratable: true,\n }\n\n return HydratableComponent\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgDA,SAAgB,cACd,WACA,UACG;AAEH,KAAI,OAAO,YAAY,eAAe,QAAQ,KAAK,8BAA8B,OAC/E,QAAO;CAKT,MAAM,sBAAsB;AAC5B,qBAAoB,cAAc;EAChC;EACA,gBAAgB;EACjB;AAED,QAAO"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"withHydration-BGgQDE-H.d.mts","names":[],"sources":["../src/lib/withHydration.ts"],"mappings":";;;;;AAMA;;UAAiB,aAAA;EACf,QAAA;EACA,cAAA;AAAA;;;;;KAOU,qBAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAiCI,aAAA,YAAyB,KAAA,UAAe,KAAA,CAAM,SAAA,CAAA,CAC5D,SAAA,EAAW,CAAA,EACX,QAAA,EAAU,qBAAA,GACT,CAAA"}
|