Use fetch api when supported
See original GitHub issueI’m using i18next
in a service worker setup right now with this xhr
backend, however, there was one obstacle to get it working: XMLHttpRequest
is not defined in the service worker context. I did manage to get the backend working in the following manner however:
import i18next from 'i18next';
import i18nextXHR from 'i18next-xhr-backend'; // eslint-disable-line import/no-unresolved,import/no-extraneous-dependencies
let t = null;
self.addEventListener('activate', (event) => {
event.waitUntil(new Promise((resolve, reject) => {
i18next
.use(i18nextXHR)
.init({
fallbackLng: ['ja', 'en', 'zh'],
preload: ['ja', 'en', 'zh'],
ns: 'translation',
defaultNS: 'translation',
keySeparator: false, // Allow usage of dots in keys
nsSeparator: false,
backend: {
loadPath: '/locales/{{lng}}/{{ns}}.json',
ajax(url, options, cb) {
fetch(url, options)
.then((res) => {
if (res.ok) {
return res.text()
.then((json) => {
cb(json, res);
});
}
return new Error('Error loading translation json');
});
},
},
}, (err, _t) => {
if (err) {
reject(err);
return;
}
t = _t;
resolve();
});
}));
});
By using the ajax
option and using the fetch
API, this backend works like charm… however, maybe it’s a good idea to use the fetch
API here wherever it’s supported by default? It’s a more modern API supported by modern browsers, and would make using i18next
in service worker contexts, or other modern context much more easy
Issue Analytics
- State:
- Created 7 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Fetch API - MDN Web Docs
The Fetch API provides an interface for fetching resources (including across the network). It will seem familiar to anyone who has used ...
Read more >Fetch | Can I use... Support tables for HTML5, CSS3, etc
"Can I use" provides up-to-date browser support tables for support of front-end web technologies on desktop and mobile web browsers.
Read more >JavaScript Fetch API Explained By Examples
The Fetch API allows you to asynchronously request for a resource. Use the fetch() method to return a promise that resolves into a...
Read more >The Fetch API is finally coming to Node.js - LogRocket Blog
It's been a long time coming, but the Fetch API is now available in Node.js core. Learn why it took so long and...
Read more >Fetch API in Node.js - Atatus
In all popular browsers, the Fetch API has native support. The node-fetch package is used by JavaScript developers to create server-side code.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Just published the module here
Actually, what do you think of this as the
fetch-backend
:Just tested it and seems to work in my case. I’ll publish it as an npm module tomorrow