From a0e78ff853c37b89862d29b5dddd1dd23e8b0176 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Fri, 29 Aug 2025 12:50:06 -0400 Subject: [PATCH] fix: closes #13625, fix utils.params so it works with relative_paths --- public/src/client/login.js | 5 ++++- public/src/client/register.js | 5 ++++- public/src/utils.common.js | 6 ++++-- test/utils.js | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/public/src/client/login.js b/public/src/client/login.js index f7508b8ec2..e2495fb07f 100644 --- a/public/src/client/login.js +++ b/public/src/client/login.js @@ -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); diff --git a/public/src/client/register.js b/public/src/client/register.js index f989901e7b..ca86be35f0 100644 --- a/public/src/client/register.js +++ b/public/src/client/register.js @@ -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); diff --git a/public/src/utils.common.js b/public/src/utils.common.js index 0014b3ae85..47520b2425 100644 --- a/public/src/utils.common.js +++ b/public/src/utils.common.js @@ -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); } diff --git a/test/utils.js b/test/utils.js index 3e6338934c..e9ccbd4108 100644 --- a/test/utils.js +++ b/test/utils.js @@ -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);