ekoeryanto / vuepress-plugin-sitemap

Sitemap generator plugin for vuepress.
MIT License
93 stars 19 forks source link

fix: sitemap entries #15

Closed seifertm closed 5 years ago

seifertm commented 5 years ago

Consider the following vuepress page with the default locale en located at / and a second locale de located at /de/:

index.md
/de/index.md

Currently, vuepress-plugin-sitemap will generate the following sitemap entry:

  <url>
    <loc>https://example.com/index.html</loc>
    <changefreq>monthly</changefreq>
    <xhtml:link rel="alternate" hreflang="en" href="https://example.com/index.html"/>
    <xhtml:link rel="alternate" hreflang="de" href="https://example.com/de/index.html"/>
  </url>

However, Google is unhappy with this, because the hreflang links reference pages that have no back reference in the sitemap. Google notifies webmasters about a problem in their sitemap. Please refer to the corresponding Google support page on this topic.

This PR ensures that each hreflang references an existing location in sitemap. For our example page, the sitemap will look like this:

  <url>
    <loc>https://example.com/index.html</loc>
    <changefreq>monthly</changefreq>
    <xhtml:link rel="alternate" hreflang="en" href="https://example.com/index.html"/>
    <xhtml:link rel="alternate" hreflang="de" href="https://example.com/de/index.html"/>
  </url>
  <url>
    <loc>https://example.com/de/index.html</loc>
    <changefreq>monthly</changefreq>
    <xhtml:link rel="alternate" hreflang="en" href="https://example.com/index.html"/>
    <xhtml:link rel="alternate" hreflang="de" href="https://example.com/de/index.html"/>
  </url>

The new code iterates over all pages and normalizes their URL by stripping the locale's prefix from the URL. The loop builds a map with the normalized URL as a key and a list of locale prefixes as value. When sitemap data is assembled in pageMap, the mapping used to determine the hreflang links for the current page.

ekoeryanto commented 5 years ago

Thanks!

seifertm commented 5 years ago

Thank you for taking care of this so quickly! Looking forward to the next release :)

I'd also be willing to provide test cases for this PR, if you intend to add automated tests at some point. Just ping me.

ekoeryanto commented 5 years ago

That's great!

ekoeryanto commented 5 years ago

Eh, I invited you as collaborator as I am busy this month.