Generate redirects and sitemap exclusions from front matter - #2032
Merged
Merged
Conversation
redirect_from, redirect_to, and sitemap: false were accepted by the content schema but did nothing; public/_redirects and the EXCLUDED_PAGES list were kept in sync by hand. - src/utils/front-matter-routes.ts reads posts/pages front matter at config load (gray-matter), maps entries to their URLs, and derives sitemap exclusions and _redirects rules. - A front-matter-redirects integration appends the generated rules to dist-astro/_redirects in astro:build:done and fails the build on a source duplicated between public/_redirects and front matter. - The 22 now-generated lines are removed from public/_redirects. The effective rule set in the built file is unchanged (63 rules). - redirect_to posts are dropped from the sitemap: their URLs 301 off-site. - CI validates the built dist-astro/_redirects; the redirects e2e spec reads the built file. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
redirect_from,redirect_to, andsitemap: falseare in the content schema, but nothing read them. Redirects lived in a hand-maintainedpublic/_redirects, and sitemap exclusions were a hardcoded list inastro.config.mjs. The two were kept in sync by hand. This PR makes front matter the source of truth.Approach
src/utils/front-matter-routes.ts(plus tests) readssrc/content/{posts,pages}front matter withgray-matter, which is already a devDependency. It maps each entry to its URL: posts go throughgetPostUrl, and pages usepermalinkor/id/. From that it derives:sitemap: false, plusredirect_toentries, since those URLs 301 off-site.redirect_from -> entry URL 301andentry URL -> redirect_to 301. Each directory-style source is emitted in both/foo/and/fooforms, matching the existing convention.redirect_fromshadows a real page.astro.config.mjs@astrojs/sitemapfilter. The non-content routes stay hardcoded: 404,_not-found, fine-print,resume/linkedin, andresume/print.front-matter-redirectsintegration that appends the generated rules todist-astro/_redirectsinastro:build:done. It fails the build if a source appears in bothpublic/_redirectsand front matter.public/_redirectskeeps only the non-content rules: feed, sitemap, retired pages, and legacy slug fixes with no front matter.astro-e2e.ymlandbuild-and-deploy.ymlnow runscript/validate-redirects dist-astro/_redirectsafter the build. The pre-build check onpublic/_redirectsinci.ymlis unchanged.redirects.spec.tsand the redirect block inpages.spec.tsnow read the built file, lazily.post-metadata.tsand the schema are untouched, soisExternalviaredirect_tostill works.Before/after: effective redirect rules
These are the sorted non-comment rules in
dist-astro/_redirects, from a build ofmaincompared with this branch.Identical: 63 rules both ways, empty diff.
Of those, 22 lines moved out of
public/_redirectsand into generated output. None were added and none were dropped. The moved lines are:/books,/books-for-geeks,/recommended-reading, and/cv, fromother-recommended-reading.mdandresume.md.redirect_toposts.rules-of-communicating-at-github,types-of-pull-requests,eight-things-i-wish-i-knew-my-first-week, andmoderating-open-source-conversations.script/validate-redirects dist-astro/_redirectsreports 63 redirects and 0 issues, with no duplicate sources and no loops.Before/after: sitemap URLs
204 -> 201. The only removals are the three
redirect_toposts:/2012/04/23/enterprise-open-source-usage-is-up-but-challenges-remain//2015/04/27/eight-lessons-learned-hacking-on-github-pages-for-six-months//2023/10/04/how-to-communicate-like-a-github-engineer/This is the one intentional behavior change. Those URLs 301 off-site in production (
curl -Ion the 2012 URL returns a 301 to TechCrunch), so they shouldn't be in the sitemap. No content file currently setssitemap: false, so there are no other changes.Follow-up (not in this PR)
About 11 legacy slug-correction posts have redirects in
public/_redirectsbut noredirect_fromin front matter. Examples arewhat-is-a-hacker,open-collabortion,source-disclosed,jekyll-collections, andcathedral-bazaar-management. Moving them into front matter would finish the migration. I left them alone to avoid touching post files here.Verification
npm run check: 0 errors.front-matter-routes.test.ts, pluspost-metadata.test.ts, all pass.SKIP_PDF=1 npm run build: succeeds and logsAppended 22 front matter redirects to _redirects.main, as above.script/validate-redirectspasses on bothpublic/_redirectsanddist-astro/_redirects.redirects.spec.tsand thepages.spec.tsredirect tests, 22 pass.🤖 Generated with Claude Code