Skip to content

feat: serve content at a commit chosen at runtime with live - #3936

Draft
larbish wants to merge 1 commit into
feat/content-worker-searchfrom
feat/live-content
Draft

larbish wants to merge 1 commit into
feat/content-worker-searchfrom
feat/live-content

Conversation

@larbish

@larbish larbish commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

❓ Type of change

  • ✨ New feature (a non-breaking change that adds functionality)

📚 Description

Stacked on #3931, which adds the search worker and its basePath option. The base branch is feat/content-worker-search, so this diff only shows the live mode.

A site on Nuxt Content v4 serves what its build snapshot holds until the next deploy. comark-docs and nuxt.com serve their content from GitHub at a commit chosen while the site runs, so a push goes live through a webhook with no build. Each of them carries its own code for that. This PR adds it to the module as a live option.

  • live: { ref, maxAge } on createNuxtContent(): ref is a function returning the commit to serve. With several sources it is one function, whose commit pins every source, or one function per source name, so sources can live in different repositories.
    • Every request to /__nuxt_content is served at those commits.
    • The commits are stored in the cache.driver the app already passes, in one entry, so every server serves the same ones. Without a driver they stay in the server's memory.
    • maxAge is how many seconds the stored commits are served before the functions are called again. The request that finds them older waits for the functions, and requests arriving meanwhile share the call. Without maxAge, the functions are called on every request.
    • A source whose function fails keeps its stored commit, or the commit of its build snapshot, while the other sources move.
  • Import paths, from feat!: import createNuxtContent from @nuxt/content/server #3935: server/content.ts imports createNuxtContent() and lastCommit() from @nuxt/content/server, and the v3 server composables move to @nuxt/content/v3/server. @nuxt/content/content is removed. Once feat!: import createNuxtContent from @nuxt/content/server #3935 is merged into v4 and v4 into feat: search content in a web worker with app/content.worker.ts #3931, these changes leave this diff.
  • lastCommit(options): a live.ref function for a forge() source. It takes the options of forge() and returns the last commit that touched path on ref, so a push that changes no content file doesn't move the site.
  • resolveContent(content): auto-imported in server code. Returns the content pinned to the commits being served, for server routes such as RSS or a sitemap. A content that isn't live is returned as it is, and so is every content in dev.
  • revalidateContent(content): auto-imported in server code, for a webhook. It calls every function, stores the commits and returns what changed: { from, to, changed, removed, navigation } for one source, and one such result per source whose commit moved for sources. It has no purge and no host code. If a function fails, the sources that answered are stored and served, then it throws naming the others.
  • Build snapshot: the build reads each source at the commit its function returns and stamps the snapshot with it. A cold start at that commit reads nothing from the source. At a later commit, only the files that changed are read. If a function or a source fails at build, the build warns and ships no snapshot for that source.
  • No static snapshot: a live content never publishes /__nuxt_content/manifest.json or the snapshots, even with app/content.worker.ts. The app tells the worker where to load a commit's snapshot from with basePath (feat: search content in a web worker with app/content.worker.ts #3931).
  • Setup errors: live fails the setup in hybrid mode, with app/content.ts, with a source that can't be pinned to a commit (fs(), unstorage()), and when a source has no function.
  • Types and components: the module calls no function and reads no source outside the snapshot build. Types come from the schemas the sources declare, and every project component stays in the component map.
  • Memory: each source keeps the content of its current and previous commit. Older ones are released, and a source whose commit didn't move is not rebuilt.
  • Dev: live is not used. The dev server reads the sources as they are, and revalidateContent() throws, since there is no commit to move.

lastCommit() and the comparison of two commits are in src/runtime/server/commits.ts, which has no Nuxt import, so they can move to comark-content later.

Against nuxt/content-v4-temp#42

This PR replaces that draft. Each of its behaviours:

#42 behaviour Status Here
live.ref returns a commit, requests served at it KEPT
live.maxAge KEPT Seconds the stored commits are served
A live content needs server-only KEPT
Build snapshot stamped with the commit resolved on build KEPT
Last commit, or the snapshot's commit, served when live.ref fails KEPT Per source
Current and previous instance kept, older released KEPT Per source
Types from the declared schema, no network for nuxi prepare KEPT
resolveContent() KEPT
Extra surfaces exported from server/content.ts, each with a basePath DELETED Only content
handleContent() DELETED Internal only
live.snapshot: false DELETED
Commit cached in Nitro's cache storage UPDATED Stored in the app's cache.driver
revalidateContent() deletes the cached commit UPDATED Calls the functions, stores the commits, returns what changed per source
Stale commit served while live.ref runs in the background UPDATED The request waits
Any single source accepted UPDATED Only sources that can be pinned to a commit
live rejected on several sources UPDATED Accepted, with one function or one per source
Each server keeps its own cached commit unless the app mounts a Nitro driver FIXED Shared through the driver the app already passes
revalidateContent() does nothing without maxAge FIXED Always calls the functions
Background refresh may finish on a later invocation on serverless FIXED No background work

New and not in #42: lastCommit(), the revalidateContent() result, one commit per source, and no static snapshot for a live content.

Evidence

What a site writes:

