-
-
Notifications
You must be signed in to change notification settings - Fork 59
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
fix(full_url_for): handle config.url with a trailing slash #217
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
user may prefer to use
url: https://opentechschool.github.io/social-coding/
.
That's not a case. We already documented it clearly: https://hexo.io/docs/configuration
Also, Hexo already handle it in load_config.js
as a failsafe:
config.root = config.root.replace(/\/*$/, '/');
config.url = config.url.replace(/\/+$/, '');
Yes, and there is a hint in the
Which means
should be considered as invalid config. See also hexojs/hexo#1812 (comment) |
that failsafe is not available in Hexo API (e.g. when use in a plugin's unit test). It is also not ideal in use cases such as rss and sitemap. When a trailing slash (e.g. |
Also, current behavior of hexo-util/test/full_url_for.spec.js Lines 13 to 15 in 14fa817
Last but not least, all browsers, http request libs as well as crawlers will treat GET https://blog.skk.moe
> HTTP/1.1 200 OK GET https://blog.skk.moe.
> HTTP/1.1 200 OK GET https://blog.skk.moe/
> HTTP/1.1 200 OK |
I was referring to ## If your site is put in a subdirectory, set url as 'http://yoursite.com/child' and root as '/child/'
url: http://yoursite.com
root: / My understanding of the rational behind In GH Pages, since Hexo blog can be hosted anywhere, we can't know whether |
Fixed invalid protocol (
const hexo = {
config: {
url: 'http://example.com'
}
}
console.log(full_url_for.call(hexo, '/'))
// http://example.com
hexo.config.url = 'http://example.com/'
console.log(full_url_for.call(hexo, '/'))
// http://example.com/
hexo.config.url = 'http://example.com/blog'
console.log(full_url_for.call(hexo, '/'))
// http://example.com/blog
hexo.config.url = 'http://example.com/blog/'
console.log(full_url_for.call(hexo, '/'))
// http://example.com/blog/
const hexo = {
config: {
url: 'http://example.com'
}
}
console.log(full_url_for.call(hexo, 'index.html'))
// http://example.com/
hexo.config.url = 'http://example.com/'
console.log(full_url_for.call(hexo, 'index.html'))
// http://example.com/
hexo.config.url = 'http://example.com/blog'
console.log(full_url_for.call(hexo, 'index.html'))
// http://example.com/blog/
hexo.config.url = 'http://example.com/blog/'
console.log(full_url_for.call(hexo, 'index.html'))
// http://example.com/blog/ |
}); | ||
|
||
it('internal url - subdirectory with trailing slash', () => { | ||
ctx.config.url = 'https://example.com/blog/'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just as the test case of load_config
shows, the url: https://hexo.io/
will result in hexo.config.url.should.eql('https://hexo.io');
, while root: foo
will result in hexo.config.root.should.eql('foo/');
. Thus, the test case just here just won't exist.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/hexojs/hexo/blob/17ee23fa181bcc2c3b47eba5b345762aef1eac36/lib/hexo/load_config.js#L34-L35 should be removed if this PR is accepted. Those two lines are considered a workaround and this PR already handles it.
In fact, I am confused about these two configurations.
If such a rule must meet, I think that only the configuration of Or is there a situation that does not meet this rule?
|
This PR makes it flexible to any combination of the following configs: url: http://yoursite.com/child
root: /child
url: http://yoursite.com/child/
root: /child
url: http://yoursite.com/child
root: /child/
url: http://yoursite.com/child/
root: /child/
url: http://yoursite.com/child/
root: child/
url: http://yoursite.com/child/
root: child
url: http://yoursite.com/child
root: child/
url: http://yoursite.com/child
root: child
You are right, it's trivial to derive I wonder if there is any use case for: url: http://yoursite.com/
root: /child/ |
I'm not sure if this PR is being too lenient on invalid config. |
Hexo currently is already pretty lenient about it. This PR may break The purpose of this PR is reducing 301/302 redirects for anyone using The main goal is to make the config slightly more user-friendly. |
@jiangtj @curbengh @stevenjoezhang Please consider the Breaking Change we introduced and the backward compatibility of current themes. If theme is designed to work with the current configuration format, then we shouldn't change the |
@curbengh If there is no special case, I prefer to remove the
Like hexojs/hexo#4414, handle
@SukkaW If just make |
To clarify, this PR doesn't directly cause a BC; the flexibility I mentioned earlier is more of a side effect, not a primary intention of this PR, though I think it's a benefit. It is this flexibility that could be a BC. However, I reckon all users with With this PR, Hexo doesn't need (and shouldn't) enforce the syntax, so user can then use: url: http://yoursite.com/child/
root: child But that will break
The intention is to keep the trailing slash |
I mean when the user configures So that |
a notable example is Github Pages with project page, e.g. https://github.com/OpenTechSchool/social-coding corresponds to https://opentechschool.github.io/social-coding/. Project page always have a trailing slash, accessing
https://opentechschool.github.io/social-coding
will 301-redirect tohttps://opentechschool.github.io/social-coding/
. So, in this case, user may prefer to useurl: https://opentechschool.github.io/social-coding/
.This PR adds support for that.