diff --git a/src/js/lib/SearchEngineOptimization.ts b/src/js/lib/SearchEngineOptimization.ts index 6626590..7a56ab4 100644 --- a/src/js/lib/SearchEngineOptimization.ts +++ b/src/js/lib/SearchEngineOptimization.ts @@ -111,15 +111,17 @@ export class SearchEngineOptimization { for (const locale of availableLocales) { const localizedUrl = this.localizeUrl(new URL(doc.URL), locale) - const existingLink = doc.head.querySelector(`link[rel="alternate"][hreflang="${locale}"]`) - - if (!existingLink) { - link = document.createElement('link') - link.rel = 'alternate' - link.hreflang = locale - link.href = localizedUrl - - doc.head.appendChild(link) + if (doc.head !== null) { + const existingLink = doc.head.querySelector(`link[rel="alternate"][hreflang="${locale}"]`) + + if (!existingLink) { + link = document.createElement('link') + link.rel = 'alternate' + link.hreflang = locale + link.href = localizedUrl + + doc.head.appendChild(link) + } } } }