Checking hreflang on a bilingual static site with a 35-line Node script
My pension calculator has two language versions, English at / and Hindi at /hi/ , and most pages exist twice. I had always treated hreflang as a tag you paste once and forget. The problem is that it fails silently: nothing breaks in the browser, and search engines simply ignore alternate tags that don't agree with each other. So today I wrote a checker instead of trusting my build script. What…
A bilingual static website has two language versions, English at the root and Hindi at /hi/. Most pages exist in both languages, but hreflang tags were treated as something to paste once and forget. The author wrote a checker script to ensure the hreflang tags are consistent and correctly implemented, rather than relying on the build script.
The hreflang contract requires reciprocal links: if the English page points to the Hindi page, the Hindi page must also point back to the English page and list itself. The script checks for missing trailing slashes, self-reference, and return links. It confirmed that all 76 pages with hreflang passed the checks. The language switcher includes the same attributes for accessibility.
The author plans to test the script further by intentionally breaking a page and verifying it gets flagged.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.