Skip to main content

15 posts tagged with "Docusaurus"

View all tags

Duplicate hreflang in Docusaurus? Built-in i18n emits them

Β· 4 min read

Running an SEO audit on a multilingual site, you read the page source and find every <link rel="alternate" hreflang="..."> appearing twice β€” two full hreflang sets coexisting, with overlapping language codes.

Encountered this while building CCLEE Docusaurus Theme β€” a documentation theme built on Docusaurus 3.x with multilingual and SEO infrastructure baked in; hreflang is exactly the kind of thing it must get right natively.

Symptom: two hreflang sets in the head​

In the page head, the same locale's alternate link appears twice β€” one set from Docusaurus's built-in i18n, one from a custom injector. Each set is individually correct; together they conflict. One-line self-check:

curl -s https://your-site.com/ | grep -o 'hreflang="[^"]*"' | sort | uniq -c

Any locale code (including x-default) appearing more than once means duplication. If you searched for "duplicate hreflang", "hreflang emitted twice" or "Docusaurus two sets of hreflang" β€” same issue.

Root cause: built-in i18n already generates them; the plugin adds a second set​

Once i18n.locales is configured, Docusaurus's built-in i18n automatically emits complete hreflang alternates (including x-default) for every page β€” out of the box, no plugin involved. This site's custom SEO plugin, however, also had hreflang injection logic, so every page received two sets:

  • Built-in i18n: generated from the locales config, rendered with the page;
  • Custom plugin: another set injected into the head, with language codes heavily overlapping the generated ones.

hreflang semantics are declaration, not accumulation: two sets that disagree on even one entry (say, a different URL for a locale) are conflicting annotations as far as search engines are concerned.

The fix: remove the custom injection, rely on built-in i18n​

The fix is subtraction β€” delete the custom plugin's hreflang injection entirely and keep only what Docusaurus generates:

// custom SEO plugin
- const links = locales.map((locale) => ({
- rel: 'alternate',
- hreflang: locale,
- href: urlFor(locale),
- }));
- // inject into head ...

Post-fix live measurement (homepages of both sites): en-US, zh-CN and x-default each appear exactly once β€” one clean set. Re-run the grep above after the change; ship only when every locale code counts exactly one.

Boundary cases​

  • When manual injection is actually needed: only for pages Docusaurus doesn't generate, or variant URLs that don't follow i18n routing rules. Even then, grep the current state first β€” the built-in generator may already cover the pair you're about to declare.
  • Duplicates don't only come from your own plugin: multiple SEO plugins stacking, or theme plus plugin each contributing a set, are common sources; debug by disabling one injector at a time β€” every head tag should have exactly one owner.
  • hreflang vs canonical: canonical handles "duplicate content in the same language", hreflang handles "cross-language variants"; neither replaces the other, and custom logic shouldn't entangle the two.

Watch out

  • Built-in capability before custom code: Docusaurus ships i18n, sitemap and meta-description infrastructure out of the box β€” confirm what's already covered before writing injectors, or you're manufacturing duplicates.
  • Make the grep self-check part of the release flow: one line of cost that catches an entire class of head-tag conflicts.
  • Search engines ignore conflicting hreflang silently β€” no error, no warning. It's the same "silent, no alarm" family as dotenv truncating values.

FAQ​

What are hreflang tags in SEO?​

They tell search engines which language/region variants a page has and which URL each maps to, powering regional targeting for multilingual sites. Format: <link rel="alternate" hreflang="zh-CN" href="...">.

How do I check hreflang tags on a page?​

Fetch the page source and count: curl -s https://your-site.com/ | grep -o 'hreflang="[^"]*"' | sort | uniq -c. Each locale code (including x-default) should appear exactly once; more means a duplicate injection somewhere.

Does Docusaurus generate hreflang automatically?​

Yes β€” with i18n.locales configured, Docusaurus emits the full alternate set plus x-default for every page out of the box. No plugin needed; adding one is how duplicates happen.

CCLEE

Independent developer, 24 years in e-commerce, focused on grounding AI in real business scenarios.

Work with me

Docusaurus scripts Build Fails with inline Script? Only src Is Accepted, Not content

Β· 5 min read

