@crombolo/render 0.23.0 → 0.25.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/package.json +2 -2
- package/src/published-cache.ts +52 -15
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crombolo/render",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.25.0",
|
|
4
4
|
"description": "TSX server rendering and the published-content model for crombolo.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Crombolo",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"access": "public"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@crombolo/core": "0.
|
|
35
|
+
"@crombolo/core": "0.25.0",
|
|
36
36
|
"@types/react": "^19.3.0"
|
|
37
37
|
}
|
|
38
38
|
}
|
package/src/published-cache.ts
CHANGED
|
@@ -88,6 +88,14 @@ export interface PublishedContentSource {
|
|
|
88
88
|
loadContentTypeAliases?(): Promise<Map<string, string>>
|
|
89
89
|
/** How URLs are built; Umbraco's defaults when omitted. */
|
|
90
90
|
loadUrlOptions?(): Promise<PublishedUrlOptions>
|
|
91
|
+
/**
|
|
92
|
+
* Runs a build's reads as one consistent state (story 174): `fn` is handed a
|
|
93
|
+
* source bound to a single read snapshot, and the content checkpoint that
|
|
94
|
+
* snapshot contains. Without it each load reads whatever is current.
|
|
95
|
+
*/
|
|
96
|
+
withSnapshot?<T>(
|
|
97
|
+
fn: (source: PublishedContentSource, checkpoint: number) => Promise<T>,
|
|
98
|
+
): Promise<T>
|
|
91
99
|
}
|
|
92
100
|
|
|
93
101
|
/** `HideTopLevelNodeFromPath`: true, Umbraco's default, leaves the top-level page out of paths. */
|
|
@@ -196,6 +204,14 @@ export interface CacheSnapshot {
|
|
|
196
204
|
dictionaryOutline: DictionaryOutlineItem[]
|
|
197
205
|
/** Content type key (lowercase) → alias. */
|
|
198
206
|
contentTypeAliases: Map<string, string>
|
|
207
|
+
/**
|
|
208
|
+
* The content checkpoint whose changes this snapshot holds (story 174): every
|
|
209
|
+
* change numbered at or below it, and none above. Absent from a source that
|
|
210
|
+
* cannot say.
|
|
211
|
+
*/
|
|
212
|
+
checkpoint?: number
|
|
213
|
+
/** When it was built. */
|
|
214
|
+
builtAt?: Date
|
|
199
215
|
}
|
|
200
216
|
|
|
201
217
|
/** What a request resolved to: the content, and the culture its hostname assigns. */
|
|
@@ -344,8 +360,13 @@ export class PublishedCache {
|
|
|
344
360
|
const generation = this.#generation
|
|
345
361
|
if (this.#building?.generation === generation) return this.#building.done
|
|
346
362
|
const done = this.#build().then((snapshot) => {
|
|
347
|
-
// A slower, older build must not replace a newer one that finished first
|
|
348
|
-
|
|
363
|
+
// A slower, older build must not replace a newer one that finished first,
|
|
364
|
+
// nor a snapshot of older data one of newer data.
|
|
365
|
+
const older =
|
|
366
|
+
snapshot.checkpoint !== undefined &&
|
|
367
|
+
this.#snapshot.checkpoint !== undefined &&
|
|
368
|
+
snapshot.checkpoint < this.#snapshot.checkpoint
|
|
369
|
+
if (generation >= this.#snapshotGeneration && !older) {
|
|
349
370
|
this.#snapshot = snapshot
|
|
350
371
|
this.#snapshotGeneration = generation
|
|
351
372
|
}
|
|
@@ -643,27 +664,41 @@ export class PublishedCache {
|
|
|
643
664
|
}
|
|
644
665
|
}
|
|
645
666
|
|
|
667
|
+
/** The checkpoint and build time of the snapshot served now; nothing before the first build. */
|
|
668
|
+
served(): { checkpoint: number | undefined; builtAt: Date } | undefined {
|
|
669
|
+
if (this.#snapshot === EMPTY || !this.#snapshot.builtAt) return undefined
|
|
670
|
+
return { checkpoint: this.#snapshot.checkpoint, builtAt: this.#snapshot.builtAt }
|
|
671
|
+
}
|
|
672
|
+
|
|
646
673
|
async #build(): Promise<CacheSnapshot> {
|
|
647
|
-
const
|
|
648
|
-
|
|
649
|
-
|
|
674
|
+
const source = this.#source
|
|
675
|
+
if (source.withSnapshot)
|
|
676
|
+
return source.withSnapshot((scoped, checkpoint) => this.#buildFrom(scoped, checkpoint))
|
|
677
|
+
return this.#buildFrom(source, undefined)
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
async #buildFrom(
|
|
681
|
+
source: PublishedContentSource,
|
|
682
|
+
checkpoint: number | undefined,
|
|
683
|
+
): Promise<CacheSnapshot> {
|
|
684
|
+
const builtAt = new Date()
|
|
685
|
+
const nodes = await source.loadPublished()
|
|
686
|
+
const domains = (await source.loadDomains?.()) ?? []
|
|
687
|
+
const languages = (await source.loadLanguages?.()) ?? []
|
|
650
688
|
const dictionary = new Map<string, Map<string, string>>()
|
|
651
|
-
const dictionaryOutline = (await
|
|
652
|
-
for (const [key, translations] of (await
|
|
689
|
+
const dictionaryOutline = (await source.loadDictionaryOutline?.()) ?? []
|
|
690
|
+
for (const [key, translations] of (await source.loadDictionary?.()) ?? new Map())
|
|
653
691
|
dictionary.set(
|
|
654
692
|
key.toLowerCase(),
|
|
655
693
|
new Map([...translations].map(([culture, text]) => [culture.toLowerCase(), text])),
|
|
656
694
|
)
|
|
657
695
|
const media = new Map(
|
|
658
|
-
((await
|
|
659
|
-
m.key.toLowerCase(),
|
|
660
|
-
new PublishedMedia(m),
|
|
661
|
-
]),
|
|
696
|
+
((await source.loadMedia?.()) ?? []).map((m) => [m.key.toLowerCase(), new PublishedMedia(m)]),
|
|
662
697
|
)
|
|
663
|
-
const redirects = (await
|
|
664
|
-
const elementNodes = (await
|
|
698
|
+
const redirects = (await source.loadRedirects?.()) ?? []
|
|
699
|
+
const elementNodes = (await source.loadElements?.()) ?? []
|
|
665
700
|
const contentTypeAliases = new Map(
|
|
666
|
-
[...((await
|
|
701
|
+
[...((await source.loadContentTypeAliases?.()) ?? new Map())].map(([key, alias]) => [
|
|
667
702
|
key.toLowerCase(),
|
|
668
703
|
alias,
|
|
669
704
|
]),
|
|
@@ -692,7 +727,7 @@ export class PublishedCache {
|
|
|
692
727
|
const firstRootId = nodes
|
|
693
728
|
.filter((node) => node.parentId <= 0)
|
|
694
729
|
.sort((a, b) => a.sortOrder - b.sortOrder)[0]?.id
|
|
695
|
-
const { hideTopLevel } = (await
|
|
730
|
+
const { hideTopLevel } = (await source.loadUrlOptions?.()) ?? { hideTopLevel: true }
|
|
696
731
|
|
|
697
732
|
const byRoute = new Map<string, PublishedContent>()
|
|
698
733
|
const byDomainRoute = new Map<string, PublishedContent>()
|
|
@@ -946,6 +981,8 @@ export class PublishedCache {
|
|
|
946
981
|
dictionary,
|
|
947
982
|
dictionaryOutline,
|
|
948
983
|
contentTypeAliases,
|
|
984
|
+
...(checkpoint === undefined ? {} : { checkpoint }),
|
|
985
|
+
builtAt,
|
|
949
986
|
}
|
|
950
987
|
}
|
|
951
988
|
|