Skip to content

Commit

Permalink
Merge pull request #570 from nextcloud-libraries/fix/webroot
Browse files Browse the repository at this point in the history
fix(getRootUrl)!: If not configured use first subdirectory as webroot instead of last
  • Loading branch information
susnux committed Jan 31, 2024
2 parents 47c71c1 + 7a2109f commit 49c9775
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
19 changes: 16 additions & 3 deletions __tests__/webroot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
})
})

Expand Down
4 changes: 3 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,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
Expand Down

0 comments on commit 49c9775

Please sign in to comment.