-
Notifications
You must be signed in to change notification settings - Fork 11
/
gatsby-browser.js
46 lines (44 loc) · 1.43 KB
/
gatsby-browser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// set flexsearch object as a global variable to make it available to language files
global.FlexSearch = require('flexsearch')
exports.onClientEntry = function(args, _ref) {
// load json data into window variable
fetch(`${__PATH_PREFIX__}/flexsearch_index.json`)
.then(function(response) {
return response.json()
})
.then(function(index) {
Object.keys(index).forEach(lng => {
Object.keys(index[lng].index).forEach(idx => {
const index_ = index[lng].index[idx]
// load language files if needed by stemmer or filter
if (
index_.attrs.stemmer !== undefined ||
index_.attrs.filter !== undefined
) {
try {
if (lng === 'en') {
require('./lang/en')
} else if (lng === 'de') {
require('./lang/en')
} else {
console.error(
'Language not supported by pre-defined stemmer or filter'
)
}
} catch (e) {
console.error(e)
}
}
// rebuild the index
const indexObj = new FlexSearch(index_.attrs)
indexObj.import(index_.values)
index_.values = indexObj
})
})
// load index into window variable
window.__FLEXSEARCH__ = index
})
.catch(function(e) {
console.error('Failed fetch search index')
})
}