Skip to content

Commit e8390da

Browse files
committed
document global.data.js caveats for vars getter and rendering
1 parent 5134ccc commit e8390da

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,38 @@ The returned object is stamped onto every page's vars before rendering, so any p
10531053

10541054
Use `GlobalDataFunction<T>` or `AsyncGlobalDataFunction<T>` to type the function where `T` is the shape of the object you return.
10551055

1056+
**Caveats:**
1057+
1058+
**`page.vars` is a computed getter.** Each access performs a fresh merge of all variable sources. Cache the result when reading multiple properties from the same page:
1059+
1060+
```js
1061+
// good: one merge per page
1062+
const vars = page.vars
1063+
const { title, date } = vars
1064+
1065+
// avoid: merges on every property access
1066+
const title = page.vars.title
1067+
const date = page.vars.date
1068+
```
1069+
1070+
For large sites with hundreds of pages, caching the result once per page reduces build time noticeably.
1071+
1072+
**`page.vars` can throw.** If a page vars module has a syntax error, a missing dependency, or a runtime error, accessing `.vars` will throw. Wrap accesses in `try/catch` when iterating all pages so one broken page does not abort the whole function:
1073+
1074+
```js
1075+
const posts = pages.filter(p => {
1076+
try {
1077+
return p.vars.layout === 'article' && p.vars.date
1078+
} catch {
1079+
return false
1080+
}
1081+
})
1082+
```
1083+
1084+
**`page.vars.content` is raw source, not rendered HTML.** For markdown pages, `vars.content` is the raw markdown string read from the file. To get rendered HTML, call `page.renderInnerPage({ pages })`.
1085+
1086+
**`renderInnerPage()` is available.** `global.data.js` receives fully initialized `PageData` instances, so you can call `renderInnerPage()` here. See [Accessing rendered page content](#accessing-rendered-page-content) for usage and performance guidance.
1087+
10561088
### `esbuild.settings.ts`
10571089

10581090
This is an optional file you can create anywhere.

0 commit comments

Comments
 (0)