最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

next.js - How to get the timestamp of a nextjs cache entry? - Stack Overflow

programmeradmin2浏览0评论

How can i display on the page the moment the cache was generated?

using nextjs15.

something like:

// app/page.tsx
import cache from '@next/something'
...
return ("<p>page was generated on {cache.timestamp}></p>")

How can i display on the page the moment the cache was generated?

using nextjs15.

something like:

// app/page.tsx
import cache from '@next/something'
...
return ("<p>page was generated on {cache.timestamp}></p>")
Share Improve this question asked Mar 18 at 20:14 gcbgcb 14.6k9 gold badges71 silver badges96 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

If you specifically need to know when Next.js last built a static page, you could store the build timestamp in an environment variable at build time and access it in your page. Would be better in this case..

Additionaly you can use the built-in generateMetadata like this example:

  // get current server timestamp
  function getTimestamp() {
  return new Date().toLocaleString();
   }

  export default function Page() {
  // frozen at time your app build for static pages
 // or evaluated at request time for dynamic pages *see the end lines*
 const timestamp = getTimestamp();
  
 return (
 <div>
  <h1>Your next Page</h1>
   <p>Page was generated on: {timestamp}</p>
  
     {/* Rest of your page content */}
                                </div>
        );
       }

      //  To ensure the page is dynamic and shows the actual render time
          export const dynamic = 'force-dynamic';

       // To revalidate at in intervals
         export const revalidate = 60; // revalidate every x seconds

And for a more advance feature you can add in as a Server Component context.

发布评论

评论列表(0)

  1. 暂无评论