Skip to content

Commit ac6bccf

Browse files
authored
Merge pull request #5724 from saadmk11/sitemap-sort-order
Sitemap sort order priorities updated
2 parents c03f839 + d959933 commit ac6bccf

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

readthedocs/core/views/serve.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from django.views.decorators.cache import cache_page
3939
from django.views.static import serve
4040

41+
from readthedocs.builds.constants import LATEST, STABLE
4142
from readthedocs.builds.models import Version
4243
from readthedocs.core.permissions import AdminPermission
4344
from readthedocs.core.resolver import resolve, resolve_path
@@ -373,14 +374,14 @@ def changefreqs_generator():
373374
"""
374375
Generator returning ``changefreq`` needed by sitemap.xml.
375376
376-
It returns ``daily`` on first iteration, then ``weekly`` and then it
377+
It returns ``weekly`` on first iteration, then ``daily`` and then it
377378
will return always ``monthly``.
378379
379380
We are using ``monthly`` as last value because ``never`` is too
380381
aggressive. If the tag is removed and a branch is created with the same
381382
name, we will want bots to revisit this.
382383
"""
383-
changefreqs = ['daily', 'weekly']
384+
changefreqs = ['weekly', 'daily']
384385
yield from itertools.chain(changefreqs, itertools.repeat('monthly'))
385386

386387
if project.privacy_level == constants.PRIVATE:
@@ -392,6 +393,19 @@ def changefreqs_generator():
392393
only_active=True,
393394
),
394395
)
396+
397+
# This is a hack to swap the latest version with
398+
# stable version to get the stable version first in the sitemap.
399+
# We want stable with priority=1 and changefreq='weekly' and
400+
# latest with priority=0.9 and changefreq='daily'
401+
# More details on this: https://github.com/rtfd/readthedocs.org/issues/5447
402+
if (
403+
len(sorted_versions) >= 2 and
404+
sorted_versions[0].slug == LATEST and
405+
sorted_versions[1].slug == STABLE
406+
):
407+
sorted_versions[0], sorted_versions[1] = sorted_versions[1], sorted_versions[0]
408+
395409
versions = []
396410
for version, priority, changefreq in zip(
397411
sorted_versions,

readthedocs/rtd_tests/tests/test_doc_serving.py

+32-1
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,15 @@ def test_sitemap_xml(self):
240240
project=self.public,
241241
active=True
242242
)
243+
stable_version = fixture.get(
244+
Version,
245+
identifier='stable',
246+
verbose_name='stable',
247+
slug='stable',
248+
privacy_level=constants.PUBLIC,
249+
project=self.public,
250+
active=True
251+
)
243252
# This also creates a Version `latest` Automatically for this project
244253
translation = fixture.get(
245254
Project,
@@ -269,7 +278,7 @@ def test_sitemap_xml(self):
269278
),
270279
)
271280

272-
# stable is marked as PRIVATE and should not appear here
281+
# PRIVATE version should not appear here
273282
self.assertNotContains(
274283
response,
275284
self.public.get_docs_url(
@@ -294,6 +303,28 @@ def test_sitemap_xml(self):
294303
# in language and country value. (zh_CN should be zh-CN)
295304
self.assertContains(response, 'zh-CN')
296305

306+
# Check if STABLE version has 'priority of 1 and changefreq of weekly.
307+
self.assertEqual(
308+
response.context['versions'][0]['loc'],
309+
self.public.get_docs_url(
310+
version_slug=stable_version.slug,
311+
lang_slug=self.public.language,
312+
private=False,
313+
),)
314+
self.assertEqual(response.context['versions'][0]['priority'], 1)
315+
self.assertEqual(response.context['versions'][0]['changefreq'], 'weekly')
316+
317+
# Check if LATEST version has priority of 0.9 and changefreq of daily.
318+
self.assertEqual(
319+
response.context['versions'][1]['loc'],
320+
self.public.get_docs_url(
321+
version_slug='latest',
322+
lang_slug=self.public.language,
323+
private=False,
324+
),)
325+
self.assertEqual(response.context['versions'][1]['priority'], 0.9)
326+
self.assertEqual(response.context['versions'][1]['changefreq'], 'daily')
327+
297328
@override_settings(
298329
PYTHON_MEDIA=True,
299330
USE_SUBDOMAIN=False,

0 commit comments

Comments
 (0)