15
loading...
This website collects cookies to deliver better user experience
next-i18next
automatically, you can do it like below:import { GetStaticProps } from "next";
import { i18n } from "next-i18next";
import { serverSideTranslations } from "next-i18next/serverSideTranslations";
import { useRouter } from "next/router";
import { useEffect } from "react";
import Home from "../components/home";
export const getStaticProps: GetStaticProps = async ({ locale }) => {
if (process.env.NODE_ENV === "development") {
await i18n?.reloadResources();
}
return {
props: {
...(await serverSideTranslations(locale!, ["common"])),
},
};
};
const Index = () => {
if (process.env.NODE_ENV === "development") {
const router = useRouter();
useEffect(() => {
const timer = setInterval(() => {
router.replace(router.asPath, undefined, {
scroll: false,
});
}, 5000);
return () => clearTimeout(timer);
});
}
return <Home />;
};
export default Index;
next-i18next
, I realized next dev
server only loaded the translation files once when initialized and never updated even if I reloaded the page on my browser since server side doesn't change. Whenever I updated the translation, I needed to restart next dev
server, which was bad developer experience.next dev
automatically whenever the content is updated.nodemon
can easily achieve the goal. However, this is not "Fast Refresh" and takes a while.next dev
server keeps running, but too complicated to implement internal API. It can be done without API like next-remote-watch
which monitors files and calls Next.js's internal method to reload the page. I tried it but it still requires implementation of content refresh by calling i18n.reloadResources()
anyway. Also, page refresh is not "Fast Refresh" neither.next-remote-watch
does is not sustainable. Therefore, client side polling is the best way./api/something
) for such a simple polling seems overkill. I thought it's probably enough by just re-rendering the page. With this approach, unless the virtual DOM of React has been updated, nothing happens on client side (I think).GetStaticProps
which is already used by next-i18next
installation. I found a great solution to trigger this from client side.i18n.reloadResources()
there because i18n
instance is stored in a global value. I lazily implemented it with reloading i18n
at every request because my project doesn't have large translation files. This can eliminate file watcher logic at all.SetInterval()
to refresh the page every 5 seconds on client side and reloading i18n
on every GetStaticProps
call, my Next.js pages are always in sync within 5 seconds. This is Next.js/React refresh, not browser refresh nor server restart, thus it's fast enough.router.replace()
always scrolls up to the anchor and doesn't reload preps from the server. This is a known issue and there is a discussion on GitHub repository: https://github.com/vercel/next.js/discussions/13804