Unverified Commit 61fb9359 authored by Jan Kassens's avatar Jan Kassens Committed by GitHub

[create-next-app] Enable Cache Components by default in create-next-app (#99436)

Updates the default of `create-next-app` to enable Cache Components.
This makes sure new apps have an easier transition. Also updates
documentation.

Closes NAR-928
parent 40a91948
...@@ -86,7 +86,7 @@ On installation, you'll see the following prompts: ...@@ -86,7 +86,7 @@ On installation, you'll see the following prompts:
```txt filename="Terminal" ```txt filename="Terminal"
What is your project named? my-app What is your project named? my-app
Would you like to use the recommended Next.js defaults? Would you like to use the recommended Next.js defaults?
Yes, use recommended defaults - TypeScript, ESLint, No React Compiler, Tailwind CSS, No src/ directory, App Router, No Cache Components, AGENTS.md Yes, use recommended defaults - TypeScript, ESLint, No React Compiler, Tailwind CSS, No src/ directory, App Router, Cache Components, AGENTS.md
No, reuse previous settings No, reuse previous settings
No, customize settings - Choose your own preferences No, customize settings - Choose your own preferences
``` ```
......
...@@ -17,7 +17,9 @@ Caching is a technique for storing the result of data fetching and other computa ...@@ -17,7 +17,9 @@ Caching is a technique for storing the result of data fetching and other computa
## Enabling Cache Components ## Enabling Cache Components
You can enable Cache Components by adding the [`cacheComponents`](/docs/app/api-reference/config/next-config-js/cacheComponents) option to your Next config file: New App Router apps created with the recommended [`create-next-app`](/docs/app/api-reference/cli/create-next-app) defaults already have Cache Components enabled.
To enable it in an existing or manually created app, add the [`cacheComponents`](/docs/app/api-reference/config/next-config-js/cacheComponents) option to your Next config file:
```ts filename="next.config.ts" switcher ```ts filename="next.config.ts" switcher
import type { NextConfig } from 'next' import type { NextConfig } from 'next'
......
...@@ -38,7 +38,7 @@ The following options are available: ...@@ -38,7 +38,7 @@ The following options are available:
| `--js` or `--javascript` | Initialize as a JavaScript project | | `--js` or `--javascript` | Initialize as a JavaScript project |
| `--tailwind` | Initialize with Tailwind CSS config (default) | | `--tailwind` | Initialize with Tailwind CSS config (default) |
| `--react-compiler` | Initialize with React Compiler enabled | | `--react-compiler` | Initialize with React Compiler enabled |
| `--cache-components` | Initialize with Cache Components enabled | | `--cache-components` | Initialize with Cache Components enabled (default for App Router) |
| `--eslint` | Initialize with ESLint config | | `--eslint` | Initialize with ESLint config |
| `--biome` | Initialize with Biome config | | `--biome` | Initialize with Biome config |
| `--no-linter` | Skip linter configuration | | `--no-linter` | Skip linter configuration |
...@@ -90,7 +90,7 @@ On installation, you'll see the following prompts: ...@@ -90,7 +90,7 @@ On installation, you'll see the following prompts:
```txt filename="Terminal" ```txt filename="Terminal"
What is your project named? my-app What is your project named? my-app
Would you like to use the recommended Next.js defaults? Would you like to use the recommended Next.js defaults?
Yes, use recommended defaults - TypeScript, ESLint, No React Compiler, Tailwind CSS, No src/ directory, App Router, No Cache Components, AGENTS.md, Agent feedback Yes, use recommended defaults - TypeScript, ESLint, No React Compiler, Tailwind CSS, No src/ directory, App Router, Cache Components, AGENTS.md, Agent feedback
No, reuse previous settings No, reuse previous settings
No, customize settings - Choose your own preferences No, customize settings - Choose your own preferences
``` ```
......
...@@ -255,7 +255,7 @@ async function run(): Promise<void> { ...@@ -255,7 +255,7 @@ async function run(): Promise<void> {
empty: false, empty: false,
disableGit: false, disableGit: false,
reactCompiler: false, reactCompiler: false,
cacheComponents: false, cacheComponents: true,
agentsMd: true, agentsMd: true,
} }
...@@ -611,10 +611,9 @@ async function run(): Promise<void> { ...@@ -611,10 +611,9 @@ async function run(): Promise<void> {
} }
// Cache Components is an App Router feature, so only offer it when the App // Cache Components is an App Router feature, so only offer it when the App
// Router is in use. // Router is in use, including API-only projects.
if ( if (
opts.app && (opts.app || opts.api) &&
!opts.api &&
!opts.cacheComponents && !opts.cacheComponents &&
!args.includes('--no-cache-components') !args.includes('--no-cache-components')
) { ) {
......
...@@ -15,6 +15,61 @@ describe('create-next-app', () => { ...@@ -15,6 +15,61 @@ describe('create-next-app', () => {
nextTgzFilename = resolveNextTgzFilename() nextTgzFilename = resolveNextTgzFilename()
}) })
it.each([
{ flags: ['--ts', '--app'], configFile: 'next.config.ts', enabled: true },
{ flags: ['--js', '--app'], configFile: 'next.config.mjs', enabled: true },
{
flags: ['--ts', '--app', '--no-cache-components'],
configFile: 'next.config.ts',
enabled: false,
},
{
flags: ['--ts', '--no-app'],
configFile: 'next.config.ts',
enabled: false,
},
{ flags: ['--ts', '--api'], configFile: 'next.config.ts', enabled: true },
{ flags: ['--js', '--api'], configFile: 'next.config.mjs', enabled: true },
{
flags: ['--ts', '--api', '--no-cache-components'],
configFile: 'next.config.ts',
enabled: false,
},
{
flags: ['--js', '--api', '--no-cache-components'],
configFile: 'next.config.mjs',
enabled: false,
},
])(
'should set Cache Components to $enabled with $flags',
async ({ flags, configFile, enabled }) => {
const Conf = require('next/dist/compiled/conf')
await useTempDir(async (cwd) => {
const conf = new Conf({ projectName: 'create-next-app' })
conf.clear()
const projectName = 'cache-components'
const res = await run(
[projectName, ...flags, '--skip-install'],
nextTgzFilename,
{ cwd, env: { ...process.env, CI: '1' } }
)
expect(res.exitCode).toBe(0)
const config = await readFile(
join(cwd, projectName, configFile),
'utf8'
)
if (enabled) {
expect(config).toContain('cacheComponents: true')
expect(config).toContain('partialPrefetching: true')
} else {
expect(config).not.toContain('cacheComponents:')
expect(config).not.toContain('partialPrefetching:')
}
})
}
)
it('should list both agent feedback flags in help', async () => { it('should list both agent feedback flags in help', async () => {
await useTempDir(async (cwd) => { await useTempDir(async (cwd) => {
const res = await run(['--help'], nextTgzFilename, { const res = await run(['--help'], nextTgzFilename, {
...@@ -230,7 +285,7 @@ describe('create-next-app', () => { ...@@ -230,7 +285,7 @@ describe('create-next-app', () => {
" --eslint ESLint (use --biome for Biome, --no-eslint for None) " --eslint ESLint (use --biome for Biome, --no-eslint for None)
--no-react-compiler No React Compiler (use --react-compiler for React Compiler) --no-react-compiler No React Compiler (use --react-compiler for React Compiler)
--no-src-dir No src/ directory (use --src-dir for src/ directory) --no-src-dir No src/ directory (use --src-dir for src/ directory)
--no-cache-components No Cache Components (use --cache-components for Cache Components) --cache-components Cache Components (use --no-cache-components for No Cache Components)
--agents-md AGENTS.md (use --no-agents-md for No AGENTS.md) --agents-md AGENTS.md (use --no-agents-md for No AGENTS.md)
--no-agent-feedback No agent feedback (use --agent-feedback for Agent feedback) --no-agent-feedback No agent feedback (use --agent-feedback for Agent feedback)
--import-alias "@/*"" --import-alias "@/*""
......
import { retry } from 'next-test-utils' import { retry } from 'next-test-utils'
import { readFileSync } from 'fs' import { readFileSync } from 'fs'
import { join } from 'path' import { join } from 'path'
import stripAnsi from 'strip-ansi'
import { import {
createNextApp, createNextApp,
projectFilesShouldExist, projectFilesShouldExist,
...@@ -139,6 +140,9 @@ describe('create-next-app prompts', () => { ...@@ -139,6 +140,9 @@ describe('create-next-app prompts', () => {
}) })
expectTurbopackTailwindSetup(cwd, projectName) expectTurbopackTailwindSetup(cwd, projectName)
expect(
readFileSync(join(cwd, projectName, 'next.config.ts'), 'utf8')
).toContain('cacheComponents: true')
}) })
}) })
...@@ -224,6 +228,9 @@ describe('create-next-app prompts', () => { ...@@ -224,6 +228,9 @@ describe('create-next-app prompts', () => {
expect( expect(
readFileSync(join(cwd, projectName, 'next.config.ts'), 'utf8') readFileSync(join(cwd, projectName, 'next.config.ts'), 'utf8')
).not.toContain('agentFeedback') ).not.toContain('agentFeedback')
expect(
readFileSync(join(cwd, projectName, 'next.config.ts'), 'utf8')
).toContain('cacheComponents: true')
}) })
}) })
...@@ -273,9 +280,89 @@ describe('create-next-app prompts', () => { ...@@ -273,9 +280,89 @@ describe('create-next-app prompts', () => {
expect( expect(
readFileSync(join(cwd, projectName, 'next.config.ts'), 'utf8') readFileSync(join(cwd, projectName, 'next.config.ts'), 'utf8')
).toContain('\n experimental: {\n agentFeedback: true,\n },\n') ).toContain('\n experimental: {\n agentFeedback: true,\n },\n')
expect(
readFileSync(join(cwd, projectName, 'next.config.ts'), 'utf8')
).toContain('cacheComponents: true')
}) })
}) })
it.each([
{ saved: undefined, reuse: false, enabled: true },
{ saved: false, reuse: false, enabled: false },
{ saved: false, reuse: true, enabled: false },
])(
'should default Cache Components to $enabled with saved=$saved and reuse=$reuse',
async ({ saved, reuse, enabled }) => {
const Conf = require('next/dist/compiled/conf')
await useTempDir(async (cwd) => {
const conf = new Conf({ projectName: 'create-next-app' })
conf.clear()
if (saved !== undefined) {
conf.set('preferences', { cacheComponents: saved })
}
const projectName = 'cache-components-prompt'
const childProcess = createNextApp(
[projectName],
{ cwd },
nextTgzFilename,
false
)
const exited = new Promise((resolve) => {
childProcess.on('exit', resolve)
})
let output = ''
childProcess.stdout.on('data', (data) => {
output += data
})
const answer = async (prompt: string, input = '\n') => {
await retry(async () => {
expect(stripAnsi(output)).toContain(prompt)
})
childProcess.stdin.write(input)
}
// Saved preferences add a reuse option before the customize option.
const selection = saved !== undefined && !reuse ? 2 : 1
await answer(
'Would you like to use the recommended Next.js defaults?',
'\u001b[B'.repeat(selection) + '\n'
)
if (!reuse) {
for (const prompt of [
'Would you like to use TypeScript?',
'Which linter would you like to use?',
'Would you like to use React Compiler?',
'Would you like to use Tailwind CSS?',
'Would you like your code inside a',
'Would you like to use App Router?',
'Would you like to use Cache Components?',
'Would you like to customize the import alias',
'Would you like to include AGENTS.md',
]) {
await answer(prompt)
}
}
await answer('Would you like to help improve Next.js')
expect(await exited).toBe(0)
const config = readFileSync(
join(cwd, projectName, 'next.config.ts'),
'utf8'
)
if (enabled) {
expect(config).toContain('cacheComponents: true')
expect(config).toContain('partialPrefetching: true')
} else {
expect(config).not.toContain('cacheComponents:')
expect(config).not.toContain('partialPrefetching:')
}
})
}
)
it('should show reuse previous settings option when preferences exist', async () => { it('should show reuse previous settings option when preferences exist', async () => {
const Conf = require('next/dist/compiled/conf') const Conf = require('next/dist/compiled/conf')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment