Skip to content

Commit 488812b

Browse files
committed
feat: support server, cache, root params types
1 parent 851e0c4 commit 488812b

File tree

5 files changed

+329
-1168
lines changed

5 files changed

+329
-1168
lines changed

packages/next/src/build/index.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ import {
223223
createRouteTypesManifest,
224224
writeRouteTypesManifest,
225225
writeValidatorFile,
226+
writeServerTypesFile,
227+
writeCacheLifeTypesFile,
226228
} from '../server/lib/router-utils/route-types-utils'
227229

228230
type Fallback = null | boolean | string
@@ -1340,6 +1342,34 @@ export default async function build(
13401342
layoutRoutes = processLayoutRoutes(mappedAppLayouts, dir)
13411343
}
13421344

1345+
// Collect layout parameters for root params extraction
1346+
// Find the root layout (shortest path with dynamic segments)
1347+
const collectedRootParams: Record<string, string[]> = {}
1348+
1349+
// Find layouts that could be root layouts (have dynamic segments)
1350+
const layoutsWithParams = layoutRoutes
1351+
.map(({ route }) => {
1352+
const foundParams = Array.from(
1353+
route.matchAll(/\[(.*?)\]/g),
1354+
(match) => match[1]
1355+
)
1356+
return { route, params: foundParams }
1357+
})
1358+
.filter(({ params }) => params.length > 0)
1359+
1360+
// Sort by path depth (ascending) to find the shallowest layout with params
1361+
layoutsWithParams.sort((a, b) => {
1362+
const aDepth = a.route.split('/').length
1363+
const bDepth = b.route.split('/').length
1364+
return aDepth - bDepth
1365+
})
1366+
1367+
// The root layout is the shallowest layout with dynamic segments
1368+
if (layoutsWithParams.length > 0) {
1369+
const rootLayout = layoutsWithParams[0]
1370+
collectedRootParams[rootLayout.route] = rootLayout.params
1371+
}
1372+
13431373
const routeTypesManifest = await createRouteTypesManifest({
13441374
dir,
13451375
pageRoutes,
@@ -1352,8 +1382,35 @@ export default async function build(
13521382
rewrites: config.rewrites,
13531383
})
13541384

1385+
// Add collected root params and cache life config to manifest
1386+
routeTypesManifest.collectedRootParams = collectedRootParams
1387+
routeTypesManifest.cacheLifeConfig = config.experimental?.cacheLife
1388+
13551389
await writeRouteTypesManifest(routeTypesManifest, routeTypesFilePath)
13561390
await writeValidatorFile(routeTypesManifest, validatorFilePath)
1391+
1392+
// Generate server types if we have root params
1393+
if (Object.keys(collectedRootParams).length > 0) {
1394+
const serverTypesFilePath = path.join(
1395+
distDir,
1396+
'types',
1397+
'server.d.ts'
1398+
)
1399+
await writeServerTypesFile(routeTypesManifest, serverTypesFilePath)
1400+
}
1401+
1402+
// Generate cache life types if we have cache life config
1403+
if (config.experimental?.cacheLife) {
1404+
const cacheLifeTypesFilePath = path.join(
1405+
distDir,
1406+
'types',
1407+
'cache-life.d.ts'
1408+
)
1409+
await writeCacheLifeTypesFile(
1410+
routeTypesManifest,
1411+
cacheLifeTypesFilePath
1412+
)
1413+
}
13571414
})
13581415

13591416
// Turbopack already handles conflicting app and page routes.

packages/next/src/build/webpack-config.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ import { CopyFilePlugin } from './webpack/plugins/copy-file-plugin'
5151
import { ClientReferenceManifestPlugin } from './webpack/plugins/flight-manifest-plugin'
5252
import { FlightClientEntryPlugin as NextFlightClientEntryPlugin } from './webpack/plugins/flight-client-entry-plugin'
5353
import { RspackFlightClientEntryPlugin } from './webpack/plugins/rspack-flight-client-entry-plugin'
54-
import { NextTypesPlugin } from './webpack/plugins/next-types-plugin'
5554
import type {
5655
Feature,
5756
SWC_TARGET_TRIPLE,
@@ -319,8 +318,6 @@ export default async function getBaseWebpackConfig(
319318
pagesDir,
320319
reactProductionProfiling = false,
321320
rewrites,
322-
originalRewrites,
323-
originalRedirects,
324321
runWebpackSpan,
325322
appDir,
326323
middlewareMatchers,
@@ -389,7 +386,6 @@ export default async function getBaseWebpackConfig(
389386

390387
const hasAppDir = !!appDir
391388
const disableOptimizedLoading = true
392-
const enableTypedRoutes = !!config.experimental.typedRoutes && hasAppDir
393389
const bundledReactChannel = needsExperimentalReact(config)
394390
? '-experimental'
395391
: ''
@@ -2143,20 +2139,6 @@ export default async function getBaseWebpackConfig(
21432139
isEdgeServer,
21442140
encryptionKey,
21452141
})),
2146-
hasAppDir &&
2147-
!isClient &&
2148-
new NextTypesPlugin({
2149-
dir,
2150-
distDir: config.distDir,
2151-
appDir,
2152-
dev,
2153-
isEdgeServer,
2154-
pageExtensions: config.pageExtensions,
2155-
typedRoutes: enableTypedRoutes,
2156-
cacheLifeConfig: config.experimental.cacheLife,
2157-
originalRewrites,
2158-
originalRedirects,
2159-
}),
21602142
!dev &&
21612143
isClient &&
21622144
!!config.experimental.sri?.algorithm &&

0 commit comments

Comments
 (0)