blinkk / rootjs

Root.js – A full-featured web development tool with a built-in CMS.
https://rootjs.dev
MIT License
5 stars 0 forks source link

feat: impl sitemap.xml output for ssg builds #340

Closed stevenle closed 1 month ago

stevenle commented 1 month ago

@jeremydw this is a sample output:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
  <loc>https://rootjs.dev/</loc>
  <xhtml:link rel="alternate" hreflang="en" href="https://rootjs.dev/en/" />
  <xhtml:link rel="alternate" hreflang="de" href="https://rootjs.dev/de/" />
  <xhtml:link rel="alternate" hreflang="es" href="https://rootjs.dev/es/" />
</url>
<url>
  <loc>https://rootjs.dev/foo/</loc>
  <xhtml:link rel="alternate" hreflang="en" href="https://rootjs.dev/en/foo/" />
  <xhtml:link rel="alternate" hreflang="de" href="https://rootjs.dev/de/foo/" />
  <xhtml:link rel="alternate" hreflang="es" href="https://rootjs.dev/es/foo/" />
</url>
<url>
  <loc>https://rootjs.dev/de/</loc>
</url>
<url>
  <loc>https://rootjs.dev/en/</loc>
</url>
<url>
  <loc>https://rootjs.dev/es/</loc>
</url>
<url>
  <loc>https://rootjs.dev/de/foo/</loc>
</url>
<url>
  <loc>https://rootjs.dev/es/foo/</loc>
</url>
<url>
  <loc>https://rootjs.dev/en/foo/</loc>
</url>
</urlset>

this look correct to you? the localized paths are added as "alts" to the "default locale route" and also as their own line item. there isn't currently a way to "noindex" routes, i can add that in a future update when the use case is needed.

jeremydw commented 1 month ago

I think there are two improvements to make (although I feel like depending on who I have review my sitemaps, I occasionally get different feedback):

  1. The localized routes should specify alternates as well, e.g. /de/foo/ should list /en/foo/ as an alternate.
  2. For sites that implement geolocation or "locale fallback" (i.e. at URLs without a locale specified in the path), there should be an x-default item that points to that URL.

I like to look at this sitemap as an example: https://mapsplatform.google.com/sitemap.xml (although it doesn't have the XML schema linked so you need to view source - that's implemented in your output correctly) :)

Oh, one other thing worth flagging (not sure if it's already accounted for though), the 404 route shouldn't be included in the sitemap (our non-Root.js projects had this as an issue for a while).

stevenle commented 1 month ago

Here's the new sample output:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
  <loc>https://rootjs.dev/</loc>
  <xhtml:link rel="alternate" hreflang="de" href="https://rootjs.dev/de/" />
  <xhtml:link rel="alternate" hreflang="es" href="https://rootjs.dev/es/" />
  <xhtml:link rel="alternate" hreflang="fr" href="https://rootjs.dev/fr/" />
  <xhtml:link rel="alternate" hreflang="en" href="https://rootjs.dev/en/" />
</url>
<url>
  <loc>https://rootjs.dev/en/</loc>
  <xhtml:link rel="alternate" hreflang="x-default" href="https://rootjs.dev/" />
  <xhtml:link rel="alternate" hreflang="de" href="https://rootjs.dev/de/" />
  <xhtml:link rel="alternate" hreflang="es" href="https://rootjs.dev/es/" />
  <xhtml:link rel="alternate" hreflang="fr" href="https://rootjs.dev/fr/" />
</url>
<url>
  <loc>https://rootjs.dev/foo/</loc>
  <xhtml:link rel="alternate" hreflang="de" href="https://rootjs.dev/de/foo/" />
  <xhtml:link rel="alternate" hreflang="es" href="https://rootjs.dev/es/foo/" />
  <xhtml:link rel="alternate" hreflang="fr" href="https://rootjs.dev/fr/foo/" />
  <xhtml:link rel="alternate" hreflang="en" href="https://rootjs.dev/en/foo/" />
</url>
<url>
  <loc>https://rootjs.dev/en/foo/</loc>
  <xhtml:link rel="alternate" hreflang="x-default" href="https://rootjs.dev/foo/" />
  <xhtml:link rel="alternate" hreflang="de" href="https://rootjs.dev/de/foo/" />
  <xhtml:link rel="alternate" hreflang="es" href="https://rootjs.dev/es/foo/" />
  <xhtml:link rel="alternate" hreflang="fr" href="https://rootjs.dev/fr/foo/" />
</url>
<url>
  <loc>https://rootjs.dev/fr/</loc>
  <xhtml:link rel="alternate" hreflang="x-default" href="https://rootjs.dev/" />
  <xhtml:link rel="alternate" hreflang="de" href="https://rootjs.dev/de/" />
  <xhtml:link rel="alternate" hreflang="es" href="https://rootjs.dev/es/" />
  <xhtml:link rel="alternate" hreflang="en" href="https://rootjs.dev/en/" />
</url>
<url>
  <loc>https://rootjs.dev/es/</loc>
  <xhtml:link rel="alternate" hreflang="x-default" href="https://rootjs.dev/" />
  <xhtml:link rel="alternate" hreflang="de" href="https://rootjs.dev/de/" />
  <xhtml:link rel="alternate" hreflang="fr" href="https://rootjs.dev/fr/" />
  <xhtml:link rel="alternate" hreflang="en" href="https://rootjs.dev/en/" />
</url>
<url>
  <loc>https://rootjs.dev/es/foo/</loc>
  <xhtml:link rel="alternate" hreflang="x-default" href="https://rootjs.dev/foo/" />
  <xhtml:link rel="alternate" hreflang="de" href="https://rootjs.dev/de/foo/" />
  <xhtml:link rel="alternate" hreflang="fr" href="https://rootjs.dev/fr/foo/" />
  <xhtml:link rel="alternate" hreflang="en" href="https://rootjs.dev/en/foo/" />
</url>
<url>
  <loc>https://rootjs.dev/de/foo/</loc>
  <xhtml:link rel="alternate" hreflang="x-default" href="https://rootjs.dev/foo/" />
  <xhtml:link rel="alternate" hreflang="es" href="https://rootjs.dev/es/foo/" />
  <xhtml:link rel="alternate" hreflang="fr" href="https://rootjs.dev/fr/foo/" />
  <xhtml:link rel="alternate" hreflang="en" href="https://rootjs.dev/en/foo/" />
</url>
<url>
  <loc>https://rootjs.dev/de/</loc>
  <xhtml:link rel="alternate" hreflang="x-default" href="https://rootjs.dev/" />
  <xhtml:link rel="alternate" hreflang="es" href="https://rootjs.dev/es/" />
  <xhtml:link rel="alternate" hreflang="fr" href="https://rootjs.dev/fr/" />
  <xhtml:link rel="alternate" hreflang="en" href="https://rootjs.dev/en/" />
</url>
<url>
  <loc>https://rootjs.dev/fr/foo/</loc>
  <xhtml:link rel="alternate" hreflang="x-default" href="https://rootjs.dev/foo/" />
  <xhtml:link rel="alternate" hreflang="de" href="https://rootjs.dev/de/foo/" />
  <xhtml:link rel="alternate" hreflang="es" href="https://rootjs.dev/es/foo/" />
  <xhtml:link rel="alternate" hreflang="en" href="https://rootjs.dev/en/foo/" />
</url>
</urlset>

LMK if this looks good to you. (404.html is natively handled.)

stevenle commented 1 month ago

@jeremydw thanks for the review!

jeremydw commented 1 month ago

Thanks for adding the feature 🔥