Dropping a { content: '...' } entry into the scripts array of docusaurus.config.ts to inject an inline script (Baidu Tongji's IIFE, for example) fails npm run build immediately.

Encountered this while building CCLEE Docusaurus Theme β€” an advanced docs theme built on Docusaurus 3.x, with a purple theme, dark mode, and Tailwind typography enhancements, a production-ready docs site template out of the box.

TL;DR​

Docusaurus's scripts config only accepts src, not inline content. Move the inline script into static/js/ and reference it via { src: '/js/xxx.js', async: true }. If the script loads an external domain, also update your CSP.

Symptom​

Following Baidu Tongji's official snippet, the instinct is to inline it straight into scripts:

// docusaurus.config.ts
const config: Config = {
scripts: [
{
content: `var _hmt=_hmt||[];(function(){var hm=document.createElement("script");hm.src="https://hm.baidu.com/hm.js?XXXX";var s=document.getElementsByTagName("script")[0];s.parentNode.insertBefore(hm,s);})();`,
},
],
};

The build fails right away:

[ERROR] Error: "scripts[1]" is invalid.
A script must be a plain string (the src), or an object with at least a "src" property.
at validateConfig (.../configValidation.js:397:15)

Root Cause​

The scripts config is validated at build time by validateScripts, entry by entry. Only two shapes are allowed:

  1. A plain string: treated as src
  2. An object: must contain a src property; async, defer, data-* are optional

By design, scripts only emits tags of the form <script src="..." /> and reserves no content / innerHTML field for inline scripts. So no matter how short your inline content is, validation throws as soon as src is missing. Local builds and Vercel remote builds fail identically.

To inject an inline script that depends on build-time variables, use the top-level headTags config (tagName: 'script' + innerHTML) instead of scripts.

Solution​

1. Put the inline script under static/js/​

// static/js/baidu-tongji.js
var _hmt = _hmt || [];
(function () {
var hm = document.createElement('script');
hm.src = 'https://hm.baidu.com/hm.js?XXXX';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(hm, s);
})();

Files under static/ are copied as-is to the site root, so the final URL is /js/baidu-tongji.js.

2. Reference it from scripts via src​

// docusaurus.config.ts
scripts: [
// Existing Umami setup (see /blog/docusaurus-umami-analytics)
{
src: 'https://tj.ccleeai.com/script.js',
async: true,
'data-website-id': 'xxxx',
},
// Baidu Tongji: via static file
{
src: '/js/baidu-tongji.js',
async: true,
},
],

3. Update CSP accordingly​

If your site sets Content-Security-Policy (recommended β€” see our earlier Umami integration and CSP walkthrough), every newly added external domain must be allowlisted or the script will be blocked by the browser:

themeConfig: {
metadata: [
{
'http-equiv': 'Content-Security-Policy',
// Add https://hm.baidu.com to script-src
// Also allow it in connect-src and img-src (hm.js sends image pixels and fetch reports)
content: "default-src 'self'; " +
"script-src 'self' 'unsafe-inline' 'unsafe-eval' https://tj.ccleeai.com https://hm.baidu.com; " +
"connect-src 'self' https://tj.ccleeai.com https://hm.baidu.com; " +
"img-src 'self' data: https://hm.baidu.com; " +
"style-src 'self' 'unsafe-inline'; " +
"object-src 'none'; base-uri 'self'",
},
],
},

Baidu Tongji's IIFE internally calls document.createElement('script') to inject <script src="hm.baidu.com/...">, so 'unsafe-inline' alone is not enough β€” hm.baidu.com must be in script-src.

Notes

  • A plain string and src are equivalent in the scripts array: 'https://x/a.js' and { src: 'https://x/a.js' } behave the same
  • A path starting with / is relative to the site root (the copy output of static/), not the filesystem root
  • If the script depends on runtime variables and must be inline, use the top-level headTags, not scripts; headTags supports innerHTML
  • Multiple analytics setups (Umami + Baidu + GA) can coexist, but each external domain must be added to the CSP individually or its reports will be silently blocked

FAQ​

How do I configure custom scripts in Docusaurus?​

Add entries to the top-level scripts array in docusaurus.config.ts. Each entry is either a string (treated as src) or an object with a src property. Inline content cannot go in a content field β€” put the script under static/js/ and reference it via src: '/js/xxx.js'. If you also need async, defer, or custom data-* attributes, place them in the same object as src.

Why does Docusaurus scripts not support inline content?​

The scripts config emits <script src="..."> tags at build time and has no field reserved for inline scripts. The validator (validateScripts in configValidation.ts) walks each entry and throws A script must be a plain string, or an object with at least a "src" property whenever src is missing, no matter how complete your content is. This is a deliberate API boundary β€” inline injection is delegated to headTags.

How do I inject inline JavaScript (like Baidu Tongji) in Docusaurus?​

The most robust approach is "static file + internal dynamic injection": save the official IIFE snippet to static/js/baidu-tongji.js, reference it from scripts via { src: '/js/baidu-tongji.js', async: true }, and let the script itself document.createElement('script') to load hm.baidu.com/hm.js. Finally, add https://hm.baidu.com to script-src, connect-src, and img-src of your CSP, or reports will be blocked by the browser.

CCLEE

Independent developer, 24 years in e-commerce, focused on grounding AI in real business scenarios.

Work with me