-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question - how to correctly set up react-intl provider with Gatsby? #3792
Comments
Turns out it had nothing to do with Gatsby and I was simply missing import { addLocaleData } from 'react-intl';
import localeData from 'react-intl/locale-data/de';
addLocaleData(localeData); in my layouts 🤦♂️ |
how do you polyfill |
@antoinerousseau I'm using https://polyfill.io. It doesn't always work correctly, as I still sometimes get notifications about missing I've got the following code in exports.onClientEntry = () => {
/*
* Polyfills via polyfill.io
*/
return new Promise((resolve, reject) => {
// Global callback for polyfill.io script
// eslint-disable-next-line no-underscore-dangle
window.__polyfillio__ = () => {
resolve();
};
const features = [];
if (!('Intl' in window)) {
const locale = getLocaleFromPath(window.location.pathname);
features.push(`Intl.~locale.${locale}`);
}
if (!('fetch' in window)) {
features.push('fetch');
}
if (!('URL' in window && 'URLSearchParams' in window)) {
features.push('URL');
}
// Use 'always' flag to download polyfills regardless of user agent.
// We add features to the list only if they are not supported.
if (features.length) {
const s = document.createElement('script');
s.src = `https://cdn.polyfill.io/v2/polyfill.min.js?features=${features.join(
',',
)}&rum=1&flags=always&callback=__polyfillio__`;
s.async = true;
s.onerror = reject;
document.head.appendChild(s);
} else {
resolve();
}
});
}; |
thanks @szimek for sharing your code. |
@antoinerousseau I'm not sure. We had this code before we migrated to Gatsby v2. But it looks like it might not be: zloirock/core-js#25. |
I'm trying to add
react-intl
to my project and when I rungatsby build
static pages are generated correctly, but when I load them in the browser,intl
object is messed up in template components, i.e. it has incorrect locale andintl.messages
object is empty.My current setup looks like this:
IntlProvider
inindex
layout:FormattedMessage
andinjectIntl
.I've added
console.log
at each step to see where it gets messed up.Here are logs when running
gatsby build
for/de/blog
page:Here are logs when opening
/de/blog
page in a browser:As you can see in the template the data is different when running it in a browser, than in the generated static page.
Any idea how to set it up properly?
The text was updated successfully, but these errors were encountered: