Last modified time
With BrainDB
- Install BrainDB
- Then you can access modification date, like this
const doc = (await getBrainDb().documents({ slug }))[0];doc.updatedAt();
With Starlight
Starlight already has this feature, but the value is not exposed in the content collection. For example, if you set:
export default defineConfig({ integrations: [ starlight({ lastUpdated: true, }), ],});
You would see Last updated:
on the page, but at the same time, page.data.lastUpdated
would be undefined
.
With Remark plugin
-
Create remark plugin
remark-modified-time.mjs import { execSync } from "child_process";export function remarkModifiedTime() {return function (tree, file) {const filepath = file.history[0];const result = execSync(`git log -1 --pretty="format:%cI" "${filepath}"`);file.data.astro.frontmatter.lastUpdated = result.toString();};} -
Configure Astro
astro.config.mjs import { remarkModifiedTime } from "./remark-modified-time.mjs";export default defineConfig({markdown: {remarkPlugins: [remarkModifiedTime],},}); -
You may need to adjust content schema
src/content/config.ts import { z, defineCollection } from "astro:content";const blog = defineCollection({schema: z.object({lastUpdated: z.string().transform((str) => new Date(str)),}),});
But the value is accessible only after render
:
const { remarkPluginFrontmatter } = await page.render();console.log(remarkPluginFrontmatter.lastUpdated);
Based on: Astro recipes β
Tips
Github actions
If you build your site with Github Actions, you need to use fetch-depth: 0
- uses: actions/checkout@v4 with: fetch-depth: 0
Further improvements
Use the last modification date (for content pages) in:
- schema.org microdata
- sitemap β (
lastmod
) - in the Sidebar to show βnewβ badge