Skip to content

Commit 373244b

Browse files
Merge pull request #14185 from nextcloud/backport/14175/stable31
[stable31] refactor: remove `IS_DESKTOP` flag check on `generateAbsoluteUrl`
2 parents 4786b37 + d194a4c commit 373244b

File tree

2 files changed

+14
-22
lines changed

2 files changed

+14
-22
lines changed

src/utils/__tests__/handleUrl.spec.js

+6-11
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,14 @@ jest.mock('@nextcloud/dialogs', () => ({
1717

1818
describe('handleUrl', () => {
1919
describe('generateAbsoluteUrl', () => {
20-
it('should generate url with IS_DESKTOP=false correctly', () => {
21-
const output = generateAbsoluteUrl('/path')
22-
expect(output).toBe('http://localhost/nc-webroot/path')
20+
it('should generate absolute url', () => {
21+
const output = generateAbsoluteUrl('/path/{foo}', { foo: 'bar' })
22+
expect(output).toBe('http://localhost/nc-webroot/path/bar')
2323
})
2424

25-
it('should generate url with IS_DESKTOP=true correctly', () => {
26-
const originalIsDesktop = global.IS_DESKTOP
27-
global.IS_DESKTOP = true
28-
29-
const output = generateAbsoluteUrl('/path')
30-
expect(output).toBe('/nc-webroot/path')
31-
32-
global.IS_DESKTOP = originalIsDesktop
25+
it('should generate absolute url with specified base', () => {
26+
const output = generateAbsoluteUrl('/path/{foo}', { foo: 'bar' }, { baseURL: 'https://external.ltd/root' })
27+
expect(output).toBe('https://external.ltd/root/path/bar')
3328
})
3429
})
3530

src/utils/handleUrl.ts

+8-11
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,22 @@
55

66
import { showError, showSuccess } from '@nextcloud/dialogs'
77
import { t } from '@nextcloud/l10n'
8-
import { generateUrl } from '@nextcloud/router'
8+
import { generateUrl, getBaseUrl } from '@nextcloud/router'
99
import type { UrlOptions } from '@nextcloud/router'
1010

1111
/**
1212
* Generate a full absolute link with @nextcloud/router.generateUrl
1313
*
1414
* @param url - path
15-
* @param [params] parameters to be replaced into the address
16-
* @param [options] options for the parameter replacement
15+
* @param params - parameters to be replaced into the address
16+
* @param options - options for the parameter replacement
1717
*/
18-
export function generateAbsoluteUrl(url: string, params?: object, options?: UrlOptions): string {
18+
export function generateAbsoluteUrl(url: string, params?: object, options: UrlOptions = {}): string {
1919
// TODO: add this function to @nextcloud/router?
20-
const fullPath = generateUrl(url, params, options)
21-
if (!IS_DESKTOP) {
22-
return `${window.location.protocol}//${window.location.host}${fullPath}`
23-
} else {
24-
// On the Desktop generateUrl creates absolute url by default
25-
return fullPath
26-
}
20+
return generateUrl(url, params, {
21+
baseURL: getBaseUrl(),
22+
...options,
23+
})
2724
}
2825

2926
/**

0 commit comments

Comments
 (0)