i18next-http-middleware 3.9.9 → 3.9.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cjs/index.js CHANGED
@@ -258,17 +258,27 @@ function getResourcesHandler(i18next) {
258
258
  i18next.options.ns.push(ns);
259
259
  }
260
260
  });
261
+ var loaded = false;
262
+ var onLoaded;
261
263
  i18next.services.backendConnector.load(languages, namespaces, function () {
262
264
  languages.forEach(function (lng) {
263
265
  namespaces.forEach(function (ns) {
264
266
  utils.setPath(resources, [lng, ns], i18next.getResourceBundle(lng, ns));
265
267
  });
266
268
  });
269
+ loaded = true;
270
+ if (onLoaded) onLoaded();
267
271
  });
268
- if (typeof Response !== 'undefined' && res.constructor === Response && typeof JSON !== 'undefined') {
269
- return options.send(res, JSON.stringify(resources));
270
- }
271
- return options.send(res, resources);
272
+ var sendResources = function sendResources() {
273
+ if (typeof Response !== 'undefined' && res.constructor === Response && typeof JSON !== 'undefined') {
274
+ return options.send(res, JSON.stringify(resources));
275
+ }
276
+ return options.send(res, resources);
277
+ };
278
+ if (loaded) return sendResources();
279
+ return new Promise(function (resolve) {
280
+ onLoaded = resolve;
281
+ }).then(sendResources);
272
282
  };
273
283
  }
274
284
  function missingKeyHandler(i18next) {
package/esm/index.js CHANGED
@@ -241,17 +241,27 @@ export function getResourcesHandler(i18next) {
241
241
  i18next.options.ns.push(ns);
242
242
  }
243
243
  });
244
+ var loaded = false;
245
+ var onLoaded;
244
246
  i18next.services.backendConnector.load(languages, namespaces, function () {
245
247
  languages.forEach(function (lng) {
246
248
  namespaces.forEach(function (ns) {
247
249
  utils.setPath(resources, [lng, ns], i18next.getResourceBundle(lng, ns));
248
250
  });
249
251
  });
252
+ loaded = true;
253
+ if (onLoaded) onLoaded();
250
254
  });
251
- if (typeof Response !== 'undefined' && res.constructor === Response && typeof JSON !== 'undefined') {
252
- return options.send(res, JSON.stringify(resources));
253
- }
254
- return options.send(res, resources);
255
+ var sendResources = function sendResources() {
256
+ if (typeof Response !== 'undefined' && res.constructor === Response && typeof JSON !== 'undefined') {
257
+ return options.send(res, JSON.stringify(resources));
258
+ }
259
+ return options.send(res, resources);
260
+ };
261
+ if (loaded) return sendResources();
262
+ return new Promise(function (resolve) {
263
+ onLoaded = resolve;
264
+ }).then(sendResources);
255
265
  };
256
266
  }
257
267
  export function missingKeyHandler(i18next) {
package/lib/index.js CHANGED
@@ -272,6 +272,8 @@ export function getResourcesHandler (i18next, options = {}) {
272
272
  }
273
273
  })
274
274
 
275
+ let loaded = false
276
+ let onLoaded
275
277
  i18next.services.backendConnector.load(languages, namespaces, function () {
276
278
  languages.forEach(lng => {
277
279
  namespaces.forEach(ns => {
@@ -282,14 +284,22 @@ export function getResourcesHandler (i18next, options = {}) {
282
284
  )
283
285
  })
284
286
  })
287
+ loaded = true
288
+ if (onLoaded) onLoaded()
285
289
  })
286
290
 
287
- if (typeof Response !== 'undefined' &&
288
- res.constructor === Response &&
289
- typeof JSON !== 'undefined') {
290
- return options.send(res, JSON.stringify(resources))
291
+ const sendResources = () => {
292
+ if (typeof Response !== 'undefined' &&
293
+ res.constructor === Response &&
294
+ typeof JSON !== 'undefined') {
295
+ return options.send(res, JSON.stringify(resources))
296
+ }
297
+ return options.send(res, resources)
291
298
  }
292
- return options.send(res, resources)
299
+
300
+ if (loaded) return sendResources()
301
+ // an async backend calls back after load() returned, so send once it did
302
+ return new Promise(resolve => { onLoaded = resolve }).then(sendResources)
293
303
  }
294
304
  }
295
305
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "i18next-http-middleware",
3
- "version": "3.9.9",
3
+ "version": "3.9.10",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "./cjs/index.js",