|
1 | 1 | ---
|
2 |
| -title: Cannot infer intended usage of current time with `Date.now()`, `Date()`, or `new Date()` in a Server Component |
| 2 | +title: Cannot access `Date.now()`, `Date()`, or `new Date()` before other uncached data or Request data in a Server Component |
3 | 3 | ---
|
4 | 4 |
|
5 | 5 | ## Why This Error Occurred
|
6 | 6 |
|
7 |
| -Reading the current time in a Server Component can be ambiguous. Sometimes you intend to capture the time when something was cached, other times you intend to capture the time of a user Request. You might also be trying to measure runtime performance to track elapsed time. |
8 |
| - |
9 |
| -Depending on your use case you might use alternative time APIs like `performance.now()`, you might cache the time read with `"use cache"`, or you might communicate that the time must be evaluated on each request by guarding it with `await connection()` or moving it into a Client Component. |
| 7 | +`Date.now()`, `Date()`, or `new Date()` was used in a Server Component before accessing other uncached data through APIs like `fetch()` and native database drivers, or Request data through built-in APIs like `cookies()`, `headers()`, `connection()` and `searchParams`. Accessing the current time in this way interferes with the prerendering and prefetching capabilities of Next.js. |
10 | 8 |
|
11 | 9 | ## Possible Ways to Fix It
|
12 | 10 |
|
| 11 | +If the current time is being used for diagnostic purposes such as logging or performance tracking consider using `performance.now()` instead. |
| 12 | + |
| 13 | +If the current time is appropriate to be prerendered and prefetched consider moving it into a Cache Component or Cache Function with the `"use cache"` directive. |
| 14 | + |
| 15 | +If the current time is intended to be accessed dynamically on every user request first consider whether it is more appropriate to access it in a Client Component, which can often be the case when reading the time for display purposes. If a Client Component isn't the right choice then consider whether you can move the current time access later, behind other existing uncached data or Request data access. If there is no way to do this you can always precede the current time access with Request data access by using `await connection()`. |
| 16 | + |
| 17 | +> **Note**: Sometimes the place that accesses the current time is inside 3rd party code. While you can't easily convert the time access to `performance.now()` the other strategies can be applied in your own project code regardless of how deeply the time is read. |
| 18 | +
|
13 | 19 | ### Performance use case
|
14 | 20 |
|
15 | 21 | If you are using the current time for performance tracking with elapsed time use `performance.now()`.
|
@@ -40,6 +46,7 @@ export default async function Page() {
|
40 | 46 | ```
|
41 | 47 |
|
42 | 48 | > **Note**: If you need report an absolute time to an observability tool you can also use `performance.timeOrigin + performance.now()`.
|
| 49 | +> **Note**: It is essential that the values provided by `performance.now()` do not influence the rendered output of your Component and should never be passed into Cache Functions as arguments or props |
43 | 50 |
|
44 | 51 | ### Cacheable use cases
|
45 | 52 |
|
@@ -214,6 +221,8 @@ async function DynamicBanner() {
|
214 | 221 | }
|
215 | 222 | ```
|
216 | 223 |
|
| 224 | +> **Note**: This example illustrates using `await connection()` but you could just as well used moved where a uncached fetch was happening or read cookies before as well. |
| 225 | +
|
217 | 226 | ## Useful Links
|
218 | 227 |
|
219 | 228 | - [`Date.now` API](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now)
|
|
0 commit comments