Unverified Commit c970d951 authored by Ben Gubler's avatar Ben Gubler Committed by GitHub

feat: stabilize experimental.typedRoutes flag (#82762)

- Rename `experimental.typedRoutes` to `typedRoutes` 
- Remove old references to `typedRoutes` not working with Turbopack
- Remove unnecessary encoding of `typedRoutes` for bundlers
parent 65cf21c9
...@@ -795,10 +795,7 @@ pub struct ExperimentalConfig { ...@@ -795,10 +795,7 @@ pub struct ExperimentalConfig {
swc_trace_profiling: Option<bool>, swc_trace_profiling: Option<bool>,
/// @internal Used by the Next.js internals only. /// @internal Used by the Next.js internals only.
trust_host_header: Option<bool>, trust_host_header: Option<bool>,
/// Generate Route types and enable type checking for Link and Router.push,
/// etc. This option requires `appDir` to be enabled first.
/// @see [api reference](https://nextjs.org/docs/app/api-reference/next-config-js/typedRoutes)
typed_routes: Option<bool>,
url_imports: Option<serde_json::Value>, url_imports: Option<serde_json::Value>,
view_transition: Option<bool>, view_transition: Option<bool>,
/// This option is to enable running the Webpack build in a worker thread /// This option is to enable running the Webpack build in a worker thread
......
--- ---
title: typedRoutes title: typedRoutes
description: Enable experimental support for statically typed links. description: Enable support for statically typed links.
version: experimental
--- ---
Experimental support for [statically typed links](/docs/app/api-reference/config/typescript#statically-typed-links). This feature requires using the App Router as well as TypeScript in your project. > **Note**: This option has been marked as stable, so you should use `typedRoutes` instead of `experimental.typedRoutes`.
Support for [statically typed links](/docs/app/api-reference/config/typescript#statically-typed-links). This feature requires using TypeScript in your project.
```js filename="next.config.js" ```js filename="next.config.js"
/** @type {import('next').NextConfig} */ /** @type {import('next').NextConfig} */
const nextConfig = { const nextConfig = {
experimental: {
typedRoutes: true, typedRoutes: true,
},
} }
module.exports = nextConfig module.exports = nextConfig
......
...@@ -109,15 +109,13 @@ module.exports = nextConfig ...@@ -109,15 +109,13 @@ module.exports = nextConfig
Next.js can statically type links to prevent typos and other errors when using `next/link`, improving type safety when navigating between pages. Next.js can statically type links to prevent typos and other errors when using `next/link`, improving type safety when navigating between pages.
To opt-into this feature, `experimental.typedRoutes` need to be enabled and the project needs to be using TypeScript. To opt-into this feature, `typedRoutes` need to be enabled and the project needs to be using TypeScript.
```ts filename="next.config.ts" ```ts filename="next.config.ts"
import type { NextConfig } from 'next' import type { NextConfig } from 'next'
const nextConfig: NextConfig = { const nextConfig: NextConfig = {
experimental: {
typedRoutes: true, typedRoutes: true,
},
} }
export default nextConfig export default nextConfig
......
...@@ -2310,7 +2310,6 @@ export default async function getBaseWebpackConfig( ...@@ -2310,7 +2310,6 @@ export default async function getBaseWebpackConfig(
optimizeCss: config.experimental.optimizeCss, optimizeCss: config.experimental.optimizeCss,
nextScriptWorkers: config.experimental.nextScriptWorkers, nextScriptWorkers: config.experimental.nextScriptWorkers,
scrollRestoration: config.experimental.scrollRestoration, scrollRestoration: config.experimental.scrollRestoration,
typedRoutes: config.experimental.typedRoutes,
basePath: config.basePath, basePath: config.basePath,
excludeDefaultMomentLocales: config.excludeDefaultMomentLocales, excludeDefaultMomentLocales: config.excludeDefaultMomentLocales,
assetPrefix: config.assetPrefix, assetPrefix: config.assetPrefix,
......
...@@ -219,7 +219,7 @@ type InternalLinkProps = { ...@@ -219,7 +219,7 @@ type InternalLinkProps = {
// adding this to the publicly exported type currently breaks existing apps // adding this to the publicly exported type currently breaks existing apps
// `RouteInferType` is a stub here to avoid breaking `typedRoutes` when the type // `RouteInferType` is a stub here to avoid breaking `typedRoutes` when the type
// isn't generated yet. It will be replaced when the webpack plugin runs. // isn't generated yet. It will be replaced when type generation runs.
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
export type LinkProps<RouteInferType = any> = InternalLinkProps export type LinkProps<RouteInferType = any> = InternalLinkProps
type LinkPropsRequired = RequiredKeys<LinkProps> type LinkPropsRequired = RequiredKeys<LinkProps>
......
...@@ -44,7 +44,7 @@ type InternalFormProps = { ...@@ -44,7 +44,7 @@ type InternalFormProps = {
} & Omit<HTMLFormProps, 'action' | DisallowedFormProps> } & Omit<HTMLFormProps, 'action' | DisallowedFormProps>
// `RouteInferType` is a stub here to avoid breaking `typedRoutes` when the type // `RouteInferType` is a stub here to avoid breaking `typedRoutes` when the type
// isn't generated yet. It will be replaced when the webpack plugin runs. // isn't generated yet. It will be replaced when type generation runs.
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
export type FormProps<RouteInferType = any> = InternalFormProps export type FormProps<RouteInferType = any> = InternalFormProps
......
...@@ -118,7 +118,7 @@ type InternalLinkProps = { ...@@ -118,7 +118,7 @@ type InternalLinkProps = {
// adding this to the publicly exported type currently breaks existing apps // adding this to the publicly exported type currently breaks existing apps
// `RouteInferType` is a stub here to avoid breaking `typedRoutes` when the type // `RouteInferType` is a stub here to avoid breaking `typedRoutes` when the type
// isn't generated yet. It will be replaced when the webpack plugin runs. // isn't generated yet. It will be replaced when type generation runs.
// WARNING: This should be an interface to prevent TypeScript from inlining it // WARNING: This should be an interface to prevent TypeScript from inlining it
// in declarations of libraries dependending on Next.js. // in declarations of libraries dependending on Next.js.
// Not trivial to reproduce so only convert to an interface when needed. // Not trivial to reproduce so only convert to an interface when needed.
......
...@@ -746,6 +746,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() => ...@@ -746,6 +746,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
tsconfigPath: z.string().min(1).optional(), tsconfigPath: z.string().min(1).optional(),
}) })
.optional(), .optional(),
typedRoutes: z.boolean().optional(),
useFileSystemPublicRoutes: z.boolean().optional(), useFileSystemPublicRoutes: z.boolean().optional(),
// The webpack config type is unknown, use z.any() here // The webpack config type is unknown, use z.any() here
webpack: z.any().nullable().optional(), webpack: z.any().nullable().optional(),
......
...@@ -608,8 +608,9 @@ export interface ExperimentalConfig { ...@@ -608,8 +608,9 @@ export interface ExperimentalConfig {
} }
/** /**
* Generate Route types and enable type checking for Link and Router.push, etc. * Enable type checking for Link and Router.push, etc.
* @see https://nextjs.org/docs/app/api-reference/next-config-js/typedRoutes * @deprecated Use `typedRoutes` instead — this feature is now stable.
* @see https://nextjs.org/docs/app/api-reference/config/typescript#statically-typed-links
*/ */
typedRoutes?: boolean typedRoutes?: boolean
...@@ -999,6 +1000,14 @@ export interface NextConfig extends Record<string, any> { ...@@ -999,6 +1000,14 @@ export interface NextConfig extends Record<string, any> {
*/ */
typescript?: TypeScriptConfig typescript?: TypeScriptConfig
/**
* Enable type checking for Link and Router.push, etc.
* This feature requires TypeScript in your project.
*
* @see [Typed Links documentation](https://nextjs.org/docs/app/api-reference/config/typescript#statically-typed-links)
*/
typedRoutes?: boolean
/** /**
* Headers allow you to set custom HTTP headers for an incoming request path. * Headers allow you to set custom HTTP headers for an incoming request path.
* *
...@@ -1415,6 +1424,7 @@ export const defaultConfig = Object.freeze({ ...@@ -1415,6 +1424,7 @@ export const defaultConfig = Object.freeze({
ignoreBuildErrors: false, ignoreBuildErrors: false,
tsconfigPath: 'tsconfig.json', tsconfigPath: 'tsconfig.json',
}, },
typedRoutes: false,
distDir: '.next', distDir: '.next',
cleanDistDir: true, cleanDistDir: true,
assetPrefix: '', assetPrefix: '',
...@@ -1560,7 +1570,6 @@ export const defaultConfig = Object.freeze({ ...@@ -1560,7 +1570,6 @@ export const defaultConfig = Object.freeze({
amp: undefined, amp: undefined,
urlImports: undefined, urlImports: undefined,
turbo: undefined, turbo: undefined,
typedRoutes: false,
typedEnv: false, typedEnv: false,
clientTraceMetadata: undefined, clientTraceMetadata: undefined,
parallelServerCompiles: false, parallelServerCompiles: false,
......
...@@ -654,6 +654,13 @@ function assignDefaults( ...@@ -654,6 +654,13 @@ function assignDefaults(
configFileName, configFileName,
silent silent
) )
warnOptionHasBeenMovedOutOfExperimental(
result,
'typedRoutes',
'typedRoutes',
configFileName,
silent
)
warnOptionHasBeenMovedOutOfExperimental( warnOptionHasBeenMovedOutOfExperimental(
result, result,
'outputFileTracingRoot', 'outputFileTracingRoot',
......
...@@ -348,7 +348,7 @@ export async function writeRouteTypesManifest( ...@@ -348,7 +348,7 @@ export async function writeRouteTypesManifest(
await fs.promises.writeFile(filePath, generateRouteTypesFile(manifest)) await fs.promises.writeFile(filePath, generateRouteTypesFile(manifest))
// Write the link.d.ts file if typedRoutes is enabled // Write the link.d.ts file if typedRoutes is enabled
if (config.experimental?.typedRoutes === true) { if (config.typedRoutes === true) {
const linkTypesPath = path.join(dirname, 'link.d.ts') const linkTypesPath = path.join(dirname, 'link.d.ts')
await fs.promises.writeFile(linkTypesPath, generateLinkTypesFile(manifest)) await fs.promises.writeFile(linkTypesPath, generateLinkTypesFile(manifest))
} }
......
...@@ -55,7 +55,7 @@ export type { Instrumentation } from './server/instrumentation/types' ...@@ -55,7 +55,7 @@ export type { Instrumentation } from './server/instrumentation/types'
*/ */
// `RouteInferType` is a stub here to avoid breaking `typedRoutes` when the type // `RouteInferType` is a stub here to avoid breaking `typedRoutes` when the type
// isn't generated yet. It will be replaced when the webpack plugin runs. // isn't generated yet. It will be replaced when type generation runs.
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
export type Route<RouteInferType = any> = string & {} export type Route<RouteInferType = any> = string & {}
......
...@@ -2,9 +2,7 @@ ...@@ -2,9 +2,7 @@
* @type {import('next').NextConfig} * @type {import('next').NextConfig}
*/ */
const nextConfig = { const nextConfig = {
experimental: {
typedRoutes: true, typedRoutes: true,
},
async redirects() { async redirects() {
return [ return [
{ {
......
...@@ -3,9 +3,7 @@ import createMdx from '@next/mdx' ...@@ -3,9 +3,7 @@ import createMdx from '@next/mdx'
const withMdx = createMdx() const withMdx = createMdx()
export default withMdx({ export default withMdx({
experimental: {
typedRoutes: true, typedRoutes: true,
},
pageExtensions: ['js', 'jsx', 'ts', 'tsx', 'mdx'], pageExtensions: ['js', 'jsx', 'ts', 'tsx', 'mdx'],
......
/** @type {import('next').NextConfig} */ /** @type {import('next').NextConfig} */
module.exports = { module.exports = {
experimental: {
typedRoutes: true, typedRoutes: true,
experimental: {
webpackBuildWorker: true, webpackBuildWorker: true,
}, },
} }
/** @type {import('next').NextConfig} */ /** @type {import('next').NextConfig} */
module.exports = { module.exports = {
experimental: {
typedRoutes: true, typedRoutes: true,
experimental: {
parallelServerBuildTraces: true, parallelServerBuildTraces: true,
webpackBuildWorker: true, webpackBuildWorker: true,
}, },
......
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