fix: closes #13625, fix utils.params so it works with relative_paths

This commit is contained in:
Barış Soner Uşaklı
2025-08-29 12:50:06 -04:00
parent 35641f377c
commit a0e78ff853
4 changed files with 45 additions and 4 deletions

View File

@@ -56,7 +56,10 @@ define('forum/login', ['hooks', 'translator', 'jquery-form'], function (hooks, t
success: function (data) {
hooks.fire('action:app.loggedIn', data);
const pathname = utils.urlToLocation(data.next).pathname;
const params = utils.params({ url: data.next });
const params = utils.params({
url: data.next,
relative_path: config.relative_path,
});
params.loggedin = true;
delete params.register; // clear register message incase it exists
const qs = $.param(params);

View File

@@ -83,7 +83,10 @@ define('forum/register', [
if (data.next) {
const pathname = utils.urlToLocation(data.next).pathname;
const params = utils.params({ url: data.next });
const params = utils.params({
url: data.next,
relative_path: config.relative_path,
});
params.registered = true;
const qs = $.param(params);

View File

@@ -571,9 +571,11 @@ const utils = {
let url;
if (options.url && !options.url.startsWith('http')) {
// relative path passed in
options.url = options.url.replace(new RegExp(`/?${config.relative_path.slice(1)}/`, 'g'), '');
const cleanurl = options.url.replace(new RegExp(`/^${(options.relative_path || '')}/`, 'g'), '');
url = new URL(document.location);
url.pathname = options.url;
const queryIndex = cleanurl.indexOf('?');
url.search = queryIndex !== -1 ? cleanurl.slice(queryIndex) : '';
url.pathname = cleanurl;
} else {
url = new URL(options.url || document.location);
}

View File

@@ -320,6 +320,39 @@ describe('Utility Methods', () => {
done();
});
it('should get url params for relative url', (done) => {
const params = utils.params({
url: '/page?foo=1&bar=test&herp=2',
relative_path: '',
});
assert.strictEqual(params.foo, 1);
assert.strictEqual(params.bar, 'test');
assert.strictEqual(params.herp, 2);
done();
});
it('should get url params for relative url', (done) => {
const params = utils.params({
url: '/page?foo=1&bar=test&herp=2',
relative_path: '/forum',
});
assert.strictEqual(params.foo, 1);
assert.strictEqual(params.bar, 'test');
assert.strictEqual(params.herp, 2);
done();
});
it('should get url params for relative url', (done) => {
const params = utils.params({
url: '/forum/page?foo=1&bar=test&herp=2',
relative_path: '/forum',
});
assert.strictEqual(params.foo, 1);
assert.strictEqual(params.bar, 'test');
assert.strictEqual(params.herp, 2);
done();
});
it('should get url params as arrays', (done) => {
const params = utils.params({ url: 'http://nodebb.org?foo=1&bar=test&herp[]=2&herp[]=3' });
assert.strictEqual(params.foo, 1);