8000 Support redirects from non-padded PEP numbers by AA-Turner · Pull Request #2421 · python/peps · GitHub
[go: up one dir, main page]

Skip to content

Support redirects from non-padded PEP numbers #2421

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

10000
Closed
wants to merge 5 commits into from

Conversation

AA-Turner
Copy link
Member

fixes #2420

A

Copy link
Member
@ezio-melotti ezio-melotti left a comment

Choose a reason for hiding this comment

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

I left a few comments but otherwise looks good. Is there a way to see it rendered online before merging it?

@AA-Turner
Copy link
Member Author

@ezio-melotti -- JS works for now. https://pep-previews--2421.org.readthedocs.build/42 or https://pep-previews--2421.org.readthedocs.build/042/ should both redirect to the proper PEP, and there is now a custom 404.

The auto-linker will need to be set up with no prefix to the domain & then just passing the number.

A

Co-authored-by: Ezio Melotti <ezio.melotti@gmail.com>
@ezio-melotti
Copy link
Member

https://pep-previews--2421.org.readthedocs.build/pep-8/ seems to work fine.
https://pep-previews--2421.org.readthedocs.build/pep-foo/ also works.
https://pep-previews--2421.org.readthedocs.build/pep-0123 creates a redirect loop, since it's a valid number, but refers to a pep that doesn't exist.

I this we should add a check on the length of the number. I'll leave a suggestion in the code.

