Unverified Commit 5a2d24ea authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

create-next-app: remove App Router prompt and always use App Router (#100850)

`create-next-app` no longer asks "Would you like to use App Router?
(recommended)". New projects always use the App Router.

**Why**

The App Router is the recommended default, and the interactive prompt
only offered a way to pick the Pages Router by accident. A saved `app:
false` preference from an earlier run also kept silently producing Pages
Router projects.

**Approach**

- Remove the App Router toggle prompt and set `opts.app = true` unless
`--no-app` or `--api` is passed.
- Drop `app` from the defaults/preferences display, so the "recommended
defaults" and "reuse previous settings" summaries no longer list "App
Router" / "Pages Router".
- Saved `app` preferences are ignored. `--no-app` remains as the
explicit opt-out, so the existing Pages templates and their tests are
unchanged.

**Validation**

- `prompts.test.ts`: removed the App Router prompt from the customize
flow; the "reuse previous settings" test with a saved `app: false` now
expects an `app/` directory.
- Type-checked `packages/create-next-app/index.ts` with no new errors.
The create-next-app e2e tests were not run locally.

**Docs**

Removed the prompt line and "App Router" from the recommended defaults
summary in the installation guide and the `create-next-app` CLI
reference.

---------
Co-authored-by: 's avatarroboeve[bot] <311457182+roboeve[bot]@users.noreply.github.com>
Co-authored-by: 's avatarTim Neutkens <6324199+timneutkens@users.noreply.github.com>
parent 94ea1c21
......@@ -86,7 +86,7 @@ On installation, you'll see the following prompts:
```txt filename="Terminal"
What is your project named? my-app
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, Cache Components, AGENTS.md
Yes, use recommended defaults - TypeScript, ESLint, No React Compiler, Tailwind CSS, No src/ directory, Cache Components, AGENTS.md
No, reuse previous settings
No, customize settings - Choose your own preferences
```
......@@ -99,7 +99,6 @@ Which linter would you like to use? ESLint / Biome / None
Would you like to use React Compiler? No / Yes
Would you like to use Tailwind CSS? No / Yes
Would you like your code inside a `src/` directory? No / Yes
Would you like to use App Router? (recommended) No / Yes
Would you like to use Cache Components? No / Yes
Would you like to customize the import alias (`@/*` by default)? No / Yes
What import alias would you like configured? @/*
......
......@@ -90,7 +90,7 @@ On installation, you'll see the following prompts:
```txt filename="Terminal"
What is your project named? my-app
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, Cache Components, AGENTS.md, Agent feedback
Yes, use recommended defaults - TypeScript, ESLint, No React Compiler, Tailwind CSS, No src/ directory, Cache Components, AGENTS.md, Agent feedback
No, reuse previous settings
No, customize settings - Choose your own preferences
```
......@@ -103,7 +103,6 @@ Which linter would you like to use? ESLint / Biome / None
Would you like to use React Compiler? No / Yes
Would you like to use Tailwind CSS? No / Yes
Would you like your code inside a `src/` directory? No / Yes
Would you like to use App Router? (recommended) No / Yes
Would you like to use Cache Components? No / Yes
Would you like to customize the import alias (`@/*` by default)? No / Yes
What import alias would you like configured? @/*
......
......@@ -145,7 +145,10 @@ const packageManager: PackageManager = !!opts.useNpm
: getPkgManager()
async function run(): Promise<void> {
const conf = new Conf({ projectName: 'create-next-app' })
const conf = new Conf({
projectName: 'create-next-app',
cwd: process.env.NEXT_PRIVATE_TEST_CONFIG_DIR,
})
if (opts.resetPreferences) {
const { resetPreferences } = await prompts({
......@@ -248,7 +251,6 @@ async function run(): Promise<void> {
eslint: false,
linter: 'eslint',
tailwind: true,
app: true,
srcDir: false,
importAlias: '@/*',
customizeImportAlias: false,
......@@ -296,11 +298,6 @@ async function run(): Promise<void> {
values: { true: 'src/ directory', false: 'No src/ directory' },
flags: { true: '--src-dir', false: '--no-src-dir' },
},
{
key: 'app',
values: { true: 'App Router', false: 'Pages Router' },
flags: { true: '--app', false: '--no-app' },
},
{
key: 'cacheComponents',
values: { true: 'Cache Components', false: 'No Cache Components' },
......@@ -591,23 +588,10 @@ async function run(): Promise<void> {
}
}
// New projects always use the App Router. Saved preferences from older
// versions are ignored; only an explicit --no-app opts out.
if (!opts.app && !args.includes('--no-app') && !opts.api) {
if (skipPrompt) {
opts.app = getPrefOrDefault('app')
} else {
const styledAppDir = blue('App Router')
const { app } = await prompts({
onState: onPromptState,
type: 'toggle',
name: 'app',
message: `Would you like to use ${styledAppDir}? (recommended)`,
initial: getPrefOrDefault('app'),
active: 'Yes',
inactive: 'No',
})
opts.app = Boolean(app)
preferences.app = Boolean(app)
}
opts.app = true
}
// Cache Components is an App Router feature, so only offer it when the App
......
import { mkdir, readFile, writeFile } from 'fs/promises'
import { chmod, mkdir, readFile, writeFile } from 'fs/promises'
import { join } from 'path'
import {
writeAgentFeedbackFiles,
......@@ -8,6 +8,7 @@ import {
import {
resolveNextTgzFilename,
run,
getPreferencesDir,
useTempDir,
projectFilesShouldExist,
projectFilesShouldNotExist,
......@@ -51,7 +52,10 @@ describe('create-next-app', () => {
const Conf = require('next/dist/compiled/conf')
await useTempDir(async (cwd) => {
const conf = new Conf({ projectName: 'create-next-app' })
const conf = new Conf({
projectName: 'create-next-app',
cwd: getPreferencesDir(cwd),
})
conf.clear()
const projectName = 'cache-components'
const res = await run(
......@@ -124,42 +128,50 @@ describe('create-next-app', () => {
await useTempDir(async (cwd) => {
const projectName = 'dir-not-writable'
// Create writable preferences before restricting the app's parent directory.
await mkdir(getPreferencesDir(cwd))
// if the folder isn't able to be write restricted we can't test so skip
if (
await writeFile(join(cwd, 'test'), 'hello')
.then(() => true)
.catch(() => false)
) {
console.warn(
`Test folder is not write restricted skipping write permission test`
)
return
}
try {
await chmod(cwd, 0o500)
const res = await run(
[
projectName,
'--ts',
'--app',
'--eslint',
'--no-tailwind',
'--no-src-dir',
'--no-import-alias',
'--no-react-compiler',
'--no-agents-md',
...(process.env.NEXT_RSPACK ? ['--rspack'] : []),
],
nextTgzFilename,
{
cwd,
reject: false,
// if the folder isn't able to be write restricted we can't test so skip
if (
await writeFile(join(cwd, 'test'), 'hello')
.then(() => true)
.catch(() => false)
) {
console.warn(
`Test folder is not write restricted skipping write permission test`
)
return
}
)
expect(res.stderr).toMatch(expectedErrorMessage)
expect(res.exitCode).toBe(1)
}, 0o500).catch((err) => {
const res = await run(
[
projectName,
'--ts',
'--app',
'--eslint',
'--no-tailwind',
'--no-src-dir',
'--no-import-alias',
'--no-react-compiler',
'--no-agents-md',
...(process.env.NEXT_RSPACK ? ['--rspack'] : []),
],
nextTgzFilename,
{
cwd,
reject: false,
}
)
expect(res.stderr).toMatch(expectedErrorMessage)
expect(res.exitCode).toBe(1)
} finally {
await chmod(cwd, 0o700)
}
}).catch((err) => {
if (!expectedErrorMessage.test(err.message)) {
throw err
}
......
......@@ -7,6 +7,7 @@
import { execSync, spawn, SpawnOptions } from 'child_process'
import { existsSync, readFileSync } from 'fs'
import { join, resolve } from 'path'
import { fileURLToPath } from 'url'
import glob from 'glob'
import Conf from 'next/dist/compiled/conf'
......@@ -25,6 +26,11 @@ import { serializeTestPkgPathsEnv } from './test-pkg-paths'
const cli = require.resolve('create-next-app/dist/index.js')
// Keep saved preferences inside each fixture so concurrent runs cannot reset
// or overwrite one another's settings.
export const getPreferencesDir = (cwd: string | URL = process.cwd()) =>
resolve(cwd instanceof URL ? fileURLToPath(cwd) : cwd, '.create-next-app')
/**
* Run the built version of `create-next-app` with the given arguments.
*/
......@@ -34,7 +40,8 @@ export const createNextApp = (
testVersion?: string,
clearPreferences: boolean = true
) => {
const conf = new Conf({ projectName: 'create-next-app' })
const configDir = getPreferencesDir(options?.cwd)
const conf = new Conf({ projectName: 'create-next-app', cwd: configDir })
if (clearPreferences) {
conf.clear()
}
......@@ -64,7 +71,8 @@ export const createNextApp = (
...options,
env: {
...cloneEnv,
...options.env,
...options?.env,
NEXT_PRIVATE_TEST_CONFIG_DIR: configDir,
},
})
}
......
......@@ -4,6 +4,7 @@ import { join } from 'path'
import stripAnsi from 'strip-ansi'
import {
createNextApp,
getPreferencesDir,
projectFilesShouldExist,
resolveNextTgzFilename,
useTempDir,
......@@ -296,7 +297,10 @@ describe('create-next-app prompts', () => {
const Conf = require('next/dist/compiled/conf')
await useTempDir(async (cwd) => {
const conf = new Conf({ projectName: 'create-next-app' })
const conf = new Conf({
projectName: 'create-next-app',
cwd: getPreferencesDir(cwd),
})
conf.clear()
if (saved !== undefined) {
conf.set('preferences', { cacheComponents: saved })
......@@ -337,7 +341,6 @@ describe('create-next-app prompts', () => {
'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',
......@@ -368,7 +371,10 @@ describe('create-next-app prompts', () => {
await useTempDir(async (cwd) => {
// Manually set preferences to simulate a previous run
const conf = new Conf({ projectName: 'create-next-app' })
const conf = new Conf({
projectName: 'create-next-app',
cwd: getPreferencesDir(cwd),
})
conf.set('preferences', {
typescript: false,
eslint: true,
......@@ -417,7 +423,7 @@ describe('create-next-app prompts', () => {
cwd,
projectName,
files: [
'pages', // pages router (not app)
'app', // always App Router, even with a saved `app: false`
'package.json',
'jsconfig.json', // javascript
],
......
......@@ -3,6 +3,7 @@ import { join } from 'path'
import { spawn } from 'child_process'
import { fetchViaHTTP, findPort, killApp } from 'next-test-utils'
import webdriver from 'next-webdriver'
import { getPreferencesDir } from './lib/utils'
import {
resolveTestPkgPaths,
serializeTestPkgPathsEnv,
......@@ -46,6 +47,7 @@ export const run = async (
options:
| execa.Options
| {
cwd?: string
reject?: boolean
env?: Record<string, string>
}
......@@ -68,6 +70,7 @@ export const run = async (
// when running tests directly (without `run-tests.js`).
NEXT_TEST_PKG_PATHS: serializeTestPkgPathsEnv(),
...options.env,
NEXT_PRIVATE_TEST_CONFIG_DIR: getPreferencesDir(options.cwd),
NEXT_PRIVATE_TEST_VERSION: nextJSVersion,
},
})
......@@ -213,6 +216,7 @@ export async function tryNextDev({
export {
createNextApp,
getPreferencesDir,
projectFilesShouldExist,
projectFilesShouldNotExist,
projectShouldHaveNoGitChanges,
......
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