Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirects: improvements from design doc #10881

Merged
merged 27 commits into from
Jan 2, 2024
Merged

Redirects: improvements from design doc #10881

merged 27 commits into from
Jan 2, 2024

Conversation

stsewd
Copy link
Member

@stsewd stsewd commented Oct 31, 2023

Implements https://dev.readthedocs.io/en/latest/design/redirects.html

  • New fields: status code, description, enabled, force, position.
  • Redirects now have an explicit order, the code from automation rules was re-used.
    For the templates, I also re-used the styles from automation rules.
  • Prefix redirects are removed in favor of exact redirects + wildcard
  • API V3 and form will show an error if the user tries to use the old style wildcard form ($rest), or use the old types.
  • Variables like path/full_path were renamed to filename and path, we use those names on proxito and friends.
  • Redirect are not run on pull request previews.
  • "Sphinx redirects" were renamed.
  • get_redirect_path was previously re-running the same checks from get_redirect_path_with_status, we don't do this anymore, we avoid duplication and should be a little faster.
  • The query used to match redirects is improved to run just the required redirects, depending on the path and filename. Did a quick test, and performance is kind of the same, hopefully we will see an improvement on a more large scale, but evaluation of redirects is fast currently.
  • Docs were re-structured a little, now all examples are after the list of redirect types, since they apply to all types.
  • The "limitations" section was at the start of the document, but they apply to user-redirects (not built-in redirects).
  • Matching ext-theme changes at Redirects: match recent improvements ext-theme#237.
  • Docs: https://docs--10881.org.readthedocs.build/en/10881/user-defined-redirects.html
  • We need to decide what to do with a couple of redirects that are already using the new syntax, since it may be unexpected for some projects if they start working, full list at https://readthedocs.slack.com/archives/C02DNG2HSNP/p1701962946332549.

Questions

  • Should we allow forced redirects now? Pages are cached on CF, and we don't have many projects using forced redirects, evaluation of redirects is fast.

How to deploy

  • Deploy web extra
  • Run the migration redirects 0006
  • Deploy webs
  • Run the data migration redirects 0007
  • Run the following code from a django shell
from readthedocs.core.resolver import Resolver
from readthedocs.projects.models import Project

for project in Project.objects.filter(redirects__isnull=False).distinct():
    default_path = Resolver().resolve_path(project)

    for redirect in project.redirects.all():
        # Migrate exact redirects with a wildcard to new syntax.
        if redirect.redirect_type == "exact" and redirect.from_url.endswith(
            "$rest"
        ):
            redirect.from_url = redirect.from_url.removesuffix("$rest") + "*"
            redirect.to_url = redirect.to_url + ":splat"

        # Migrate prefix redirects to exact redirects with a wildcard.
        if redirect.redirect_type == "prefix":
            redirect.redirect_type = "exact"
            redirect.from_url = redirect.from_url + "*"
            redirect.to_url = f"{default_path}/:splat"

        # Saving will normalize from_url and to_url as needed.
        redirect.save()

We would experiment some redirects not working while the migration takes place,
we don't have that many redirects, so it should be quick. We need to save each redirect individually so they are normalized as the new code expects.

Screenshots

Screenshot from 2023-12-06 17-06-11

Screenshot from 2023-12-06 17-01-57

Closing credits?

stsewd added a commit to readthedocs/ext-theme that referenced this pull request Dec 6, 2023
Matches readthedocs/readthedocs.org#10881.

Applied the same code we have for automation rules.
stsewd added a commit to readthedocs/ext-theme that referenced this pull request Dec 6, 2023
Matches readthedocs/readthedocs.org#10881.

Applied the same code we have for automation rules.
@@ -1283,6 +1283,8 @@ class VersionAutomationRule(PolymorphicModel, TimeStampedModel):
choices=VERSION_TYPES,
)

_position_manager = ProjectItemPositionManager(position_field_name="priority")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be a mixin + a class attribute, not strong opinion.

def __init__(self, position_field_name):
self.position_field_name = position_field_name

def change_position_before_save(self, item):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All this code comes from automation rules, it's the same code, just changed to use self.position_field_name instead of priority.

@stsewd stsewd marked this pull request as ready for review December 7, 2023 18:59
Copy link
Member

@ericholscher ericholscher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a great change. I'd be curious to hear more about the performance changes that come from this. If we think we can do redirect queries on each pageview, I'd be strongly in favor of removing the limitations around Forced redirects, and enabling them by default I think.

For this redirect to work, your old version must be disabled,
if the version is still active, you can use the ``Force Redirect`` option.

Creating a shortlink
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love all the examples in this page 💯

readthedocs/redirects/models.py Show resolved Hide resolved

if filename in ["/index.html", "/"]:
# If the filename is the root index file, we only need to match page and exact redirects.
# Since we don't have a filename to redirect to, since ``/``/``/index.html`` is already the root.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the /``/``/index.html -- is that just the path ///index.html?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's A/B, I have reworded this to make it less confusing.

@stsewd
Copy link
Member Author

stsewd commented Jan 2, 2024

If we think we can do redirect queries on each pageview, I'd be strongly in favor of removing the limitations around Forced redirects, and enabling them by default I think.

We can decide about enabling them after shipping these changes. Currently, matching redirects takes less than 1ms, so they are already fast, I don't think we will have any problems with that.

@ericholscher
Copy link
Member

If we think we can do redirect queries on each pageview, I'd be strongly in favor of removing the limitations around Forced redirects, and enabling them by default I think.

We can decide about enabling them after shipping these changes. Currently, matching redirects takes less than 1ms, so they are already fast, I don't think we will have any problems with that.

Sounds good. I'm 👍 on merging this and doing that work next.

@stsewd stsewd merged commit 06abe5d into main Jan 2, 2024
7 checks passed
@stsewd stsewd deleted the redirects-improvements branch January 2, 2024 19:21
stsewd added a commit to readthedocs/ext-theme that referenced this pull request Jan 2, 2024
Matches readthedocs/readthedocs.org#10881.

Applied the same code we have for automation rules.
stsewd added a commit to readthedocs/blog that referenced this pull request Jan 3, 2024
bensze01 added a commit to bensze01/mbedtls-docs that referenced this pull request Jan 16, 2024
Migrate to the new redirect format introduced by ReadTheDocs in
readthedocs/readthedocs.org#10881

The wildcard redirect is broken at the moment due to faulty handling of
infinite redirect detection on the ReadTheDocs side.

Signed-off-by: Bence Szépkúti <[email protected]>
bensze01 added a commit to bensze01/mbedtls-docs that referenced this pull request Jan 16, 2024
Migrate to the new redirect format introduced by ReadTheDocs in
readthedocs/readthedocs.org#10881

The wildcard redirect is broken at the moment due to faulty infinite
redirect detection on the ReadTheDocs side.

Signed-off-by: Bence Szépkúti <[email protected]>
bensze01 added a commit to bensze01/mbedtls that referenced this pull request Jan 16, 2024
Migrate to the new redirect format introduced by ReadTheDocs in
readthedocs/readthedocs.org#10881

Signed-off-by: Bence Szépkúti <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants