-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
#3391 - Match trailing slash on root to how root was configured. #4240
#3391 - Match trailing slash on root to how root was configured. #4240
Conversation
Is there anything I can do to help move this forward? I believe this change maintains backwards compatibility and includes a full set of tests, so it should be a low-risk include. |
@kdickerson Sorry for the long silence. I have recently adopted Backbone maintainership (see #4244) and will give your PR high priority. |
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.
As-is, this is a breaking change, which I could only include in a major release. However, I see a way in which the desired behavior can be enabled as an opt-in, without breaking changes. It amounts to adding a new option keepSlash
to the history.start
method, which will toggle the same behavior that is toggled using the private _wantsRootSlash
in the current proposal.
Everyone interested in this problem, please let me know your thoughts! @kdickerson @qodesmith @rdquintas @bartram @ellatrix @jaidetree
@jgonggrijp It's been many years since I've used Backbone so I don't think my input is relevant here. |
That would be fine by me. I was trying to avoid a new configuration option and make it do what the developer expects based on how the developer passed the |
Thanks @kdickerson. Would you like to adjust the branch yourself or shall I do it? As far as I'm concerned, this can stay within the same PR. |
I can do it. I'll see if I can get it done today. |
6d057b9
to
53994d0
Compare
Pushed an update after rebasing onto master. Maintains existing behavior unless |
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.
Awesome, thanks!
Could you remove all changes to existing tests? And then try whether you can get rid of _wantsRootSlash
entirely? I think it is redundant with _keepRootSlash
.
The The problem, specifically, is that the legacy behavior is not "always remove a trailing slash." It sometimes removes it and sometimes adds it (if So it seemed like a better option to make the new mode mean "respect the developer's decision about the trailing slash." I changed the name of two of the existing tests to improve the naming since several tests had the same name and tested different things. Including one of which tested the exact opposite of the name (name said "No trailing slash on root" and then tests that it has a trailing slash). I believe the only functionality I changed in an existing test was to add a trailing slash to |
I disagree that the third case should be accounted for. The logic in
No, this is a misunderstanding. The slash is added back if the
What, according to you, is "the right thing" when the root is an empty string?
Right, this is what I expected you to implement.
Why do you want a "never use a trailing slash" mode?
If that is the intent, I agree that
This is also a misunderstanding. In those two tests, the user has not set a trailing slash on root (in the first, by not setting a root at all, in the second by setting it to
I could see that you only added assertions to the end of that test, but that still changes the meaning of the test.
Yes, please do. Please, never change existing tests, unless you can prove (in the strongest sense of that word) that they are wrong.
Maybe, but let us focus on getting the assumptions right first. |
Ah, I had missed that. I dislike code which force changes input to something else. In this case it resulted in #2656 due to users expecting the code to respect their configured value (and have no trailing slash). And the response to #2656 to always removing trailing slashes led to #3391 because other users also expected the code to respect their configured values (and have a trailing slash). Too bad the original response to #2656 wasn't to respect the configured value rather than to switch the behavior from slash to no slash. I realized my current version was colored by my original implementation which attempted to maintain as much backwards compatibility as possible using only the value provided for Knowing that I'll get these changes pushed out later today. |
I wouldn't say that That said, I agree that there have been many unfortunate changes in the past (see also #4266). In the case of
Do you mean the existing tests or your own tests? In the first case, the tests likely originated in a period when the implementation didn't have all the subtleties that it has today. They should probably still be preserved in order to prevent regressions, but if you tell me which tests you think could be removed, I'll consider it.
Great, thanks! |
53994d0
to
9d6165d
Compare
Can help but wonder if I saw the normalization of the slashes when I wrote the original commit since this is basically the same as that. I chose the option name So I think the semantics of |
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.
Can help but wonder if I saw the normalization of the slashes when I wrote the original commit since this is basically the same as that.
Very well might! This is also the reason I suggested that you replaced the "autodetected" _wantsRootSlash
by an opt-in _keepSlash
/_trailingSlash
; I thought you could leave it at that.
I chose the option name
trailingSlash
since I feltkeepSlash
was misleading unless one knows the value forroot
will be normalized (which I don't see in the documentation, and should probably be added).keepSlash
sounds like if there's a slash it will stay, but doesn't, to me, suggest that a slash will always be used.
Fine by me.
I'm mostly happy with the changes so far, but I would like to request that you also update the documentation of history.start
in the index.html
. Please feel welcome to elaborate a bit on leading and trailing slashes, I think you are right that this has been too implicit until now.
backbone.js
Outdated
if (fragment === '' || fragment.charAt(0) === '?') { | ||
rootPath = rootPath.slice(0, -1) || '/'; | ||
if (!this._trailingSlash && (fragment === '' || fragment.charAt(0) === '?')) { | ||
rootPath = rootPath.slice(0, -1) || '/'; // rootPath must always have at least '/' |
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.
If you feel this comment is necessary, please put it on a separate line above. I think it doesn't add much here, so you can also leave it out completely.
FYI, I didn't add any documentation because
I'll update the documentation. |
Good point! That remark refers to the annotated source code, but I totally agree it makes a different impression. I'll create an issue for it, now that you remind me of it. |
…ng slash on root.
9d6165d
to
425b931
Compare
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.
Thanks!
<tt>Backbone.history.start({pushState: true, root: "/public/search/"})</tt> | ||
<tt>Backbone.history.start({pushState: true, root: "/public/search/"})</tt>. | ||
</p> | ||
|
||
<p> | ||
The value provided for <tt>root</tt> will be normalized to include a leading | ||
and trailing slash. When navigating to a route the default behavior is to | ||
exclude the trailing slash from the URL (e.g., <tt>/public/search?query=...</tt>). | ||
If you prefer to include the trailing slash (e.g., <tt>/public/search/?query=...</tt>) | ||
use <tt>Backbone.history.start({trailingSlash: true})</tt>. | ||
URLs will always contain a leading slash. When root is <tt>/</tt> URLs will | ||
look like <tt>/?query=...</tt> regardless of the value of <tt>trailingSlash</tt>. |
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.
Excellent. 👍
Fixes #3391 - Allow developers to control whether a trailing-slash should be included or not.