Next.js Discord

Discord Forum

Using Date throws performance benchmarking error?

Answered
American black bear posted this in #help-forum
Open in Discord
American black bearOP
I have a copyright component which takes the current year using Date API, which throws a mysterious error (included as image attachment of this post) I've never seen before updating to canary. Next.js thinks that I am using Date API for performance benchmarking and warns me that I should be using performance API instead, however I am using Date for displaying the current year.

The linked docs on the topic do not specify how to fix this, and instead just tell me to switch to using performance. Any ideas for fixes?

"use client"

function Copyright() {
  const year = new Date().getFullYear()

  return <span>&copy; {year} Company Name. All rights reserved.</span>
}


Error text:
Error: Route "/" used `new Date()` instead of using `performance` or without explicitly calling `await connection()` beforehand. See more info here: https://nextjs.org/docs/messages/next-prerender-current-time
Answered by joulev
then new Date() requires either connection() or an explicit use cache
View full answer

7 Replies

American black bearOP
yes
const config = {
  experimental: {
    useCache: true,
    dynamicIO: true,
  },
};
@American black bear yes
then new Date() requires either connection() or an explicit use cache
Answer
nextjs doesn't care that you only need to get the year, as far as nextjs is concerned you are trying to get the exact millisecond count since unix epoch, hence it needs to know whether you want it cached or not
American black bearOP
Caching the component using Date api fixed the issue:

"use cache"

export default async function Footer() {
  return (
    <span>{new Date().getFullYear()}</span>
  )
}
Thank you @joulev