tailwind-nextjs-blog/components/ClientReload.tsx

24 lines
537 B
TypeScript
Raw Normal View History

2022-10-17 23:37:01 +08:00
import { useEffect } from 'react';
import Router from 'next/router';
2022-07-17 21:40:41 +08:00
/**
* Client-side complement to next-remote-watch
* Re-triggers getStaticProps when watched mdx files change
*
*/
export const ClientReload = () => {
// Exclude socket.io from prod bundle
useEffect(() => {
import('socket.io-client').then((module) => {
2022-10-17 23:37:01 +08:00
const socket = module.io();
socket.on('reload', () => {
2022-07-17 21:40:41 +08:00
Router.replace(Router.asPath, undefined, {
scroll: false,
2022-10-17 23:37:01 +08:00
});
});
});
}, []);
2022-07-17 21:40:41 +08:00
2022-10-17 23:37:01 +08:00
return null;
};