Skip to content

Commit

Permalink
Merge branch 'vercel:canary' into Marukome0743-static-indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
Marukome0743 authored Oct 1, 2024
2 parents 96b89ac + eecf90c commit a0f7f14
Show file tree
Hide file tree
Showing 202 changed files with 2,478 additions and 1,308 deletions.
5 changes: 0 additions & 5 deletions crates/next-core/src/next_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1073,11 +1073,6 @@ impl NextConfig {
Vc::cell(self.experimental.taint.unwrap_or(false))
}

#[turbo_tasks::function]
pub fn enable_dynamic_io(&self) -> Vc<bool> {
Vc::cell(self.experimental.dynamic_io.unwrap_or(false))
}

#[turbo_tasks::function]
pub fn use_swc_css(&self) -> Vc<bool> {
Vc::cell(
Expand Down
21 changes: 7 additions & 14 deletions crates/next-core/src/next_import_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,12 @@ pub async fn get_next_client_import_map(
match ty.into_value() {
ClientContextType::Pages { .. } => {}
ClientContextType::App { app_dir } => {
let react_flavor = if *next_config.enable_ppr().await?
|| *next_config.enable_taint().await?
|| *next_config.enable_dynamic_io().await?
{
"-experimental"
} else {
""
};
let react_flavor =
if *next_config.enable_ppr().await? || *next_config.enable_taint().await? {
"-experimental"
} else {
""
};

import_map.insert_exact_alias(
"react",
Expand Down Expand Up @@ -686,12 +684,7 @@ async fn rsc_aliases(
) -> Result<()> {
let ppr = *next_config.enable_ppr().await?;
let taint = *next_config.enable_taint().await?;
let dynamic_io = *next_config.enable_dynamic_io().await?;
let react_channel = if ppr || taint || dynamic_io {
"-experimental"
} else {
""
};
let react_channel = if ppr || taint { "-experimental" } else { "" };
let react_client_package = get_react_client_package(&next_config).await?;

let mut alias = IndexMap::new();
Expand Down
38 changes: 19 additions & 19 deletions docs/02-app/01-building-your-application/04-caching/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Caching behavior changes depending on whether the route is statically or dynamic

## Request Memoization

Next.js extends the [`fetch` API](#fetch) to automatically **memoize** requests that have the same URL and options. This means you can call a fetch function for the same data in multiple places in a React component tree while only executing it once.
React extends the [`fetch` API](#fetch) to automatically **memoize** requests that have the same URL and options. This means you can call a fetch function for the same data in multiple places in a React component tree while only executing it once.

<Image
alt="Deduplicated Fetch Requests"
Expand Down Expand Up @@ -372,24 +372,24 @@ When configuring the different caching mechanisms, it's important to understand

The following table provides an overview of how different Next.js APIs affect caching:

| API | Router Cache | Full Route Cache | Data Cache | React Cache |
| ----------------------------------------------------------------------- | -------------------------- | --------------------- | --------------------- | ----------- |
| [`<Link prefetch>`](#link) | Cache | | | |
| [`router.prefetch`](#routerprefetch) | Cache | | | |
| [`router.refresh`](#routerrefresh) | Revalidate | | | |
| [`fetch`](#fetch) | | | Cache | Cache |
| [`fetch` `options.cache`](#fetch-optionscache) | | | Cache or Opt out | |
| [`fetch` `options.next.revalidate`](#fetch-optionsnextrevalidate) | | Revalidate | Revalidate | |
| [`fetch` `options.next.tags`](#fetch-optionsnexttags-and-revalidatetag) | | Cache | Cache | |
| [`revalidateTag`](#fetch-optionsnexttags-and-revalidatetag) | Revalidate (Server Action) | Revalidate | Revalidate | |
| [`revalidatePath`](#revalidatepath) | Revalidate (Server Action) | Revalidate | Revalidate | |
| [`const revalidate`](#segment-config-options) | | Revalidate or Opt out | Revalidate or Opt out | |
| [`const dynamic`](#segment-config-options) | | Cache or Opt out | Cache or Opt out | |
| [`cookies`](#cookies) | Revalidate (Server Action) | Opt out | | |
| [`headers`, `searchParams`](#dynamic-functions) | | Opt out | | |
| [`generateStaticParams`](#generatestaticparams) | | Cache | | |
| [`React.cache`](#react-cache-function) | | | | Cache |
| [`unstable_cache`](/docs/app/api-reference/functions/unstable_cache) | | | Cache | |
| API | Router Cache | Full Route Cache | Data Cache | React Request Memoization |
| ----------------------------------------------------------------------- | -------------------------- | --------------------- | --------------------- | ------------------------- |
| [`<Link prefetch>`](#link) | Cache | | | |
| [`router.prefetch`](#routerprefetch) | Cache | | | |
| [`router.refresh`](#routerrefresh) | Revalidate | | | |
| [`fetch`](#fetch) | | | Cache | Cache |
| [`fetch` `options.cache`](#fetch-optionscache) | | | Cache or Opt out | |
| [`fetch` `options.next.revalidate`](#fetch-optionsnextrevalidate) | | Revalidate | Revalidate | |
| [`fetch` `options.next.tags`](#fetch-optionsnexttags-and-revalidatetag) | | Cache | Cache | |
| [`revalidateTag`](#fetch-optionsnexttags-and-revalidatetag) | Revalidate (Server Action) | Revalidate | Revalidate | |
| [`revalidatePath`](#revalidatepath) | Revalidate (Server Action) | Revalidate | Revalidate | |
| [`const revalidate`](#segment-config-options) | | Revalidate or Opt out | Revalidate or Opt out | |
| [`const dynamic`](#segment-config-options) | | Cache or Opt out | Cache or Opt out | |
| [`cookies`](#cookies) | Revalidate (Server Action) | Opt out | | |
| [`headers`, `searchParams`](#dynamic-functions) | | Opt out | | |
| [`generateStaticParams`](#generatestaticparams) | | Cache | | |
| [`React.cache`](#react-cache-function) | | | | Cache |
| [`unstable_cache`](/docs/app/api-reference/functions/unstable_cache) | | | Cache | |

### `<Link>`

Expand Down
2 changes: 2 additions & 0 deletions docs/03-pages/02-api-reference/01-components/head.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export default IndexPage

In this case only the second `<meta property="og:title" />` is rendered. `meta` tags with duplicate `key` attributes are automatically handled.

> **Good to know**: `<title>` and `<base>` tags are automatically checked for duplicates by Next.js, so using key is not necessary for these tags.
> The contents of `head` get cleared upon unmounting the component, so make sure each page completely defines what it needs in `head`, without making assumptions about what other pages added.
## Use minimal nesting
Expand Down
51 changes: 51 additions & 0 deletions errors/next-request-in-use-cache.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
title: Cannot access `cookies()` or `headers()` in `"use cache"`
---

#### Why This Error Occurred

A function is trying to read from the current incoming request inside the scope of a function annotated with `"use cache"`. This is not supported because it would make the cache invalidated by every request which is probably not what you intended.

#### Possible Ways to Fix It

Instead of calling this inside the `"use cache"` function, move it outside the function and pass the value in as an argument. The specific value will now be part of the cache key through its arguments.

Before:

```jsx filename="app/page.js"
import { cookies } from 'next/headers'

async function getExampleData() {
"use cache"
- const isLoggedIn = (await cookies()).has('token')
...
}

export default async function Page() {
const data = await getExampleData()
return ...
}
```

After:

```jsx filename="app/page.js"
import { cookies } from 'next/headers'

async function getExampleData(isLoggedIn) {
"use cache"
...
}

export default async function Page() {
+ const isLoggedIn = (await cookies()).has('token')
const data = await getExampleData(isLoggedIn)
return ...
}
```

### Useful Links

- [`headers()` function](https://nextjs.org/docs/app/api-reference/functions/headers)
- [`cookies()` function](https://nextjs.org/docs/app/api-reference/functions/cookies)
- [`draftMode()` function](https://nextjs.org/docs/app/api-reference/functions/draft-mode)
16 changes: 16 additions & 0 deletions examples/environment-variables/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const metadata = {
title: "Next.js",
description: "Generated by Next.js",
};

export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html lang="en">
<body>{children}</body>
</html>
);
}
123 changes: 123 additions & 0 deletions examples/environment-variables/app/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import Link from "next/link";
import styles from "../styles.module.css";

type CodeProps = {
children: React.ReactNode;
};

const Code = ({ children }: CodeProps) => (
<code className={styles.inlineCode}>{children}</code>
);

const IndexPage = () => {
// The following console.log statements will only be executed on Node.js.
// Check the terminal to see the environment variables.
// Using the variables below in the browser will return `undefined`.
// Next.js doesn't expose environment variables unless they start with `NEXT_PUBLIC_`.
console.log("[Node.js only] ENV_VARIABLE:", process.env.ENV_VARIABLE);
console.log(
"[Node.js only] ENV_LOCAL_VARIABLE:",
process.env.ENV_LOCAL_VARIABLE,
);

return (
<div className={styles.container}>
<div className={styles.card}>
<h1>Environment Variables with Next.js</h1>
<hr className={styles.hr} />
<p>
In the table below you'll see how{" "}
<Link href="https://nextjs.org/docs/app/building-your-application/configuring/environment-variables">
environment variables can be exposed to the browser
</Link>{" "}
with Next.js.
</p>
<p>
In general only <Code>.env.local</Code> or <Code>.env</Code> are
needed for this, but the table also features the usage of{" "}
<Code>.env.development</Code> and <Code>.env.production</Code>.
</p>
<table className={styles.table}>
<thead>
<tr>
<th>Variable Name</th>
<th>Value</th>
<th>Added By</th>
</tr>
</thead>
<tbody>
<tr>
<td>NEXT_PUBLIC_ENV_VARIABLE</td>
<td>{process.env.NEXT_PUBLIC_ENV_VARIABLE}</td>
<td>
<Code>.env</Code>
</td>
</tr>
<tr>
<td>NEXT_PUBLIC_ENV_LOCAL_VARIABLE</td>
<td>{process.env.NEXT_PUBLIC_ENV_LOCAL_VARIABLE}</td>
<td>
<Code>.env.local</Code>
</td>
</tr>
<tr>
<td>NEXT_PUBLIC_DEVELOPMENT_ENV_VARIABLE</td>

<td>{process.env.NEXT_PUBLIC_DEVELOPMENT_ENV_VARIABLE}</td>
<td>
<Code>.env.development</Code>
</td>
</tr>
<tr>
<td>NEXT_PUBLIC_PRODUCTION_ENV_VARIABLE</td>

<td>{process.env.NEXT_PUBLIC_PRODUCTION_ENV_VARIABLE}</td>
<td>
<Code>.env.production</Code>
</td>
</tr>
</tbody>
</table>
<p>
<Code>.env.local</Code> is not added by the example, because it must
be ignored by git, but you can add it manually:
</p>
<pre>
<code>cp .env.local.example .env.local</code>
</pre>
<p>
Variables in <Code>.env.production</Code> won't be available if the
app is running in development:
</p>
<pre>
<code>npm run dev</code>
</pre>
<p>
Similarly, variables in <Code>.env.development</Code> won't be
available if the app is running on production:
</p>
<pre>
<code>npm run build && npm run start</code>
</pre>
<p>Once you run the app, you'll see logs like these in the terminal:</p>
<pre>
<code>
info - Loaded env from /home/user/../.env.local{"\n"}
info - Loaded env from /home/user/../.env.development{"\n"}
info - Loaded env from /home/user/../.env{"\n"}
</code>
</pre>
<p>
The order is important, the first loaded env will have a higher
priority.
</p>
<p>
<Code>.env</Code> will not overwrite any variables defined in{" "}
<Code>.env.local</Code> or <Code>.env.development</Code>.
</p>
</div>
</div>
);
};

export default IndexPage;
2 changes: 1 addition & 1 deletion examples/environment-variables/next-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/pages/building-your-application/configuring/typescript for more information.
// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information.
14 changes: 7 additions & 7 deletions examples/environment-variables/package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{
"private": true,
"scripts": {
"dev": "next",
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"next": "latest",
"react": "^18.2.0",
"react-dom": "^18.2.0"
"react": "^18.3.1",
"react-dom": "^18.3.1"
},
"devDependencies": {
"@types/node": "^18.11.5",
"@types/react": "^18.0.23",
"@types/react-dom": "^18.0.7",
"typescript": "^4.8.4"
"@types/node": "^22.7.4",
"@types/react": "^18.3.10",
"@types/react-dom": "^18.3.0",
"typescript": "^5.6.2"
}
}
Loading

0 comments on commit a0f7f14

Please # to comment.