Skip to content

Commit

Permalink
fix(i18n): don't transform links with protocol
Browse files Browse the repository at this point in the history
Fixes #262
  • Loading branch information
harlan-zw committed May 6, 2024
1 parent 8494928 commit 0949698
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/runtime/nitro/sitemap/urlset/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { joinURL, parseURL, withHttps, withLeadingSlash } from 'ufo'
import { hasProtocol, joinURL, parseURL, withHttps, withLeadingSlash } from 'ufo'
import type {
AlternativeEntry,
ModuleRuntimeConfig,
Expand All @@ -14,15 +14,17 @@ export function normaliseI18nSources(sources: SitemapSourceResolved[], { autoI18
const urls = (s.urls || []).map((_url) => {
const url = typeof _url === 'string' ? { loc: _url } : _url
url.loc = url.loc || url.url!
url.loc = withLeadingSlash(url.loc)
if (!hasProtocol(url.loc, { acceptRelative: true }))
url.loc = withLeadingSlash(url.loc)

return url
})
s.urls = urls.map((url) => {
// only if the url wasn't already configured, excludes page, etc
if (url._sitemap || url._i18nTransform)
return url
// if the url starts with a prefix, we should automatically bundle it to the correct sitemap using _sitemap
if (url.loc) {
if (url.loc && !hasProtocol(url.loc, { acceptRelative: true })) {
const match = splitForLocales(url.loc, autoI18n.locales.map(l => l.code))
const localeCode = match[0] || autoI18n.defaultLocale
const pathWithoutPrefix = match[1]
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/i18n/server/routes/i18n-urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ export default defineSitemapEventHandler(() => {
{
loc: 'english-url', // issue with en being picked up as the locale
},
// absolute URL issue
{ loc: 'https://www.somedomain.com/abc/def' },
]
})
3 changes: 3 additions & 0 deletions test/integration/i18n/dynamic-urls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ describe('i18n dynamic urls', () => {
<xhtml:link rel="alternate" hreflang="fr-FR" href="https://nuxtseo.com/fr/__sitemap/url" />
<xhtml:link rel="alternate" hreflang="es-ES" href="https://nuxtseo.com/es/__sitemap/url" />
</url>
<url>
<loc>https://www.somedomain.com/abc/def</loc>
</url>
<url>
<loc>https://nuxtseo.com/en/dynamic/foo</loc>
<xhtml:link rel="alternate" href="https://nuxtseo.com/en/dynamic/foo" hreflang="x-default" />
Expand Down

0 comments on commit 0949698

Please sign in to comment.