From 7a2109f1c027c3faa7e7bb42f6606a5e9dd4d668 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Mon, 29 Jan 2024 12:51:44 +0100 Subject: [PATCH] fix(getRootUrl)!: If not configured use first subdirectory as webroot instead of last Signed-off-by: Ferdinand Thiessen --- __tests__/webroot.spec.ts | 19 ++++++++++++++++--- lib/index.ts | 4 +++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/__tests__/webroot.spec.ts b/__tests__/webroot.spec.ts index 36ca24b..8333d8e 100644 --- a/__tests__/webroot.spec.ts +++ b/__tests__/webroot.spec.ts @@ -57,12 +57,25 @@ describe('Web root handling', () => { expect(getBaseUrl()).toBe(`${window.location.origin}/nextcloud`) }) - // TODO: This seems to be wrong, would expect `/nextcloud` + test('with implicit empty web root', () => { + window._oc_webroot = undefined + window.location.pathname = '/' + expect(getRootUrl()).toBe('/') + expect(getBaseUrl()).toBe(`${window.location.origin}/`) + }) + test('with implicit web root and path rename', () => { + window._oc_webroot = undefined + window.location.pathname = '/nextcloud' + expect(getRootUrl()).toBe('/nextcloud') + expect(getBaseUrl()).toBe(`${window.location.origin}/nextcloud`) + }) + + test('with implicit web root on route with path rename', () => { window._oc_webroot = undefined window.location.pathname = '/nextcloud/apps/files' - expect(getRootUrl()).toBe('/nextcloud/apps') - expect(getBaseUrl()).toBe(`${window.location.origin}/nextcloud/apps`) + expect(getRootUrl()).toBe('/nextcloud') + expect(getBaseUrl()).toBe(`${window.location.origin}/nextcloud`) }) }) diff --git a/lib/index.ts b/lib/index.ts index 969fd21..c6e3280 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -227,7 +227,9 @@ export function getRootUrl(): string { if (pos !== -1) { webroot = webroot.slice(0, pos) } else { - webroot = webroot.slice(0, webroot.lastIndexOf('/')) + const index = webroot.indexOf('/', 1) + // Make sure to not cut end of path if there is just the webroot like `/nextcloud` + webroot = webroot.slice(0, index > 0 ? index : undefined) } } return webroot