Skip to content
This repository was archived by the owner on Jan 26, 2024. It is now read-only.

Commit d3004fb

Browse files
committed
Fix for the offline mode after #1687
1 parent cddc7f6 commit d3004fb

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

core/i18n/resource/i18n/en-US.csv

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,4 +354,5 @@
354354
"Allow notification about the order","Allow notification about the order"
355355
"Extension developers would like to thank you for placing an order!","Extension developers would like to thank you for placing an order!"
356356
"most you may purchase","most you may purchase"
357-
"have as many","have as many"
357+
"have as many","have as many"
358+
"Compare products","Compare products"

core/store/lib/search.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ function search (elasticQuery) {
5858
},
5959
body: JSON.stringify(elasticQuery.body)
6060
}).then(resp => { return resp.json() }).catch(err => {
61-
throw new Error(err)
61+
if (err.message.indexOf('fetch') >= 0 || !isOnline()) {
62+
console.error('Offline mode ' + err.message)
63+
} else {
64+
throw new Error(err)
65+
}
6266
})
6367
}
6468
/**
@@ -69,7 +73,11 @@ function search (elasticQuery) {
6973
*/
7074
function _handleEsResult (resp, start = 0, size = 50): ESResponse {
7175
if (resp == null) {
72-
throw new Error('Invalid ES result - null not exepcted')
76+
if (isOnline()) {
77+
throw new Error('Invalid ES result - null not exepcted')
78+
} else {
79+
return null // empty result but not exception raised - we're in offline mode
80+
}
7381
}
7482
if (resp.hasOwnProperty('hits')) {
7583
return {
@@ -166,13 +174,16 @@ export function quickSearchByQuery ({ query, start = 0, size = 50, entityType =
166174

167175
search(esQuery).then((resp) => { // we're always trying to populate cache - when online
168176
const res = _handleEsResult(resp, start, size)
169-
cache.setItem(cacheKey, res).catch((err) => { console.error('Cannot store cache for ' + cacheKey + ', ' + err) })
170-
if (!servedFromCache) { // if navigator onLine == false means ES is unreachable and probably this will return false; sometimes returned false faster than indexedDb cache returns result ...
171-
console.debug('Result from ES for ' + cacheKey + ' (' + entityType + '), ms=' + (new Date().getTime() - benchmarkTime.getTime()))
172-
res.cache = false
173-
res.noresults = false
174-
res.offline = false
175-
resolve(res)
177+
178+
if (res) { // otherwise it can be just a offline mode
179+
cache.setItem(cacheKey, res).catch((err) => { console.error('Cannot store cache for ' + cacheKey + ', ' + err) })
180+
if (!servedFromCache) { // if navigator onLine == false means ES is unreachable and probably this will return false; sometimes returned false faster than indexedDb cache returns result ...
181+
console.debug('Result from ES for ' + cacheKey + ' (' + entityType + '), ms=' + (new Date().getTime() - benchmarkTime.getTime()))
182+
res.cache = false
183+
res.noresults = false
184+
res.offline = false
185+
resolve(res)
186+
}
176187
}
177188
}).catch(err => {
178189
reject(err)

0 commit comments

Comments
 (0)