Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
f59543e2
Unverified
Commit
f59543e2
authored
Aug 25, 2025
by
Ben Gubler
Committed by
GitHub
Aug 25, 2025
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[backport] fix: typesafe linking to route handlers and pages API routes (#82858) (#83025)
parent
c8cf55f4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
25 additions
and
8 deletions
+25
-8
typegen.ts
packages/next/src/server/lib/router-utils/typegen.ts
+13
-7
route.ts
test/e2e/app-dir/typed-routes/app/api-test/route.ts
+5
-0
typed-links.test.ts
test/e2e/app-dir/typed-routes/typed-links.test.ts
+6
-0
typed-routes.test.ts
test/e2e/app-dir/typed-routes/typed-routes.test.ts
+1
-1
No files found.
packages/next/src/server/lib/router-utils/typegen.ts
View file @
f59543e2
...
@@ -201,18 +201,24 @@ export function generateLinkTypesFile(
...
@@ -201,18 +201,24 @@ export function generateLinkTypesFile(
routesManifest
:
RouteTypesManifest
routesManifest
:
RouteTypesManifest
):
string
{
):
string
{
// Generate serialized static and dynamic routes for the internal namespace
// Generate serialized static and dynamic routes for the internal namespace
const
allRoutes
=
{
// Build a unified set of routes across app/pages/redirect/rewrite as well as
...
routesManifest
.
appRoutes
,
// app route handlers and Pages Router API routes.
...
routesManifest
.
pageRoutes
,
const
allRoutesSet
=
new
Set
<
string
>
([
...
routesManifest
.
redirectRoutes
,
...
Object
.
keys
(
routesManifest
.
appRoutes
),
...
routesManifest
.
rewriteRoutes
,
...
Object
.
keys
(
routesManifest
.
pageRoutes
),
}
...
Object
.
keys
(
routesManifest
.
redirectRoutes
),
...
Object
.
keys
(
routesManifest
.
rewriteRoutes
),
// Allow linking to App Route Handlers (e.g. `/logout/route.ts`)
...
Object
.
keys
(
routesManifest
.
appRouteHandlerRoutes
),
// Allow linking to Pages Router API routes (e.g. `/api/*`)
...
Array
.
from
(
routesManifest
.
pageApiRoutes
),
])
const
staticRouteTypes
:
string
[]
=
[]
const
staticRouteTypes
:
string
[]
=
[]
const
dynamicRouteTypes
:
string
[]
=
[]
const
dynamicRouteTypes
:
string
[]
=
[]
// Process each route using the same logic as the plugin
// Process each route using the same logic as the plugin
for
(
const
route
of
Object
.
keys
(
allRoutes
)
)
{
for
(
const
route
of
allRoutesSet
)
{
const
{
isDynamic
,
routeType
}
=
formatRouteToRouteType
(
route
)
const
{
isDynamic
,
routeType
}
=
formatRouteToRouteType
(
route
)
if
(
isDynamic
)
{
if
(
isDynamic
)
{
dynamicRouteTypes
.
push
(
routeType
)
dynamicRouteTypes
.
push
(
routeType
)
...
...
test/e2e/app-dir/typed-routes/app/api-test/route.ts
0 → 100644
View file @
f59543e2
import
{
NextResponse
}
from
'next/server'
export
function
GET
()
{
return
NextResponse
.
json
({
message
:
'Hello, world!'
})
}
test/e2e/app-dir/typed-routes/typed-links.test.ts
View file @
f59543e2
...
@@ -15,6 +15,12 @@ describe('typed-links', () => {
...
@@ -15,6 +15,12 @@ describe('typed-links', () => {
expect
(
dts
).
toContain
(
`declare module 'next/link'`
)
expect
(
dts
).
toContain
(
`declare module 'next/link'`
)
})
})
it
(
'should include handler route from app/api-test/route.ts in generated link route definitions'
,
async
()
=>
{
const
dts
=
await
next
.
readFile
(
'.next/types/link.d.ts'
)
// Ensure the app route handler at app/api-test/route.ts ("/api-test") is present
expect
(
dts
).
toContain
(
'`/api-test`'
)
})
if
(
isNextStart
)
{
if
(
isNextStart
)
{
it
(
'should pass type checking with valid routes'
,
async
()
=>
{
it
(
'should pass type checking with valid routes'
,
async
()
=>
{
await
next
.
stop
()
await
next
.
stop
()
...
...
test/e2e/app-dir/typed-routes/typed-routes.test.ts
View file @
f59543e2
...
@@ -2,7 +2,7 @@ import { nextTestSetup } from 'e2e-utils'
...
@@ -2,7 +2,7 @@ import { nextTestSetup } from 'e2e-utils'
const
expectedDts
=
`
const
expectedDts
=
`
type AppRoutes = "/" | "/_shop/[[...category]]" | "/dashboard" | "/dashboard/settings" | "/docs/[...slug]" | "/gallery/photo/[id]" | "/project/[slug]"
type AppRoutes = "/" | "/_shop/[[...category]]" | "/dashboard" | "/dashboard/settings" | "/docs/[...slug]" | "/gallery/photo/[id]" | "/project/[slug]"
type AppRouteHandlerRoutes = "/api/docs/[...slug]" | "/api/shop/[[...category]]" | "/api/users/[id]"
type AppRouteHandlerRoutes = "/api
-test" | "/api
/docs/[...slug]" | "/api/shop/[[...category]]" | "/api/users/[id]"
type PageRoutes = "/about" | "/users/[id]"
type PageRoutes = "/about" | "/users/[id]"
type LayoutRoutes = "/" | "/dashboard"
type LayoutRoutes = "/" | "/dashboard"
type RedirectRoutes = "/blog/[category]/[[...slug]]" | "/project/[slug]"
type RedirectRoutes = "/blog/[category]/[[...slug]]" | "/project/[slug]"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment