Skip to content

Commit b1e9823

Browse files
Allow rejection of attempted load via returning falsy loadPath (#88)
* Allow rejection of attempted load via returning falsy loadPath * callback with empty object {} if resolvedLoadPath is falsy
1 parent d6f745f commit b1e9823

3 files changed

Lines changed: 36 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ for plain browser:
7070
// function(lngs, namespaces) { return customPath; }
7171
// the returned path will interpolate lng, ns if provided like giving a static path
7272
// the function might return a promise
73+
// returning falsy will abort the download
7374
//
7475
// If allowMultiLoading is false, lngs and namespaces will have only one element each,
7576
// If allowMultiLoading is true, lngs and namespaces can have multiple elements

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class Backend {
5959
loadPath = makePromise(loadPath)
6060

6161
loadPath.then(resolvedLoadPath => {
62+
if (!resolvedLoadPath) return callback(null, {})
6263
const url = this.services.interpolator.interpolate(resolvedLoadPath, { lng: languages.join('+'), ns: namespaces.join('+') })
6364
this.loadUrl(url, callback, loadUrlLanguages, loadUrlNamespaces)
6465
})

test/http.spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,40 @@ describe(`http backend using ${hasXMLHttpRequest ? 'XMLHttpRequest' : 'fetch'}`,
165165
})
166166
})
167167

168+
describe('with loadPath function returning falsy', () => {
169+
let backend
170+
let calledLanguages = []
171+
let calledNamespaces = []
172+
const loadPathSpy = (languages, namespaces) => {
173+
calledLanguages = calledLanguages.concat(languages)
174+
calledNamespaces = calledNamespaces.concat(namespaces)
175+
return ''
176+
}
177+
178+
before(() => {
179+
backend = new Http(
180+
{
181+
interpolator: i18next.services.interpolator
182+
},
183+
{
184+
loadPath: loadPathSpy
185+
}
186+
)
187+
})
188+
189+
describe('#read', () => {
190+
it('should not load data', (done) => {
191+
backend.read('en', 'test', (err, data) => {
192+
expect(err).not.to.be.ok()
193+
expect(calledLanguages).to.eql(['en'])
194+
expect(calledNamespaces).to.eql(['test'])
195+
expect(data).to.eql({})
196+
done()
197+
})
198+
})
199+
})
200+
})
201+
168202
describe('with addPath function', () => {
169203
let backend
170204
const calledLanguages = []

0 commit comments

Comments
 (0)