Skip to content

Commit 74e4178

Browse files
committed
refactor: remove ability to call getStaticPaths from app directory pages
1 parent 8271e0a commit 74e4178

File tree

1 file changed

+101
-122
lines changed

1 file changed

+101
-122
lines changed

packages/next/src/build/utils.ts

+101-122
Original file line numberDiff line numberDiff line change
@@ -1227,7 +1227,6 @@ type GenerateParamsResult = {
12271227
config?: AppConfig
12281228
isDynamicSegment?: boolean
12291229
segmentPath: string
1230-
getStaticPaths?: GetStaticPaths
12311230
generateStaticParams?: GenerateStaticParams
12321231
isLayout?: boolean
12331232
}
@@ -1306,7 +1305,7 @@ export async function collectGenerateParams(tree: LoaderTree) {
13061305

13071306
const isDynamicSegment = /^\[.+\]$/.test(page)
13081307

1309-
const { generateStaticParams, getStaticPaths } = mod || {}
1308+
const { generateStaticParams } = mod || {}
13101309

13111310
if (isDynamicSegment && isClientComponent && generateStaticParams) {
13121311
throw new Error(
@@ -1323,20 +1322,14 @@ export async function collectGenerateParams(tree: LoaderTree) {
13231322
isDynamicSegment,
13241323
segmentPath,
13251324
config,
1326-
getStaticPaths: !isClientComponent ? getStaticPaths : undefined,
13271325
generateStaticParams: !isClientComponent
13281326
? generateStaticParams
13291327
: undefined,
13301328
}
13311329

13321330
// If the configuration contributes to the static generation, then add it
13331331
// to the list.
1334-
if (
1335-
result.config ||
1336-
result.generateStaticParams ||
1337-
result.getStaticPaths ||
1338-
isDynamicSegment
1339-
) {
1332+
if (result.config || result.generateStaticParams || isDynamicSegment) {
13401333
generateParams.push(result)
13411334
}
13421335

@@ -1431,145 +1424,131 @@ export async function buildAppStaticPaths({
14311424
},
14321425
},
14331426
async (): Promise<PartialStaticPathsResult> => {
1434-
const pageEntry = generateParams[generateParams.length - 1]
1427+
let hadAllParamsGenerated = false
14351428

1436-
// if the page has legacy getStaticPaths we call it like normal
1437-
if (typeof pageEntry?.getStaticPaths === 'function') {
1438-
return buildStaticPaths({
1439-
page,
1440-
configFileName,
1441-
getStaticPaths: pageEntry.getStaticPaths,
1442-
})
1443-
} else {
1444-
// if generateStaticParams is being used we iterate over them
1445-
// collecting them from each level
1446-
let hadAllParamsGenerated = false
1447-
1448-
const buildParams = async (
1449-
paramsItems: Params[] = [{}],
1450-
idx = 0
1451-
): Promise<Params[]> => {
1452-
const current = generateParams[idx]
1453-
1454-
if (idx === generateParams.length) {
1455-
return paramsItems
1456-
}
1429+
const buildParams = async (
1430+
paramsItems: Params[] = [{}],
1431+
idx = 0
1432+
): Promise<Params[]> => {
1433+
const current = generateParams[idx]
14571434

1458-
if (
1459-
typeof current.generateStaticParams !== 'function' &&
1460-
idx < generateParams.length
1461-
) {
1462-
if (current.isDynamicSegment) {
1463-
// This dynamic level has no generateStaticParams so we change
1464-
// this flag to false, but it could be covered by a later
1465-
// generateStaticParams so it could be set back to true.
1466-
hadAllParamsGenerated = false
1467-
}
1468-
return buildParams(paramsItems, idx + 1)
1435+
if (idx === generateParams.length) {
1436+
return paramsItems
1437+
}
1438+
1439+
if (
1440+
typeof current.generateStaticParams !== 'function' &&
1441+
idx < generateParams.length
1442+
) {
1443+
if (current.isDynamicSegment) {
1444+
// This dynamic level has no generateStaticParams so we change
1445+
// this flag to false, but it could be covered by a later
1446+
// generateStaticParams so it could be set back to true.
1447+
hadAllParamsGenerated = false
14691448
}
1470-
hadAllParamsGenerated = true
1449+
return buildParams(paramsItems, idx + 1)
1450+
}
1451+
hadAllParamsGenerated = true
14711452

1472-
const newParams: Params[] = []
1453+
const newParams: Params[] = []
14731454

1474-
if (current.generateStaticParams) {
1475-
const store = ComponentMod.staticGenerationAsyncStorage.getStore()
1455+
if (current.generateStaticParams) {
1456+
const store = ComponentMod.staticGenerationAsyncStorage.getStore()
14761457

1477-
if (store) {
1478-
if (typeof current?.config?.fetchCache !== 'undefined') {
1479-
store.fetchCache = current.config.fetchCache
1480-
}
1481-
if (typeof current?.config?.revalidate !== 'undefined') {
1482-
store.revalidate = current.config.revalidate
1483-
}
1484-
if (current?.config?.dynamic === 'force-dynamic') {
1485-
store.forceDynamic = true
1486-
}
1458+
if (store) {
1459+
if (typeof current?.config?.fetchCache !== 'undefined') {
1460+
store.fetchCache = current.config.fetchCache
14871461
}
1488-
1489-
for (const params of paramsItems) {
1490-
const result = await current.generateStaticParams({
1491-
params,
1492-
})
1493-
1494-
// TODO: validate the result is valid here or wait for buildStaticPaths to validate?
1495-
for (const item of result) {
1496-
newParams.push({ ...params, ...item })
1497-
}
1462+
if (typeof current?.config?.revalidate !== 'undefined') {
1463+
store.revalidate = current.config.revalidate
1464+
}
1465+
if (current?.config?.dynamic === 'force-dynamic') {
1466+
store.forceDynamic = true
14981467
}
14991468
}
15001469

1501-
if (idx < generateParams.length) {
1502-
return buildParams(newParams, idx + 1)
1470+
for (const params of paramsItems) {
1471+
const result = await current.generateStaticParams({
1472+
params,
1473+
})
1474+
1475+
// TODO: validate the result is valid here or wait for buildStaticPaths to validate?
1476+
for (const item of result) {
1477+
newParams.push({ ...params, ...item })
1478+
}
15031479
}
1480+
}
15041481

1505-
return newParams
1482+
if (idx < generateParams.length) {
1483+
return buildParams(newParams, idx + 1)
15061484
}
15071485

1508-
const builtParams = await buildParams()
1486+
return newParams
1487+
}
15091488

1510-
if (
1511-
generateParams.some(
1512-
(generate) => generate.config?.dynamicParams === true
1513-
) &&
1514-
nextConfigOutput === 'export'
1515-
) {
1516-
throw new Error(
1517-
'"dynamicParams: true" cannot be used with "output: export". See more info here: https://nextjs.org/docs/app/building-your-application/deploying/static-exports'
1518-
)
1519-
}
1489+
const builtParams = await buildParams()
15201490

1521-
// TODO: dynamic params should be allowed to be granular per segment but
1522-
// we need additional information stored/leveraged in the prerender
1523-
// manifest to allow this behavior.
1524-
const dynamicParams = generateParams.every(
1525-
(param) => param.config?.dynamicParams !== false
1491+
if (
1492+
generateParams.some(
1493+
(generate) => generate.config?.dynamicParams === true
1494+
) &&
1495+
nextConfigOutput === 'export'
1496+
) {
1497+
throw new Error(
1498+
'"dynamicParams: true" cannot be used with "output: export". See more info here: https://nextjs.org/docs/app/building-your-application/deploying/static-exports'
15261499
)
1500+
}
15271501

1528-
const isProduction = process.env.NODE_ENV === 'production'
1502+
// TODO: dynamic params should be allowed to be granular per segment but
1503+
// we need additional information stored/leveraged in the prerender
1504+
// manifest to allow this behavior.
1505+
const dynamicParams = generateParams.every(
1506+
(param) => param.config?.dynamicParams !== false
1507+
)
15291508

1530-
const supportsStaticGeneration = hadAllParamsGenerated || isProduction
1509+
const isProduction = process.env.NODE_ENV === 'production'
15311510

1532-
const supportsPPRFallbacks =
1533-
isRoutePPREnabled && isAppPPRFallbacksEnabled
1511+
const supportsStaticGeneration = hadAllParamsGenerated || isProduction
15341512

1535-
const fallbackMode = dynamicParams
1536-
? supportsStaticGeneration
1537-
? supportsPPRFallbacks
1538-
? FallbackMode.PRERENDER
1539-
: FallbackMode.BLOCKING_STATIC_RENDER
1540-
: undefined
1541-
: FallbackMode.NOT_FOUND
1513+
const supportsPPRFallbacks = isRoutePPREnabled && isAppPPRFallbacksEnabled
15421514

1543-
let result: PartialStaticPathsResult = {
1544-
fallbackMode,
1545-
prerenderedRoutes: undefined,
1546-
}
1515+
const fallbackMode = dynamicParams
1516+
? supportsStaticGeneration
1517+
? supportsPPRFallbacks
1518+
? FallbackMode.PRERENDER
1519+
: FallbackMode.BLOCKING_STATIC_RENDER
1520+
: undefined
1521+
: FallbackMode.NOT_FOUND
15471522

1548-
if (hadAllParamsGenerated && fallbackMode) {
1549-
result = await buildStaticPaths({
1550-
staticPathsResult: {
1551-
fallback: fallbackModeToStaticPathsResult(fallbackMode),
1552-
paths: builtParams.map((params) => ({ params })),
1553-
},
1554-
page,
1555-
configFileName,
1556-
appDir: true,
1557-
})
1558-
}
1523+
let result: PartialStaticPathsResult = {
1524+
fallbackMode,
1525+
prerenderedRoutes: undefined,
1526+
}
15591527

1560-
// If the fallback mode is a prerender, we want to include the dynamic
1561-
// route in the prerendered routes too.
1562-
if (isRoutePPREnabled && isAppPPRFallbacksEnabled) {
1563-
result.prerenderedRoutes ??= []
1564-
result.prerenderedRoutes.unshift({
1565-
path: page,
1566-
encoded: page,
1567-
fallbackRouteParams: getParamKeys(page),
1568-
})
1569-
}
1528+
if (hadAllParamsGenerated && fallbackMode) {
1529+
result = await buildStaticPaths({
1530+
staticPathsResult: {
1531+
fallback: fallbackModeToStaticPathsResult(fallbackMode),
1532+
paths: builtParams.map((params) => ({ params })),
1533+
},
1534+
page,
1535+
configFileName,
1536+
appDir: true,
1537+
})
1538+
}
15701539

1571-
return result
1540+
// If the fallback mode is a prerender, we want to include the dynamic
1541+
// route in the prerendered routes too.
1542+
if (isRoutePPREnabled && isAppPPRFallbacksEnabled) {
1543+
result.prerenderedRoutes ??= []
1544+
result.prerenderedRoutes.unshift({
1545+
path: page,
1546+
encoded: page,
1547+
fallbackRouteParams: getParamKeys(page),
1548+
})
15721549
}
1550+
1551+
return result
15731552
}
15741553
)
15751554
}

0 commit comments

Comments
 (0)