Next.js 重新加载网页

问题描述 投票:0回答:1

我可以在特定时间通过服务器重新加载客户端网页吗?

我想在中午 12 点重新加载客户的页面。我使用的是调度程序,但是客户端的网页没有自动刷新。

You can see my code API is working but the web page is not reloading itself.

javascript web next.js web-applications next.js13
1个回答
0
投票

您可以创建一个刷新客户端组件并在您想要重新加载/刷新的页面中渲染

// @/components/Refresh.jsx
'use client';

import { useEffect, useRef } from 'react';

export default function Refresh({ interval }) {
  const id = useRef('');

  useEffect(() => {
    id.current = setInterval(() => {
      // add your logic here to match 12 pm with current time and then reload
      window.location.reload();
    }, interval);
    return () => clearInterval(id.current);
  }, [id.current, interval]);
  return null;
}

// @/app/layout.jsx for all pages
// @/app/page.jsx for just homepage

<Refresh interval={60*1000} />

© www.soinside.com 2019 - 2024. All rights reserved.