Live Content With D1 and R2

Live Content With D1 and R2

How AstroWEBTheme uses Cloudflare D1 for live content and Cloudflare R2 for media while keeping Astro layouts, feeds, and LLMS output.

AstroWEBTheme can serve content from static MDX files, Cloudflare D1 rows, or a hybrid of both. This helps client sites start simple and grow into editor-friendly workflows without throwing away the Astro front end.

The Live Content Tables

The live content workflow uses these D1 tables:

  • content_entries stores posts, pages, metadata, status, and body content.
  • media_assets stores R2 object metadata, alt text, captions, visibility, and object keys.
  • content_media_relations connects content rows to media assets.

The important content fields are:

  • collection
  • slug
  • title
  • description
  • body_mdx
  • body_html
  • status
  • published_at
  • last_modified
  • hero_image_id
  • tags_json
  • categories_json
  • subjects_json
  • sitemap
  • rss
  • llms

Public Content Flags

For a published demo post, use:

status = 'published'
indexable = 1
searchable = 1
sitemap = 1
rss = 1
llms = 'full'

Use llms = 'full' for content that should be useful to AI agents and answer engines.

Connecting R2 Images

R2 files are represented in D1 with a media_assets row. A content row can point to that asset with hero_image_id.

The renderer resolves:

content_entries.hero_image_id -> media_assets.id -> media_assets.r2_key

Then the public image URL is built from the media configuration.

Static, D1, and Hybrid Collections

Content source settings live in src/site/config/content.mjs.

Typical modes:

  • file: use only static MDX.
  • d1: use only D1 rows.
  • hybrid: merge static MDX and D1 rows.

The blog collection currently demonstrates a hybrid mode, which allows normal MDX posts and live D1 posts to appear in the same collection.

Feeds and Agent Outputs

Live content should appear in:

  • collection pages
  • tag/category/subject pages
  • /rss.xml
  • /<collection>/rss.xml
  • /llms.txt
  • /<collection>/llms.txt
  • sitemap output when enabled

This makes live D1 content visible to visitors, search systems, and AI agents.

Local and Remote Workflow

Use migrations for repeatable demo content.

Local D1:

pnpm exec wrangler d1 execute astrowebtheme-d1 --local --file .\migrations\013_live_content_demo_pack.sql

Remote D1:

pnpm exec wrangler d1 execute astrowebtheme-d1 --remote --file .\migrations\013_live_content_demo_pack.sql

Only run remote writes after reviewing the migration.

Security Notes

D1 content should be sanitized before rendering. Production deployments should also use:

  • response-header Content-Security-Policy
  • nonce-aware scripts
  • authenticated dashboard routes
  • no-store admin responses
  • reviewed media permissions
  • Turnstile or equivalent protection for public write forms

The result is a live publishing flow that still fits the theme security model.