Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions core/src/utils/xhr-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

import { getRootUrl } from '@nextcloud/router'

/**
* @param {string} url The URL to check
* @return {boolean} true if the URL points to this nextcloud instance
*/
const isNextcloudUrl = (url) => {
const nextcloudBaseUrl = window.location.protocol + '//' + window.location.host + getRootUrl()
// try with relative and absolute URL
Expand All @@ -43,24 +47,25 @@ export const interceptRequests = () => {
})(XMLHttpRequest.prototype.open)

window.fetch = (function(fetch) {
return (input, init) => {
if (!isNextcloudUrl(input.url)) {
return fetch(input, init)
return (resource, options) => {
// fetch allows the `input` to be either a Request object or any stringifyable value
if (!isNextcloudUrl(resource.url ?? resource.toString())) {
return fetch(resource, options)
}
if (!init) {
init = {}
if (!options) {
options = {}
}
if (!init.headers) {
init.headers = new Headers()
if (!options.headers) {
options.headers = new Headers()
}

if (init.headers instanceof Headers && !init.headers.has('X-Requested-With')) {
init.headers.append('X-Requested-With', 'XMLHttpRequest')
} else if (init.headers instanceof Object && !init.headers['X-Requested-With']) {
init.headers['X-Requested-With'] = 'XMLHttpRequest'
if (options.headers instanceof Headers && !options.headers.has('X-Requested-With')) {
options.headers.append('X-Requested-With', 'XMLHttpRequest')
} else if (options.headers instanceof Object && !options.headers['X-Requested-With']) {
options.headers['X-Requested-With'] = 'XMLHttpRequest'
}

return fetch(input, init)
return fetch(resource, options)
}
})(window.fetch)
}
4 changes: 2 additions & 2 deletions dist/core-main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/core-main.js.map

Large diffs are not rendered by default.