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

Add support for relative protocols in url filter #1276

Merged
merged 2 commits into from
Jul 1, 2021
Merged

Add support for relative protocols in url filter #1276

merged 2 commits into from
Jul 1, 2021

Conversation

oscarotero
Copy link
Contributor

This PR fixes #1247

Currently, the url filter normalize values starting with // as if they where paths, so for example a url to //example.com is changed to /example.com (or pathPrefix/example.com if there's a path prefix configured).

But technically, //example.com is a perfectly valid url that should be resolved to http://example.com or https://example.com, depending of the protocol of the current document location.

This PR fixes this problem. Note that this is a breaking change.

@MadeByMike
Copy link
Contributor

This looks good to me. Although it's also potentially a breaking change for some people. I think we'd need to document and communicate this change thoroughly.

@zachleat this is slightly off topic but it would be super useful if the website documentation was in the same repo as eleventy (possibility even a monorepo (...I know, I know, but you can do it with a light touch, it's not all bad!))

I also think using changeset could be a good addition to managing things: https://github.com/atlassian/changesets.

Let me know if you'd be open to PRs for either of these.

// work with undefined
url = url || "";

if (
validUrl.isUri(url) ||
url.indexOf("http://") === 0 ||
url.indexOf("https://") === 0
url.indexOf("https://") === 0 ||
(url.indexOf("//") === 0 && url !== "//")
Copy link
Contributor

Choose a reason for hiding this comment

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

This could also be written as (url.startsWith("//") && url !== "//")
Actually all of them in the conditional can be replaced with startsWith() as there is no way the url to contain the protocol in any other position except the... start of the string

@nhoizey
Copy link
Contributor

nhoizey commented Jul 15, 2020

I think it should be mentioned that protocol relative URLs can be harmful, and are now considered an anti-pattern:
https://www.paulirish.com/2010/the-protocol-relative-url/

@zachleat zachleat added the breaking-change This will have to be included with a major version as it breaks backwards compatibility. label Mar 18, 2021
@zachleat zachleat merged commit b047fc7 into 11ty:master Jul 1, 2021
@zachleat zachleat added this to the Eleventy 1.0.0 milestone Jul 1, 2021
@zachleat
Copy link
Member

zachleat commented Jul 1, 2021

This will ship with 1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change This will have to be included with a major version as it breaks backwards compatibility. pr: enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

url filter should detect absolute urls without the protocol
5 participants