From 6f7819366042c210c00aa46a867677a0d8519d55 Mon Sep 17 00:00:00 2001 From: Dmitry Lyzo Date: Sat, 26 Sep 2020 12:27:14 +0300 Subject: [PATCH 1/2] Fix 'file:' fetching (bundled apps) --- src/scripts/settings/webSettings.js | 35 +++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/scripts/settings/webSettings.js b/src/scripts/settings/webSettings.js index 2ffe290d88..e28e0e84ff 100644 --- a/src/scripts/settings/webSettings.js +++ b/src/scripts/settings/webSettings.js @@ -1,9 +1,40 @@ let data; +// `fetch` with `file:` support +// Recent browsers seem to support `file` protocol under some conditions. +// Based on https://github.com/github/fetch/pull/92#issuecomment-174730593 +async function fetchLocal(url, options) { + return new Promise((resolve, reject) => { + const xhr = new XMLHttpRequest; + + xhr.onload = () => { + // `file` protocol has invalid OK status + let status = xhr.status; + if (xhr.responseURL.startsWith('file:') && status === 0) { + status = 200; + } + + resolve(new Response(xhr.responseText, {status: status})); + } + + xhr.onerror = () => { + reject(new TypeError('Local request failed')); + } + + xhr.open('GET', url); + + if (options && options.cache) { + xhr.setRequestHeader('Cache-Control', options.cache); + } + + xhr.send(null); + }); +} + async function getConfig() { if (data) return Promise.resolve(data); try { - const response = await fetch('config.json', { + const response = await fetchLocal('config.json', { cache: 'no-cache' }); @@ -22,7 +53,7 @@ async function getConfig() { async function getDefaultConfig() { try { - const response = await fetch('config.template.json', { + const response = await fetchLocal('config.template.json', { cache: 'no-cache' }); From 56d71798381e036a243118815194be9e963b9fda Mon Sep 17 00:00:00 2001 From: Dmitry Lyzo Date: Sun, 27 Sep 2020 17:48:29 +0300 Subject: [PATCH 2/2] Fix webOS 1.2 and lint --- src/scripts/settings/webSettings.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/scripts/settings/webSettings.js b/src/scripts/settings/webSettings.js index e28e0e84ff..80587713bf 100644 --- a/src/scripts/settings/webSettings.js +++ b/src/scripts/settings/webSettings.js @@ -1,25 +1,33 @@ let data; +const urlResolver = document.createElement('a'); + // `fetch` with `file:` support // Recent browsers seem to support `file` protocol under some conditions. // Based on https://github.com/github/fetch/pull/92#issuecomment-174730593 +// https://github.com/github/fetch/pull/92#issuecomment-512187452 async function fetchLocal(url, options) { + urlResolver.href = url; + + const requestURL = urlResolver.href; + return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest; xhr.onload = () => { // `file` protocol has invalid OK status let status = xhr.status; - if (xhr.responseURL.startsWith('file:') && status === 0) { + if (requestURL.startsWith('file:') && status === 0) { status = 200; } + /* eslint-disable-next-line compat/compat */ resolve(new Response(xhr.responseText, {status: status})); - } + }; xhr.onerror = () => { reject(new TypeError('Local request failed')); - } + }; xhr.open('GET', url);