Why the Photo Tree Is No Content Collection Anymore
When my old Astro site went bilingual, the photo tree left the content layer: every photo and node needs both languages in one file.
Reviewed by adrian
on
In March 2026 I built the photo tree of my old site as a content collection: a recursive collections_photos, one index.md per node, loaded through the content layer of Astro. Two hours later the same day a flag for sets followed. By July none of that code was running. The tree loaded through import.meta.glob and a frontmatter parser I wrote myself, not through defineCollection(). The reason was neither speed nor taste: the site had gone bilingual, and a content collection cannot hold what a bilingual photo needs.
What broke
The content collections of the site had one entry per language. A post's id was <locale>/<slug>, so the German and English versions of an article were two files and two entries, joined afterwards by a shared translationKey. For articles that is fine. A whole post per language is normal, and the two versions may differ a little in wording without anyone noticing.
A photo is different. It needs a German and an English title at the same time, in the same request, because the photo page renders whichever fits the visitor's language. Switching the language on that photo has to land on the same URL slug. Two entries with the same photo ID and different locale prefixes do not model that. They model two photos that happen to share a filename.
The index.md nodes had the same problem one level up. travels/asia/chiang-mai/index.md needs both titles in one file, not two files that both claim the slug chiang-mai.
What replaced it
src/lib/photos.ts reads the tree directly with Vite's import.meta.glob, eagerly, at build time:
const imageModules = import.meta.glob<{ default: ImageMetadata }>(
'../content/photos/**/img/*.jpg',
{ eager: true },
);
const jsonModules = import.meta.glob<PhotoMeta>(
'../content/photos/**/img/*.json',
{ eager: true, import: 'default' },
);
const indexMarkdownModules = import.meta.glob<string>(
'../content/photos/**/index.md',
{ eager: true, query: '?raw' },
);
A small parseFrontmatter() in the same file reads the frontmatter of the index.md files, without Astro's Zod validation. Every field that needs a translation is a record keyed by locale, down to the photo sidecar:
export interface PhotoMeta {
id: string;
title: Record<Locale, string[]>;
alt: Record<Locale, string>;
tags: Record<Locale, string[]>;
location: string;
date: string;
exif: { camera?: string; lens?: string; /* … */ };
}
export interface PhotoNode {
slug: string;
titles: Partial<Record<Locale, string>>;
descriptions?: Partial<Record<Locale, string>>;
category?: Partial<Record<Locale, string>>;
tags?: Partial<Record<Locale, string[]>>;
isSet: boolean;
childSlugs: string[];
// …
}
A sidecar like 2025-03-09-112512.json now carries title.de and title.en in one file, next to the JPG it describes. No duplicate, no second entry to keep in sync, no lookup by translation key at render time.
What survived
The set: true flag is still in the same place in the frontmatter and does the same job. It tells the archive page which nodes are finished photo sets and which are navigation nodes like travels/asia/. Only the loading underneath had to change.
The idea that the folder is the route survived as well, now split across two route files. src/pages/photos/[...slug].astro serves English and src/pages/de/fotos/[...slug].astro German. Both read the same PhotoNode and Photo tree and render the field for their language. The one route from March became two routes over the same data.
Takeaways
- A content collection models one entry per language well. It does not model one thing that has to carry every language in a single file, and photos with bilingual metadata are that kind of thing.
- When translations come to an existing feature, check whether its data model assumed one language per record. If it did, the fix is a new shape for the record, rarely a config change.
- Before a rewrite, sort out which parts of a design are about loading data. The rest, like the
setflag or folders as routes, can stay.
Based on
-
Why the Photo Tree Isn't a Content Collection Anymore
(
raw/2026/2026-07-18/why-the-photo-tree-isn-t-a-content-collection-anymore.md) [post]
Topic
- Static Website A website made of finished HTML files that are built once and then served unchanged. No server code, no database.
Mentioned in
- Adding Notes, Links and Archives Three sections of my old Astro site: short notes rendered inline, a link log as a data collection, and one archive across every content type.
- Building the Photo Stream The photo stream of my old Astro site: JSON sidecars next to each JPG, import.meta.glob, a justified grid and batches revealed on scroll.
- Collection Sets and Category Collections One boolean in the frontmatter told the photo sets of my old site apart from pure navigation nodes, so only finished sets reached the archive.
- Replacing justified-layout with My Own Package How my old site swapped Flickr's unmaintained justified-layout for computeLayout() from my own package, without touching the photo tree.
- Structuring Photos as a Collection Tree How the photos of my old Astro site grew from flat albums into a recursive tree: one catch-all route, photos per node, breadcrumbs and tag pages.
Mentioned
- Astro Tool for building websites from content such as Markdown. Ships plain HTML by default and only loads JavaScript where it is needed.