<meta name="description" content="Python Enhancement Proposals (PEPs)"/>
<script>
const pepNumMatch = window.location.pathname.match(/^\d+$/)
if (pepNumMatch !== null) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (pepNumMatch !== null) {
if (pepNumMatch !== null && match[0].length < 4) {

Copy link
Member Author

Choose a reason for hiding this comment

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

Our comments crossed over, sorry!

We could check the number but if invalid the only harm of not checking would be an extra redirect from one invalid URL to another.

A

<link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,400;0,700;1,400&display=swap" rel="stylesheet">
<meta name="description" content="Python Enhancement Proposals (PEPs)"/>
<script>
const pepNumMatch = window.location.pathname.match(/^\d+$/)
Copy link
Member

Choose a reason for hiding this comment

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

With ^ and $ in the regex, it won't match pep-NNN. Did you just change this temporarily to test?
Note that in the suggestion below I used match[0] because of this change, but if my previous suggestion is restored it should be match[1].

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 won't match pep-NNN

From above, "I don't want to include the pep- prefix for the auto-redirects"

A

Copy link
Member
@ezio-melotti ezio-melotti Mar 12, 2022

Choose a reason for hiding this comment

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

Why not?
If we set the autolinking to https://peps.python.org/pep-<num>/ and <num> is a valid 4-digits number (e.g. pep-0008), then it will go straight to the right page. If we omit the pep- prefix, then it will have to go through an extra redirect if JS is enabled, or end up at the 404 page if JS is disabled.

In case of a valid PEP with a <4 digits number (e.g. pep-8) it will go to /pep-8/ and get redirected to /pep-0008.
In case of an invalid PEP with a <4 digits number (e.g. pep-123) it will go to /pep-123/, get redirected to /pep-0123, and then stop at the 404 page since the PEP doesn't exist and the id is already 4 digits long.

Am I missing something?

Copy link
Member

Choose a reason for hiding this comment

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

The autolinking on this repo is currently set to link to https://python.org/dev/peps/pep-<num>. My suggestion that simply matched pep-(\d+) works even with this type of URL, but fails with /^\d+$/.

@AA-Turner
Copy link
Member Author

redirect loop

The latest change matches ^\d+$ so there's no redirect anymore.

We could check the number but if invalid the only harm of not checking would be an extra redirect from one invalid URL to another.

A

@CAM-Gerlach
Copy link
Member

As mentioned on #2420 , you could do this all in HTML with no JS needed by just using<meta http-equiv="refresh" content="0; url={redirect_url}"> and auto-generating the redirect pages via a build script. We use such a script that I wrote in production for the Spyder Docs.

Instead of this approach, that could be used here as a post-build step in the CI/makefile (I would be willing to relicense it CC0), as we do, with only slight modifications (since almost everything is done via CLI arguments rather than hardcoded), and would avoid the need for JS as the redirects are statically generated on site build, like the rest of the site.

@AA-Turner
Copy link
Member Author

I don't know if the preview site has a part to play in this, but the http-equiv approach seems to be markedly slower than JS -- I see the redirect page linger on screen for ~0.5 seconds, versus not seeing it at all for the JS approach. If others could test I'd appreciate it.

A

@CAM-Gerlach
Copy link
Member

@AA-Turner Good catch; I can replicate the behavior you described. However, the redirects generated by my script I linked are essentially instant (≈100 ms; see, for example, this page) and nothing renders in the viewport except the final page, with no visible artifacts (and the host is GitHub Pages, just like the production site here).

I suspect the issue is due to your redirect pages including a <body> which gets rendered and displayed, whereas mine are just a head, and also omit the unnecessary shortcut icon and viewport elements, which require an additional HTTP resource fetch and and may also introduce a little more rendering delay, (plus as extra elements in the source and in the DOM, increase page size and parsing time slightly).

I suggest simply eliminating all those elements, which should result in identical behavior (at least in production) to the examples I shared.

Copy link
Member
@CAM-Gerlach CAM-Gerlach left a comment

Choose a reason for hiding this comment

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

Various fixes and nits. Otherwise, this looks good and is simpler to implement internally within the Sphinx extension than I thought it would be.



def prepare_redirect_pages(app: Sphinx) -> Iterator[tuple[str, dict, str]]:
for path in Path(app.srcdir).glob("pep-????.???"):
Copy link
Member
@CAM-Gerlach CAM-Gerlach Mar 13, 2022

Choose a reason for hiding this comment

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

Suggested change
for path in Path(app.srcdir).glob("pep-????.???"):
for path in Path(app.srcdir).glob("pep-[0-9][0-9][0-9][0-9].???"):

Use the correct glob to ensure this is actually a PEP with a valid number. Otherwise, it can match any arbitrary txt or rst file that starts with pep with a four-character suffix, which will cause an error on line 18 (we don't have any right now, but it will suddenly cause a build failure if we ever do, so we may as well just do this properly now.

Copy link
Member Author

Choose a reason for hiding this comment

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

if we ever do

This is I suppose my reluctance to put [0-9] rather than ? -- I seriously think this is very unlikely to happen, and [0-9] is five times as many characters as ?. (The thought applies in all cases that this has been suggested).

A

Copy link
Member

Choose a reason for hiding this comment

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

I hope you didn't stay up all night...or just up really early?

A few more characters perhaps, but in terms of what actually matters, does that make it that much less clear to the reader? And is it really worth sacrificing precision and correctness, and risking a potential future hard build failure just to skimp on a few characters? I realize there is some tradeoff to be made, but it seems the rest of us who have weighed in so far have considered it a worthwhile one.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done. I still disagree though!

A

Copy link
Member
@ezio-melotti ezio-melotti left a comment

Choose a reason for hiding this comment

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

I agree with CAM comments, and added a couple more.

@AA-Turner
Copy link
Member Author

It does seem quicker now, although there may be bias in that I want it to be quicker!

A

@AA-Turner AA-Turner marked this pull request as ready for review March 13, 2022 06:47
@hugovk
Copy link
Member
hugovk commented Mar 13, 2022

I'd like to hear about Nginx redirects #2420 (comment) before reviewing this. Thanks!

@CAM-Gerlach
Copy link
Member
CAM-Gerlach commented Mar 13, 2022

I'd like to hear about Nginx redirects #2420 (comment) before reviewing this. Thanks!

Unless I'm misunderstanding something, that approach only works for people who type in the old no longer canonical URL, i.e. www.python.org/dev/peps/pep-N; what is being proposed here works for the new PEP site without relying on the python.org infra, i.e. peps.python.org/pep-N or even peps.python.org/N, which is self-contained, makes the URL much shorter, avoids a redirect between different subdomains and infras that triggers an extra DNS query, and establishes a clearer justification for doing this now at the inception of the new site. Meanwhile the other downsides you mentioned,

Rather than running JavaScript client-side (not everyone has JS enabled, it's relatively slow, more code to maintain)

aren't so much of a factor here with the optimized HTML-based approach.

I certainly think the redirect rules could be adjusted to also do this, but that could supplement, not replace the approach in this PR, no?

@hugovk
Copy link
Member
hugovk commented Mar 13, 2022

Ah right, yes, Nginx redirects don't apply to this: #2420 (comment). Will review this, thanks!

Copy link
Member
@hugovk hugovk left a comment

Choose a reason for hiding this comment

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

@AA-Turner
Copy link
Member Author

Although none of those URLs are valid (the supported scheme is /\d+), it is very surprising behaviour.

https://peps.python.org/pep-8 redirects to the standard 404 page -- I wonder if the JS experiment's 404 has been cached or something?

A

@CAM-Gerlach
Copy link
Member

These all redirect to https://pep-previews--2421.org.readthedocs.build/pep-0404/

The intended redirect pattern that @AA-Turner implemented is /N -> /pep-NNNN. However, I can confirm in my testing that any link, not just those beginning with pep-, that does not correspond to a PEP number or full pep name goes to PEP 404.

The reason this happens is that this attempts to load /404, which lands on the redirect page to PEP 404 and thus redirects to that PEP—i.e., we've made the 404 page be a redirect to PEP 404, which is not what we want. We either need to return to the pep-N schema or figure out another way to avoid this happening (the simplest being to add a special case to not generate a redirect for PEP 404).

@AA-Turner
Copy link
Member Author

The reason this happens is that this attempts to load /404, which lands on the redirect page to PEP 404 and thus redirects to that PEP

The law of unintended consequences... I suppose we could do window.history introspection, but that brings back the drawbacks of JavaScript.

A

@hugovk
Copy link
Member
hugovk commented Mar 13, 2022

@CAM-Gerlach
Copy link
Member

We looked into it, but I don't think 0.0.1 was released yet when we implemented it, and we weren't sure if the wildcard support would fully handle our current and future use cases, so we ended up implementing our own solution. For this case, that doesn't look like that would be a huge issue, but it doesn't support the sort of rewriting we need here, so unfortunately it doesn't look like it'd work (at least at present).

@AA-Turner
Copy link
Member Author

I won't have time to resolve this before mid April -- feel free to push to this branch on my repo or close this PR as you see fit. My views are strongly in favour of the simple /N prefix -- if we can sort out the redirects to PEP 404 with JS history introspection, I think that's fine -- the JS experiment remains in this branch if it's to be resurrected.

A

@CAM-Gerlach
Copy link
Member

if we can sort out the redirects to PEP 404 with JS history introspection, I think that's fine -- the JS experiment remains in this branch if it's to be resurrected.

I really would like to not rely on such hacks, and would prefer not to go with the JS approach if possible for the reasons @hugovk mentioned, so I'd strongly lean toward the two less complicated and costly alternatives here—either reverting to the /pep-N scheme, which did already have some support by others, or just excluding PEP 404 from the redirect generation.

@CAM-Gerlach CAM-Gerlach changed the title PRS: Support redirects Support redirects from non-padded PEP numbers Mar 14, 2022
@hugovk
Copy link
Member
hugovk commented Apr 15, 2022

Closing, this has been implemented in #2420. Thanks!

@hugovk hugovk closed this Apr 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Auto-fill missing leading 0s in URL
5 participants
0