@uniweb/build 0.14.25 → 0.14.26

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uniweb/build",
3
- "version": "0.14.25",
3
+ "version": "0.14.26",
4
4
  "description": "Build tooling for the Uniweb Component Web Platform",
5
5
  "type": "module",
6
6
  "exports": {
@@ -64,7 +64,7 @@
64
64
  },
65
65
  "optionalDependencies": {
66
66
  "@uniweb/content-reader": "1.1.12",
67
- "@uniweb/runtime": "0.8.23",
67
+ "@uniweb/runtime": "0.8.24",
68
68
  "@uniweb/schemas": "0.2.4"
69
69
  },
70
70
  "peerDependencies": {
@@ -74,7 +74,7 @@
74
74
  "@tailwindcss/vite": "^4.0.0",
75
75
  "@vitejs/plugin-react": "^4.0.0 || ^5.0.0",
76
76
  "vite-plugin-svgr": "^4.0.0",
77
- "@uniweb/core": "0.7.17"
77
+ "@uniweb/core": "0.7.18"
78
78
  },
79
79
  "peerDependenciesMeta": {
80
80
  "vite": {
package/src/prerender.js CHANGED
@@ -96,7 +96,7 @@ async function executeAllFetches(siteContent, siteDir, onProgress, localeInfo) {
96
96
  onProgress(` Fetching site data: ${cfg.path || cfg.url}`)
97
97
  const result = await executeFetch(cfg, opts)
98
98
  if (result.data && !result.error) {
99
- fetchedData.push({ config: cfg, data: result.data })
99
+ fetchedData.push({ config: cfg, data: result.data, _scope: '__site__' })
100
100
  }
101
101
  }
102
102
 
@@ -112,7 +112,7 @@ async function executeAllFetches(siteContent, siteDir, onProgress, localeInfo) {
112
112
  onProgress(` Fetching page data for ${page.route}: ${cfg.path || cfg.url}`)
113
113
  const result = await executeFetch(cfg, opts)
114
114
  if (result.data && !result.error) {
115
- fetchedData.push({ config: cfg, data: result.data })
115
+ fetchedData.push({ config: cfg, data: result.data, _scope: page.route })
116
116
  // Store for dynamic route expansion
117
117
  pageFetchedData.set(page.route, {
118
118
  schema: pageFetch.schema,
@@ -212,13 +212,19 @@ export function expandDynamicPages(pages, pageFetchedData, onProgress) {
212
212
  concretePage.paramName = undefined
213
213
  concretePage.parentSchema = undefined
214
214
 
215
- // Store the dynamic route context for runtime data resolution
215
+ // Store the dynamic route context for runtime data resolution. Only the
216
+ // keys the runtime actually uses — the entity cascade re-finds the record
217
+ // from the fetched collection by paramName/paramValue/schema. The record
218
+ // (`currentItem`) and the full sibling list (`allItems`) are deliberately
219
+ // NOT baked in: nothing reads them (the documented dynamicContext shape is
220
+ // { paramName, paramValue, schema }; the record is delivered via
221
+ // content.data and siblings via `fetch: { refine: true, detail: false }`),
222
+ // and embedding `allItems` duplicated the whole collection onto every
223
+ // prerendered page in split mode.
216
224
  concretePage.dynamicContext = {
217
225
  paramName,
218
226
  paramValue,
219
227
  schema, // Plural: 'articles'
220
- currentItem: item, // The item for this specific route
221
- allItems: items, // All items from parent
222
228
  }
223
229
 
224
230
  // Use item data for page metadata if available
@@ -323,6 +329,42 @@ async function discoverLocaleContents(distDir, defaultContent) {
323
329
  return locales
324
330
  }
325
331
 
332
+ /**
333
+ * Strip the internal `_scope` tag off a fetchedData entry, leaving the clean
334
+ * `{ config, data }` shape the runtime's hydrateDataStore expects.
335
+ */
336
+ function stripFetchScope(entry) {
337
+ const { _scope, ...clean } = entry
338
+ return clean
339
+ }
340
+
341
+ /**
342
+ * Scope `fetchedData` for a single page's inline __SITE_CONTENT__.
343
+ *
344
+ * Every fetchedData entry is tagged (in executeAllFetches) with `_scope`:
345
+ * '__site__' for a site-level fetch, or the owning page's route for a
346
+ * page-level fetch. A page's first render only ever reads the cascade
347
+ * block → page → page.parent → site (see @uniweb/core EntityStore), so in
348
+ * split-content mode we embed just the entries that cascade can reach —
349
+ * site-level plus the page's own route and its parent's route (`scopeRoutes`).
350
+ * Other pages' data, and dynamic detail data reached only by client-side
351
+ * navigation, is fetched on demand and must not ride in every page's payload.
352
+ *
353
+ * `scopeRoutes == null` means "don't scope" (non-split mode, or the SPA
354
+ * fallback): keep every entry. Either way the internal `_scope` tag is stripped.
355
+ *
356
+ * @param {Array<{config: Object, data: any, _scope?: string}>} fetchedData
357
+ * @param {Set<string>|null} scopeRoutes - Routes whose entries to keep, or null.
358
+ * @returns {Array<{config: Object, data: any}>}
359
+ */
360
+ export function scopeFetchedData(fetchedData, scopeRoutes) {
361
+ if (!Array.isArray(fetchedData)) return fetchedData
362
+ if (!scopeRoutes) return fetchedData.map(stripFetchScope)
363
+ return fetchedData
364
+ .filter((e) => e._scope === '__site__' || scopeRoutes.has(e._scope))
365
+ .map(stripFetchScope)
366
+ }
367
+
326
368
  /**
327
369
  * Inject build-specific data into HTML (theme CSS, __SITE_CONTENT__, icon cache).
328
370
  * Called after the shared injectPageContent for build-specific additions.
@@ -334,7 +376,7 @@ async function discoverLocaleContents(distDir, defaultContent) {
334
376
  * @param {string|null} [options.currentRoute=null] - Route of the page this HTML is for
335
377
  * @returns {string} HTML with build-specific data injected
336
378
  */
337
- function injectBuildData(html, siteContent, { splitContent = false, currentRoute = null } = {}) {
379
+ function injectBuildData(html, siteContent, { splitContent = false, currentRoute = null, scopeRoutes = null } = {}) {
338
380
  let result = html
339
381
 
340
382
  // Inject theme CSS if not already present
@@ -367,6 +409,16 @@ function injectBuildData(html, siteContent, { splitContent = false, currentRoute
367
409
  }
368
410
  }
369
411
 
412
+ // Scope fetched (collection / API) data to this page's cascade in split mode,
413
+ // so a page never carries other pages' collections. Non-split keeps it all
414
+ // (single-file inline). Either way the internal `_scope` tag is stripped.
415
+ if (Array.isArray(contentForJson.fetchedData)) {
416
+ contentForJson = {
417
+ ...contentForJson,
418
+ fetchedData: scopeFetchedData(contentForJson.fetchedData, splitContent ? scopeRoutes : null),
419
+ }
420
+ }
421
+
370
422
  const contentScript = `<script id="__SITE_CONTENT__" type="application/json">${JSON.stringify(contentForJson).replace(/</g, '\\u003c')}</script>`
371
423
  if (result.includes('__SITE_CONTENT__')) {
372
424
  // Replace existing site content with updated version (includes expanded dynamic routes)
@@ -667,10 +719,14 @@ export async function prerenderSite(siteDir, options = {}) {
667
719
  sectionOverrideCSS: result.sectionOverrideCSS,
668
720
  })
669
721
 
670
- // Build-specific: theme CSS, __SITE_CONTENT__, icon cache
722
+ // Build-specific: theme CSS, __SITE_CONTENT__, icon cache.
723
+ // scopeRoutes mirrors the runtime data cascade (page → page.parent → site)
724
+ // so split-mode pages embed only the collection data their first render reads.
725
+ const scopeRoutes = new Set([page.route, page.parent?.route].filter(Boolean))
671
726
  html = injectBuildData(html, siteContent, {
672
727
  splitContent,
673
728
  currentRoute: page.route,
729
+ scopeRoutes,
674
730
  })
675
731
 
676
732
  // Output to the locale-prefixed route
@@ -686,6 +742,7 @@ export async function prerenderSite(siteDir, options = {}) {
686
742
  const fallbackBaseHtml = injectBuildData(htmlShell, siteContent, {
687
743
  splitContent,
688
744
  currentRoute: null, // 404 has no current page — manifest only
745
+ scopeRoutes: new Set(), // SPA fallback carries site-level fetched data only
689
746
  })
690
747
  const { html: notFoundHtml, hasNotFoundPage } = generate404Html({
691
748
  baseHtml: fallbackBaseHtml,
@@ -710,6 +767,11 @@ export async function prerenderSite(siteDir, options = {}) {
710
767
  return metadata
711
768
  })
712
769
  }
770
+ // The manifest is a single (non-per-page) file, so it keeps all fetched
771
+ // data — but the internal `_scope` tag must never leak into it.
772
+ if (Array.isArray(manifest.fetchedData)) {
773
+ manifest.fetchedData = manifest.fetchedData.map(stripFetchScope)
774
+ }
713
775
  await writeFile(localeContentPath, JSON.stringify(manifest))
714
776
  onProgress('Rewrote site-content.json as lightweight manifest')
715
777
  }
@@ -74,8 +74,45 @@ function resolveFaviconHref(configFavicon, publicDir, basePath) {
74
74
  }
75
75
 
76
76
  /**
77
- * Execute all fetches for site content (used in dev mode)
78
- * Collects fetchedData for DataStore pre-population at runtime
77
+ * Should this fetch config be pre-executed at build time and embedded into the
78
+ * dev boot payload, or left for the runtime to fetch live?
79
+ *
80
+ * Dev has no prerender step, so there is no server-rendered HTML to hydrate and
81
+ * nothing to gain from embedding fetched data — while embedding it *costs*
82
+ * freshness: the boot payload is a one-time snapshot, so editing a local
83
+ * collection regenerates `/data/*.json` but the runtime keeps reading the stale
84
+ * embed until the dev server is restarted.
85
+ *
86
+ * So in dev we embed only what the browser genuinely cannot fetch itself:
87
+ * - Local `path:` sources (file-based collections → `/data/*.json`) are served
88
+ * by Vite and always browser-reachable, so we never embed them — the runtime
89
+ * fetches them live and picks up edits on reload. This also gives local
90
+ * collections true parity with a real backend fetcher.
91
+ * - `prerender: false` opts a source out of build-time fetching by definition.
92
+ * - Remote `url:` sources with `prerender !== false` may target a
93
+ * build-time-only endpoint (server-side auth, no CORS), so we keep
94
+ * pre-fetching and embedding them, mirroring what prod prerender does.
95
+ *
96
+ * Prod is unaffected: prerender still embeds fetched data for flash-free SSG
97
+ * hydration (see build/src/prerender.js executeAllFetches / injectBuildData).
98
+ *
99
+ * @param {Object|null} cfg - A normalized fetch config (parseFetchConfig output)
100
+ * @returns {boolean}
101
+ */
102
+ export function shouldPrefetchInDev(cfg) {
103
+ if (!cfg) return false
104
+ if (!cfg.path && !cfg.url) return false // refinement / nothing to fetch
105
+ if (cfg.prerender === false) return false // author opted into runtime fetch
106
+ if (cfg.path && !cfg.url) return false // local file — runtime fetches it live
107
+ return true // remote build-time fetch — keep embedding in dev
108
+ }
109
+
110
+ /**
111
+ * Execute the dev fetches that should be embedded (see shouldPrefetchInDev) and
112
+ * collect them as `fetchedData` for DataStore pre-population. Local file-based
113
+ * collections and `prerender: false` sources are intentionally skipped so the
114
+ * runtime fetches them live — keeping the dev payload free of dynamic data and
115
+ * ensuring edits to `/data/*.json` show up on reload without a dev restart.
79
116
  *
80
117
  * @param {Object} siteContent - The collected site content
81
118
  * @param {string} siteDir - Path to site directory
@@ -86,7 +123,7 @@ async function executeDevFetches(siteContent, siteDir) {
86
123
 
87
124
  // Site-level fetch
88
125
  const siteFetch = siteContent.config?.fetch
89
- if (siteFetch) {
126
+ if (shouldPrefetchInDev(siteFetch)) {
90
127
  const result = await executeFetch(siteFetch, fetchOptions)
91
128
  if (result.data && !result.error) {
92
129
  fetchedData.push({ config: siteFetch, data: result.data })
@@ -97,7 +134,7 @@ async function executeDevFetches(siteContent, siteDir) {
97
134
  for (const page of siteContent.pages || []) {
98
135
  // Page-level fetch
99
136
  const pageFetch = page.fetch
100
- if (pageFetch) {
137
+ if (shouldPrefetchInDev(pageFetch)) {
101
138
  const result = await executeFetch(pageFetch, fetchOptions)
102
139
  if (result.data && !result.error) {
103
140
  fetchedData.push({ config: pageFetch, data: result.data })
@@ -125,7 +162,7 @@ async function processDevSectionFetches(sections, fetchOptions) {
125
162
  for (const section of sections) {
126
163
  // Execute section-level fetch
127
164
  const sectionFetch = section.fetch
128
- if (sectionFetch) {
165
+ if (shouldPrefetchInDev(sectionFetch)) {
129
166
  const result = await executeFetch(sectionFetch, fetchOptions)
130
167
  if (result.data && !result.error) {
131
168
  // Merge fetched data into section's parsedContent (not cascadedData)