Repository: readthedocs/readthedocs.org

Issue:
Sitemap sort order may prioritise unstable/development documentation
In `sitemap.xml`, the sort order should prioritise the user-selected default version (Admin > Versions > Default Version) in the backend instead of setting `latest` to highest priority. In many cases (and also by [definition](https://docs.readthedocs.io/en/stable/versions.html)), `latest` points at development documentation while `stable` is the version most people should be using, and should appear first in search results.

Example from: https://docs.dash.org/sitemap.xml
`stable` should be higher priority than `latest`, but the reverse is true.
```
<url>
  <loc>http://docs.dash.org/en/latest/</loc>
  <changefreq>daily</changefreq>
  <priority>1</priority>
</url>

<url>
  <loc>http://docs.dash.org/en/stable/</loc>
  <changefreq>weekly</changefreq>
  <priority>0.9</priority>
</url>
```


Discussion:
@strophy do you think the `changefreq` should be as it is right now? So, in your example it could be

* `stable` with `priority=1` and `changefreq=weekly`
* `latest` with `priority=0.9` but `changefreq=daily`

What do you think?
To some extent, it depends on the current development focus. Sometimes patches are released to a stable version, which results in updates to the `stable` documentation. Other times the focus is on new features, so `latest` sees most of the updates, and beta users (testnet) are the main audience.

If it is not possible to define `changefreq` in settings or conf files, I think your proposal makes reasonable sense for most projects. The bug report pertains mostly to respecting the user-selected "Default Version", any `changefreq` weekly or faster is fine with me personally.
OK, thanks for the explanation.

We won't probably accept a configuration option for this. So, I prefer to go forward with my proposal considering that you also think it's reasonable for most of the projects :)

Resolving patch:
diff --git a/readthedocs/core/views/serve.py b/readthedocs/core/views/serve.py
--- a/readthedocs/core/views/serve.py
+++ b/readthedocs/core/views/serve.py
@@ -38,6 +38,7 @@
 from django.views.decorators.cache import cache_page
 from django.views.static import serve
 
+from readthedocs.builds.constants import LATEST, STABLE
 from readthedocs.builds.models import Version
 from readthedocs.core.permissions import AdminPermission
 from readthedocs.core.resolver import resolve, resolve_path
@@ -373,14 +374,14 @@ def changefreqs_generator():
         """
         Generator returning ``changefreq`` needed by sitemap.xml.
 
-        It returns ``daily`` on first iteration, then ``weekly`` and then it
+        It returns ``weekly`` on first iteration, then ``daily`` and then it
         will return always ``monthly``.
 
         We are using ``monthly`` as last value because ``never`` is too
         aggressive. If the tag is removed and a branch is created with the same
         name, we will want bots to revisit this.
         """
-        changefreqs = ['daily', 'weekly']
+        changefreqs = ['weekly', 'daily']
         yield from itertools.chain(changefreqs, itertools.repeat('monthly'))
 
     if project.privacy_level == constants.PRIVATE:
@@ -392,6 +393,19 @@ def changefreqs_generator():
             only_active=True,
         ),
     )
+
+    # This is a hack to swap the latest version with
+    # stable version to get the stable version first in the sitemap.
+    # We want stable with priority=1 and changefreq='weekly' and
+    # latest with priority=0.9 and changefreq='daily'
+    # More details on this: https://github.com/rtfd/readthedocs.org/issues/5447
+    if (
+        len(sorted_versions) >= 2 and
+        sorted_versions[0].slug == LATEST and
+        sorted_versions[1].slug == STABLE
+    ):
+        sorted_versions[0], sorted_versions[1] = sorted_versions[1], sorted_versions[0]
+
     versions = []
     for version, priority, changefreq in zip(
             sorted_versions,


Test patch:
diff --git a/readthedocs/rtd_tests/tests/test_doc_serving.py b/readthedocs/rtd_tests/tests/test_doc_serving.py
--- a/readthedocs/rtd_tests/tests/test_doc_serving.py
+++ b/readthedocs/rtd_tests/tests/test_doc_serving.py
@@ -240,6 +240,15 @@ def test_sitemap_xml(self):
             project=self.public,
             active=True
         )
+        stable_version = fixture.get(
+            Version,
+            identifier='stable',
+            verbose_name='stable',
+            slug='stable',
+            privacy_level=constants.PUBLIC,
+            project=self.public,
+            active=True
+        )
         # This also creates a Version `latest` Automatically for this project
         translation = fixture.get(
             Project,
@@ -269,7 +278,7 @@ def test_sitemap_xml(self):
                 ),
             )
 
-        # stable is marked as PRIVATE and should not appear here
+        # PRIVATE version should not appear here
         self.assertNotContains(
             response,
             self.public.get_docs_url(
@@ -294,6 +303,28 @@ def test_sitemap_xml(self):
         # in language and country value. (zh_CN should be zh-CN)
         self.assertContains(response, 'zh-CN')
 
+        # Check if STABLE version has 'priority of 1 and changefreq of weekly.
+        self.assertEqual(
+            response.context['versions'][0]['loc'],
+            self.public.get_docs_url(
+                version_slug=stable_version.slug,
+                lang_slug=self.public.language,
+                private=False,
+            ),)
+        self.assertEqual(response.context['versions'][0]['priority'], 1)
+        self.assertEqual(response.context['versions'][0]['changefreq'], 'weekly')
+
+        # Check if LATEST version has priority of 0.9 and changefreq of daily.
+        self.assertEqual(
+            response.context['versions'][1]['loc'],
+            self.public.get_docs_url(
+                version_slug='latest',
+                lang_slug=self.public.language,
+                private=False,
+            ),)
+        self.assertEqual(response.context['versions'][1]['priority'], 0.9)
+        self.assertEqual(response.context['versions'][1]['changefreq'], 'daily')
+
     @override_settings(
         PYTHON_MEDIA=True,
         USE_SUBDOMAIN=False,