admin管理员组

文章数量:1387360

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.

本文标签: nextjsHow to get the timestamp of a nextjs cache entryStack Overflow