// server/content.ts
import { createNuxtContent, lastCommit } from '@nuxt/content/server'
import forge from 'comark-content/sources/forge'

const docs = { provider, repo: 'acme/docs', ref: 'main', path: 'content' }

export const content = createNuxtContent({
  mode: 'server-only',
  source: forge(docs),
  cache: { driver },
  live: { ref: lastCommit(docs), maxAge: 3600 },
})
// server/api/webhook.post.ts, after checking the signature
const { from, to, changed, removed, navigation } = await revalidateContent(content)
// { from: 'd1', to: 'd3', changed: ['/docs', '/docs/new'], removed: ['/docs/old'], navigation: true }
// server/routes/rss.xml.get.ts
const docs = await resolveContent(content)
const pages = await docs.list()

Several sources, in one repository or several:

live: { ref: lastCommit(repo) }
live: { ref: { site: lastCommit(site), docs: lastCommit(docs) } }

The tests build one fixture, test/fixtures/live, on forges/fake repositories that have different files at each commit. Each test starts the build's Vercel function alone, without the static files, with its own forge. A fixture route moves a branch and calls revalidateContent() as a webhook does, and another reports what was asked of the forge. Each test was seen failing: before its code was written, or with that code broken afterwards.

 Test Files  4 passed (4)
      Tests  28 passed (28)
  • test/live.test.ts, two sources in two repositories, a file-backed cache driver and maxAge:
    • each source is served at its function's commit, and a push moves only its source: the other repository is asked for its commit and nothing else;
    • revalidateContent() returns { docs: { from: 'd1', to: 'd3', changed: ['/docs', '/docs/new'], removed: ['/docs/old'], navigation: true } }, then navigation: false for a commit that changes one paragraph, {} for a push that changes no file under docs/, and every document with from: null when nothing was stored;
    • the stored commits are served for maxAge with no function called, then the next request finds the push;
    • a failing function keeps its source at the stored commit on a request, and makes revalidateContent() throw after the other source moved;
    • a second server sharing the cache directory serves a commit the first one revalidated, without calling a function;
    • a cold start with the forge down serves the build snapshot's commits and reads no file, and at a newer commit it reads the two changed files only;
    • three requests arriving together call the function once, and a call that started before a push doesn't bring its older commit back;
    • the content of the commit before the previous one is disposed, and the untouched source keeps its content;
    • a page pushed after the build renders a component no document used at build;
    • the types have the declared badge field, and the module asked nothing of the forge;
    • .vercel/output/static has no __nuxt_content.
  • test/live-forms.test.ts, three more builds of the fixture:
    • one source without driver nor maxAge: revalidateContent() returns one result, and the function is called on every request;
    • one function for two sources: both move to its commit, and the build stores no commit in the cache driver it shares with the servers;
    • a build while one function fails: that source ships no snapshot and is read from the forge on a cold start, the other source has its snapshot.
  • test/live-setup.test.ts: the four setup errors.
  • test/live-dev.test.ts: in dev, no function is called, resolveContent() returns the content unpinned, and revalidateContent() throws.

Lint, prepack, typecheck and the full suite (35 files, 150 tests) pass locally, with the CI command pnpm exec vitest run --maxWorkers 1 --no-file-parallelism.

Notes

  • One timestamp for all sources. The stored entry holds every source's commit and the time the functions were called. A function that failed is called again when maxAge runs out or by revalidateContent(), not on every request, so an outage of the forge doesn't slow every page.
  • A source with no commit at all. If its function fails, nothing is stored and the build shipped no snapshot for it, the request fails with a 500 naming the source.
  • Imports. resolveContent() and revalidateContent() are auto-imported in server code. lastCommit() is imported from @nuxt/content/server, like createNuxtContent(): the module also loads server/content.ts at build, outside Nitro, where nothing is auto-imported.
  • Typing. createNuxtContent() gets two type-only overloads: it returns an instance for source and a hub for sources, where its type was their union. Nothing changes at runtime. Without them, const { changed } = await revalidateContent(content) and (await resolveContent(content)).ref don't type-check (TS2322 and TS2339 with vue-tsc).
  • changed and removed list documents. A media file has no source hash to compare.
  • v3 server composables (queryCollection() in a server route) still read the content unpinned. A live site reads through /__nuxt_content or resolveContent().
  • Requests in flight. A request still running after two later commits of its source were served may fail, because its content is released.
  • Not tested here: real GitHub (lastCommit() runs against a stub of commits.list(), since the fake forge of forges 0.3.2 only filters commits by exact file path), a real shared cache such as Vercel Runtime Cache, a build where the function answers but the source can't be read, and media files of a live content.
  • Docs for the live mode come in a separate PR.

ELI5

Until now a site had to be rebuilt for a text change to show up. With this, the site asks "which version of the content should I show?" while it runs, remembers the answer for a while, and shows that version to everyone. When someone pushes a change, the site is told, moves to the new version in seconds, and says which pages changed so only those are refreshed.

🤖 Generated with Amp, reviewed by @larbish before opening.

@vercel

vercel Bot commented Nov 27, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
content Error Error Nov 27, 2026 10:00pm UTC

Request Review

This branch had an error being deployed

1 failed deployment
Preview — a0978a96 Deployed Nov 27, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant