Skip to content

core: $hooks.page #57

Open

Description

@lukeed

Plugins can add helpers ($utils), partials, loaders, and a $hooks.transform that changes finished HTML. None of them can add data to a page before it renders. So anything that a site computes per page, such as its language, its translations, or its previous and next page, ends up as long expressions in every template, and often as a filter over $pages on every page, whose work grows with pages × pages.

This issue adds that step to core: $hooks.page. It needs a full core benchmark before and after (see the end). #58 builds @bongo/i18n on it.

The hook

// index.d.ts
export namespace Build {
	/**
	 * Return the params of one page, before any page renders. bongo calls it once for each page.
	 * @example const page: Build.PageHook = page => page.path.startsWith('/fr/') ? { lang: 'fr', ...page.params } : page.params;
	 */
	type PageHook = (page: Template.PageInfo, ctx: PageHook.Context) => Promisable<Template.Params>;

	namespace PageHook {
		type Context = {
			/** Every page, as the hooks before this one left them. */
			pages: Template.PageInfo[];
			/** The config, with its plugins merged. */
			config: Required<Config>;
		};
	}

	type Hooks = {
		transform?: Transform;
		page?: PageHook;
	};
}

When it runs

1. read content/     frontmatter and loaders → one entry per page
2. $hooks.page       ← new: once for each page
3. build $pages, $section, $tags   from the new params
4. render            pages, then output files, then data modules
5. $hooks.transform  on each finished page and output file

Rules

  • page is the page as $pages shows it: path, url, title, summary, section, tags, headings, and params (its frontmatter).
  • ctx.pages holds every page, so a hook can compare pages.
  • The hook returns the whole new params. It decides if frontmatter wins: { lang: 'fr', ...page.params } is a default, { ...page.params, lang: 'fr' } always wins.
  • bongo derives title, summary, and tags again from the new params, so a hook can set a title or add a tag, and $tags holds that tag.
  • path, url, and section cannot change: they decide the output file and folder, which are fixed by then. A hook that changes params.url fails the build and names the page.
  • Hooks run in turn, the plugins' in order, then the config's own. Each runs over every page, and sees in ctx.pages what the hooks before it returned.
  • The new params are the same everywhere: $page.params, the entries of $pages, $section, and $tags, output files, data modules, and ctx.params of $hooks.transform.
  • A hook that throws fails the build and names the page.
  • One call per page per build. Dev calls it on every rebuild, since a rebuild reads all frontmatter anyway. A hook that compares pages should index ctx.pages once per build, such as in a WeakMap keyed by the array.

Example: folder defaults

export default {
	$hooks: {
		page: page => page.path.startsWith('/fr/') ? { lang: 'fr', ...page.params } : page.params,
	},
};

Proposed additions (decide before building)

  1. page.file: the source path of the page in content/, such as blog/post.md. Edit-on-GitHub links and <lastmod> need it, and index.d.ts already has // TODO: file.abs path and // TODO: file.rel path. Recommended: yes.
  2. Return null to drop a page: no output file, and not in $pages, $section, or $tags. This gives drafts and scheduled posts: page => page.params.draft && !dev ? null : page.params. It changes which files a build writes, so it needs its own tests and part of the benchmark.

Uses

  • Translations: a plugin sets lang, key, and versions on each page once, in place of long expressions in each template. See @bongo/i18n, and config partials with page values #58.
  • Folder defaults: one hook sets defaults for every page in a folder, such as lang or group, in place of the same frontmatter in each page (see "Example: folder defaults" above).
  • Docs site sidebar and pager: $utils.nav and $utils.pager rebuild the page list on each page render (docs/layouts/single.hbs). The hook can set params.prev and params.next once.
  • Edit-on-GitHub links from the real source file (with page.file), in place of a guess from the URL.
  • <lastmod> in sitemaps: params.updated from git or the file time (with page.file).
  • Dates parsed once, and params.year for archives.
  • Related posts and series, from one index per build.
  • Drafts and scheduled posts (with null to drop a page).

Benchmark (required)

This changes the order of a build for every site, so it needs a full core benchmark before and after. The same benchmark serves #56.

  1. Add a benchmark that generates a large site: for example 10,000 Markdown pages in nested folders, with tags, layouts and partials that read $pages, output files, and data modules.
  2. On main before the change, record the time and the peak memory of: bongo build, the first build of bongo dev, and a narrow dev rebuild (one page edited).
  3. Record the same with the change, with no hook, so that the cost of the new step alone shows. @bongo/i18n, and config partials with page values #58 measures it again with @bongo/i18n.
  4. Put the numbers in the PR. With no hook, the time and memory must not get worse.

Work

  • Benchmark on main
  • $hooks.page in core, with tests
  • The proposed additions, if accepted, with tests
  • Docs: the Hooks reference, and a guide section on folder defaults
  • Benchmark after, numbers in the PR

Activity

  1. added
    enhancement
    on Oct 15, 2026
  2. changed the title [-]core: $hooks.page, config partials with page values, and @bongo/i18n[/-] [+]core: $hooks.page[/+] on Oct 15, 2026
  3. added theissue type on Oct 20, 2026
  4. removed
    enhancement
    on Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions