-
-
Notifications
You must be signed in to change notification settings - Fork 5
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
Example of HMR with next-i18next@8 and next@10 #32
Comments
Yeap I'm aware of the changes, for now there is no way to get a reference to the i18next instance (that I'm aware of). |
Oh, I didn't realise you're the author of this lib (I actually created the linked issue). The quick fix has been merged, i.e. next-i18next now exports |
It doesn't matter where to put the hmr, it just need to get the instance. |
I've tried next-i18next v8, if you add // _app.js
import { appWithTranslation, i18n } from 'next-i18next';
if (process.env.NODE_ENV === 'development') {
if (typeof window === 'undefined') {
const { applyServerHMR } = require('i18next-hmr/server');
applyServerHMR(i18n);
} else {
const { applyClientHMR } = require('i18next-hmr/client');
applyClientHMR(i18n);
}
}
const MyApp = ({ Component, pageProps }) => <Component {...pageProps} />;
export default appWithTranslation(MyApp); It works, but it reloads the page when locale are changed. |
Thanks. I tried and couldn't get it to work that way since the i18n instance is null at that point (appWithTranslation has not been run yet). Also, the instance is re-created every time the route or locale changes. I had to do something really ugly like this: import { appWithTranslation, i18n } from 'next-i18next'
import i18NextConfig from '../next-i18next.config'
let _lastInstance = null
if (process.env.NODE_ENV === 'development') {
setInterval(() => {
if (i18n && _lastInstance !== i18n) {
if (typeof window === 'undefined') {
const { applyServerHMR } = require('i18next-hmr/server')
applyServerHMR(i18n)
} else {
const { applyClientHMR } = require('i18next-hmr/client')
applyClientHMR(i18n)
}
_lastInstance = i18n
}
}, 1000)
}
const MyApp = ({ Component, pageProps }) => <Component {...pageProps} />
export default appWithTranslation(MyApp, i18NextConfig) Regarding the reloading, I'm pretty sure the i18next-http-backend is going to have to be included by default soon. See i18next/next-i18next#1049. In the meantime, it can be solved like so: const HttpBackend = require('i18next-http-backend').default
module.exports = {
i18n: {
defaultLocale: 'en',
locales: ['en', 'de'],
},
serializeConfig: false,
backend: process.browser ? {
loadPath: '/locales/{{lng}}/{{ns}}.json', // this shouldn't be needed, but there's a bug with stripping /public in next-i18next
} : undefined,
use: process.browser ? [HttpBackend] : [],
} |
I see, unfortunately as it was discussed in here, currently, I don't have anything to do (as this lib author) in order to "overcome" it. I'm open for discussion of how to solve these issues. (In my project I fixed the version on v7) |
Facing the same problem here. I think it is more likely the Great job. |
For anyone else attempting to do this, I arrived at a pretty elegant solution 🎉
yarn add i18next-hmr i18next-http-backend --dev
const { resolve } = require("path")
const { I18NextHMRPlugin } = require("i18next-hmr/plugin")
const localesDir = resolve("public/locales")
module.exports = {
// ...other next config options
webpack(config, context) {
if (!context.isServer && context.dev) {
config.plugins.push(new I18NextHMRPlugin({ localesDir }))
}
return config
},
}
const HttpBackend = require("i18next-http-backend/cjs")
module.exports = {
// ...other next-i18next config options
use: process.browser ? [HttpBackend] : [],
}
import { useTranslation } from "next-i18next"
import { useEffect } from "react"
export const useHMR = (): void => {
const { i18n } = useTranslation()
if (process.env.NODE_ENV === "development" && !process.browser) {
import("i18next-hmr/server").then(({ applyServerHMR }) => {
applyServerHMR(i18n)
})
}
useEffect(() => {
if (process.env.NODE_ENV === "development") {
import("i18next-hmr/client").then(({ applyClientHMR }) => {
applyClientHMR(i18n)
})
}
}, [i18n])
}
import { AppProps } from "next/app"
import { FunctionComponent } from "react"
import { appWithTranslation } from "next-i18next"
import { useHMR } from "../hooks/use-hmr"
import i18nextConfig from "../../next-i18next.config"
export const App: FunctionComponent<AppProps> = ({ Component, pageProps }) => {
useHMR()
return <Component {...pageProps} />
}
export default appWithTranslation(App, i18nextConfig) // Required to use the HttpBackend that enables HMR on the client The beauty in this solution is the @felixmosh perhaps this is something that the repo should export from a nested import { useHMR } from "i18next-hmr/react" Wrapping the 2 dynamic Happy HMR'in! 🕺 |
@felixmosh the only issue that I've just noticed with my example above is that the client HMR code doesn't seem to be picking up the changes to non-default namespace files? In my project I have several namespaces, but only changes to the On the server all namespaces are picked up, so when I refresh the page I see the latest changes. Any ideas? Is it something to do with the |
@wagerfield thank you for sharing your idea 🙏🏼 i18next-hmr triggers a usage of
Can you prepare a demo repo so I will poke around? |
@felixmosh here we are kind Sir! https://github.com/wagerfield/i18next-hmr-hook If you start the dev server with You get the same lovely logs in both environments:
...and Next does a Fast Refresh and everything works as expected. Hurray 🎉 However if you edit any of the As mentioned above, the only obvious difference to me is the use of the Any help would be much appreciated. |
From what I've observed, looks like the HMR is working, You do get an update on the I've found the issue, it is here.
|
@felixmosh that is some excellent debugging—thank you! Commenting out that line resolves the issue. I'm curious as to why the Thanks again 🙌 |
@felixmosh (and anyone else following this thread) the PR that I put in to resolve the client side I can confirm that both client and server side HMR works with the https://github.com/wagerfield/i18next-hmr-hook @felixmosh with this PoC, do you think this hook should make its way into your repo under a |
Glad to hear. @wagerfield maybe ot can be as part of examples folder |
Hello, I've encountered a problem while using your HMR server: when I open / route, I see |
|
In my case, I we change source code client-hmr.js as below, problem will be solved: // other code
module.hot.accept('./trigger.js', () => {
const { changedFiles } = require('./trigger.js');
const currentNSList = Object.keys(i18n.store.data[i18n.language]); // get all loaded namespaces
const list = changedFiles
.map((changedFile) => extractLangAndNS(changedFile, currentNSList))
.filter(({ lang, ns }) => Boolean(lang) && Boolean(ns));
if (!list.length) {
return;
}
log(`Got an update with ${printList(list)}`);
return reloadTranslations(list);
}); |
@skmohammadi can you open a separate issue? It doesn't related to this issue. |
Sample repo created: https://github.com/skmohammadi/next-i18next-with-hmr |
Checkout the new example of next-i18next |
Hello ! I am using next v12.2.2 By doing the same as in next-i18next I have the HMR working ! But when I refesh the page the new translation does not persist and it fallback to the previous one |
Hi @Chill-Studio, do you have a console message on the server console when you are changing translations? |
Just that it compiled successfully |
Try to delete |
The problem persist after deleting . Do you want a project to repro ? |
Here is the repro https://github.com/Chill-Studio/repro.git To start
|
why on reload window the translation not updated? it updates only on hmr update |
Hi @geoapostolidis this thread has many responses, please open a new issue that describes your issue. As a first aid, it sounds that your server side HMR is not working, I need more details in order to help you. (Preferable reproduction repo) |
the hmr wotking well when i update the json file it updates the client correctly but when i refresh the page the translation goes back to the translation file i had when runned npm run dev and if i stop the server and re-run it it loads the updated file |
The API of next-i18next changes quite drastically with version 8 (Next v10 support). I'm not sure if this lib is still compatible (or if you want it to be), but if so it would be great with an example of how to get it going.
The text was updated successfully, but these errors were encountered: