-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Prevent empty query parameter being set on dashboard #11561
Prevent empty query parameter being set on dashboard #11561
Conversation
Signed-off-by: Andrew Thornton <[email protected]>
if (queryString) { | ||
window.history.replaceState({}, '', `?${queryString}`); | ||
} else { | ||
window.history.replaceState({}, '', window.location.pathname); |
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.
Is this branch even necessary? This would kill any #hash
present on the URL (thought the branch above would also)
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.
Here's a helper function you can put in utils.js
that preserves pathname, search and hash:
function setUrl(opts) {
const url = new URL(window.location.href);
if ('pathname' in opts) url.pathname = opts.pathname || '/';
if ('search' in opts) url.search = opts.search || '';
if ('hash' in opts) url.hash = opts.hash || '';
const newLocation = String(url).replace(url.origin, '');
window.history[opts.push ? 'pushState' : 'replaceState'](null, null, newLocation);
}
You can then call it like this to set/unset only the search
part:
setUrl({search: params.toString() || ''});
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.
The hash is will be out of date and irrelevant if you are pressing these buttons - if anything we should be setting the hash to reference the repo-list table.
Imagine we finally get round to sticking ids on the newsfeed items and you have been sent the newsfeed via an anchor then have scrolled back up to the repo-list - keeping the anchor would mean that on going back you will scroll back to the anchor even though the actual proper context for those query elements is the repo-list.
As the repo-list is the top of the page it's not necessarily needed to set the anchor hash - but - keeping it could be confusing.
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.
You can remove the hash too with it. This should be equal to your version:
setUrl({search: params.toString() || '', hash: ''});
Your first case would set a wrong path if gitea is running on a subdirectory as it would set path to parent root directory. I think my function is safer because it only sets the parts that you want to change, not all at once.
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.
Actually, it seems a replaceState
with ?something
as URL does not kill the path, at least in Firefox. But I would probably not want rely that this behaviour is the same in all browsers.
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.
So I think your way is actually fine. I did not realize browsers were smart enough to do the right thing for ?search
and #hash
pseudo urls, but it appears to be working fine because the url standard requires to apply the base url so fragments like this will work.
> new URL('?search', "http://www.example.com/dir/?search").href
'http://www.example.com/dir/?search'
> new URL((new URL("http://www.example.com/dir/")).pathname, "http://www.example.com/dir/?search").href
'http://www.example.com/dir/'
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.
hehe! one of the things that I've recently realised is that it is helpful to run your testing instance through a proxy mounted on a suburl (http://localhost/gitea) as it catches a lot of simple problems.
make lg-tm work |
Prevent the dashboard from setting an empty query parameter Fix go-gitea#11543 Signed-off-by: Andrew Thornton [email protected]
Prevent the dashboard from setting an empty query parameter Fix #11543 Signed-off-by: Andrew Thornton [email protected]
Prevent the dashboard from setting an empty query parameter Fix go-gitea#11543 Signed-off-by: Andrew Thornton [email protected]
Prevent the dashboard from setting an empty query parameter
Fix #11543
Signed-off-by: Andrew Thornton [email protected]