Unverified Commit c3e6680b authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

perf(build): optimize buildAppStaticPaths performance and add helper function (#81386)

### What?

Optimizes the `buildAppStaticPaths` function performance and extracts a
helper function `calculateFallbackMode` to reduce code duplication.

### Why?

The `buildAppStaticPaths` function had several performance bottlenecks:

1. **Repeated regex pattern compilation**: Route parameter patterns were
being compiled inside the loop for every route, causing unnecessary
overhead
2. **Inefficient root parameter lookups**: Using `Array.includes()` for
root parameter checks resulted in O(n) lookups for each parameter
3. **Duplicated fallback mode logic**: The same fallback mode
calculation was repeated in multiple places, making the code harder to
maintain and prone to inconsistencies

These inefficiencies became more apparent when processing large numbers
of routes with complex parameter structures.

### How?

**Performance optimizations:**
- Pre-compile regex patterns for route parameters outside the loop and
store them in a Map for O(1) lookups
- Convert `rootParamKeys` to a Set for O(1) lookup time instead of O(n)
array searches
- Optimize the fallback parameter collection loop to break early and
avoid redundant iterations

**Code organization:**
- Extract `calculateFallbackMode` helper function to eliminate code
duplication
- Add comprehensive tests for the new helper function to ensure
correctness
- Simplify the main loop logic by using the pre-compiled patterns and
optimized data structures
parent a3f031d8
...@@ -2,7 +2,8 @@ import { FallbackMode } from '../../lib/fallback' ...@@ -2,7 +2,8 @@ import { FallbackMode } from '../../lib/fallback'
import type { Params } from '../../server/request/params' import type { Params } from '../../server/request/params'
import { import {
assignErrorIfEmpty, assignErrorIfEmpty,
generateParamPrefixCombinations, generateAllParamCombinations,
calculateFallbackMode,
filterUniqueParams, filterUniqueParams,
generateRouteStaticParams, generateRouteStaticParams,
} from './app' } from './app'
...@@ -356,7 +357,7 @@ describe('generateParamPrefixCombinations', () => { ...@@ -356,7 +357,7 @@ describe('generateParamPrefixCombinations', () => {
{ id: '2', name: 'test' }, { id: '2', name: 'test' },
] ]
const unique = generateParamPrefixCombinations(['id'], params, []) const unique = generateAllParamCombinations(['id'], params, [])
expect(unique).toEqual([{ id: '1' }, { id: '2' }]) expect(unique).toEqual([{ id: '1' }, { id: '2' }])
}) })
...@@ -369,11 +370,7 @@ describe('generateParamPrefixCombinations', () => { ...@@ -369,11 +370,7 @@ describe('generateParamPrefixCombinations', () => {
{ lang: 'fr', region: 'CA', page: 'about' }, { lang: 'fr', region: 'CA', page: 'about' },
] ]
const unique = generateParamPrefixCombinations( const unique = generateAllParamCombinations(['lang', 'region'], params, [])
['lang', 'region'],
params,
[]
)
expect(unique).toEqual([ expect(unique).toEqual([
{ lang: 'en' }, { lang: 'en' },
...@@ -386,20 +383,20 @@ describe('generateParamPrefixCombinations', () => { ...@@ -386,20 +383,20 @@ describe('generateParamPrefixCombinations', () => {
it('should handle parameter value collisions', () => { it('should handle parameter value collisions', () => {
const params = [{ slug: ['foo', 'bar'] }, { slug: 'foo,bar' }] const params = [{ slug: ['foo', 'bar'] }, { slug: 'foo,bar' }]
const unique = generateParamPrefixCombinations(['slug'], params, []) const unique = generateAllParamCombinations(['slug'], params, [])
expect(unique).toEqual([{ slug: ['foo', 'bar'] }, { slug: 'foo,bar' }]) expect(unique).toEqual([{ slug: ['foo', 'bar'] }, { slug: 'foo,bar' }])
}) })
it('should handle empty inputs', () => { it('should handle empty inputs', () => {
// Empty routeParamKeys // Empty routeParamKeys
expect(generateParamPrefixCombinations([], [{ id: '1' }], [])).toEqual([]) expect(generateAllParamCombinations([], [{ id: '1' }], [])).toEqual([])
// Empty routeParams // Empty routeParams
expect(generateParamPrefixCombinations(['id'], [], [])).toEqual([]) expect(generateAllParamCombinations(['id'], [], [])).toEqual([])
// Both empty // Both empty
expect(generateParamPrefixCombinations([], [], [])).toEqual([]) expect(generateAllParamCombinations([], [], [])).toEqual([])
}) })
it('should handle undefined parameters', () => { it('should handle undefined parameters', () => {
...@@ -409,7 +406,7 @@ describe('generateParamPrefixCombinations', () => { ...@@ -409,7 +406,7 @@ describe('generateParamPrefixCombinations', () => {
{ id: '3' }, // missing name key { id: '3' }, // missing name key
] ]
const unique = generateParamPrefixCombinations(['id', 'name'], params, []) const unique = generateAllParamCombinations(['id', 'name'], params, [])
expect(unique).toEqual([ expect(unique).toEqual([
{ id: '1' }, { id: '1' },
...@@ -426,7 +423,7 @@ describe('generateParamPrefixCombinations', () => { ...@@ -426,7 +423,7 @@ describe('generateParamPrefixCombinations', () => {
{ lang: 'fr' }, // missing region and category { lang: 'fr' }, // missing region and category
] ]
const unique = generateParamPrefixCombinations( const unique = generateAllParamCombinations(
['lang', 'region', 'category'], ['lang', 'region', 'category'],
params, params,
[] []
...@@ -450,7 +447,7 @@ describe('generateParamPrefixCombinations', () => { ...@@ -450,7 +447,7 @@ describe('generateParamPrefixCombinations', () => {
{ slug: 'U:undefined' }, // String that looks like undefined prefix { slug: 'U:undefined' }, // String that looks like undefined prefix
] ]
const unique = generateParamPrefixCombinations(['slug'], params, []) const unique = generateAllParamCombinations(['slug'], params, [])
expect(unique).toEqual([ expect(unique).toEqual([
{ slug: ['foo', 'bar'] }, { slug: ['foo', 'bar'] },
...@@ -468,7 +465,7 @@ describe('generateParamPrefixCombinations', () => { ...@@ -468,7 +465,7 @@ describe('generateParamPrefixCombinations', () => {
{ slug: ['foo', 'bar|baz'] }, // Array with pipe in element { slug: ['foo', 'bar|baz'] }, // Array with pipe in element
] ]
const unique = generateParamPrefixCombinations(['slug'], params, []) const unique = generateAllParamCombinations(['slug'], params, [])
expect(unique).toEqual([{ slug: 'foo|bar' }, { slug: ['foo', 'bar|baz'] }]) expect(unique).toEqual([{ slug: 'foo|bar' }, { slug: ['foo', 'bar|baz'] }])
}) })
...@@ -480,7 +477,7 @@ describe('generateParamPrefixCombinations', () => { ...@@ -480,7 +477,7 @@ describe('generateParamPrefixCombinations', () => {
{ a: '1', b: '2', c: '3', d: '7' }, { a: '1', b: '2', c: '3', d: '7' },
] ]
const unique = generateParamPrefixCombinations( const unique = generateAllParamCombinations(
['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'],
params, params,
[] []
...@@ -505,7 +502,7 @@ describe('generateParamPrefixCombinations', () => { ...@@ -505,7 +502,7 @@ describe('generateParamPrefixCombinations', () => {
{ lang: 'fr', region: 'CA', slug: 'about' }, { lang: 'fr', region: 'CA', slug: 'about' },
] ]
const unique = generateParamPrefixCombinations( const unique = generateAllParamCombinations(
['lang', 'region', 'slug'], ['lang', 'region', 'slug'],
params, params,
['lang', 'region'] // Root params ['lang', 'region'] // Root params
...@@ -529,7 +526,7 @@ describe('generateParamPrefixCombinations', () => { ...@@ -529,7 +526,7 @@ describe('generateParamPrefixCombinations', () => {
{ category: 'sports', slug: 'news' }, { category: 'sports', slug: 'news' },
] ]
const unique = generateParamPrefixCombinations( const unique = generateAllParamCombinations(
['category', 'slug'], ['category', 'slug'],
params, params,
[] // No root params [] // No root params
...@@ -552,7 +549,7 @@ describe('generateParamPrefixCombinations', () => { ...@@ -552,7 +549,7 @@ describe('generateParamPrefixCombinations', () => {
{ lang: 'fr', page: 'home' }, { lang: 'fr', page: 'home' },
] ]
const unique = generateParamPrefixCombinations( const unique = generateAllParamCombinations(
['lang', 'page'], ['lang', 'page'],
params, params,
['lang'] // Single root param ['lang'] // Single root param
...@@ -575,7 +572,7 @@ describe('generateParamPrefixCombinations', () => { ...@@ -575,7 +572,7 @@ describe('generateParamPrefixCombinations', () => {
{ page: 'contact' }, // Missing lang root param { page: 'contact' }, // Missing lang root param
] ]
const unique = generateParamPrefixCombinations( const unique = generateAllParamCombinations(
['lang', 'page'], ['lang', 'page'],
params, params,
['lang'] // Root param ['lang'] // Root param
...@@ -596,7 +593,7 @@ describe('generateParamPrefixCombinations', () => { ...@@ -596,7 +593,7 @@ describe('generateParamPrefixCombinations', () => {
{ category: 'sports', slug: 'news' }, { category: 'sports', slug: 'news' },
] ]
const unique = generateParamPrefixCombinations( const unique = generateAllParamCombinations(
['category', 'slug'], ['category', 'slug'],
params, params,
['lang', 'region'] // Root params not in route params ['lang', 'region'] // Root params not in route params
...@@ -620,7 +617,7 @@ describe('generateParamPrefixCombinations', () => { ...@@ -620,7 +617,7 @@ describe('generateParamPrefixCombinations', () => {
{ lang: 'en', locale: 'us' }, // Missing slug parameter { lang: 'en', locale: 'us' }, // Missing slug parameter
] ]
const unique = generateParamPrefixCombinations( const unique = generateAllParamCombinations(
['lang', 'locale', 'slug'], // All route params ['lang', 'locale', 'slug'], // All route params
params, params,
['lang', 'locale'] // Root params ['lang', 'locale'] // Root params
...@@ -637,7 +634,7 @@ describe('generateParamPrefixCombinations', () => { ...@@ -637,7 +634,7 @@ describe('generateParamPrefixCombinations', () => {
// This might be what's happening for the [slug] route // This might be what's happening for the [slug] route
const params: Params[] = [] // No generateStaticParams results const params: Params[] = [] // No generateStaticParams results
const unique = generateParamPrefixCombinations( const unique = generateAllParamCombinations(
['lang', 'locale', 'slug'], // All route params ['lang', 'locale', 'slug'], // All route params
params, params,
['lang', 'locale'] // Root params ['lang', 'locale'] // Root params
...@@ -1027,3 +1024,73 @@ describe('generateRouteStaticParams', () => { ...@@ -1027,3 +1024,73 @@ describe('generateRouteStaticParams', () => {
}) })
}) })
}) })
describe('calculateFallbackMode', () => {
it('should return NOT_FOUND when dynamic params are disabled', () => {
const result = calculateFallbackMode(false, [], FallbackMode.PRERENDER)
expect(result).toBe(FallbackMode.NOT_FOUND)
})
it('should return NOT_FOUND when dynamic params are disabled regardless of root params', () => {
const result = calculateFallbackMode(
false,
['rootParam'],
FallbackMode.BLOCKING_STATIC_RENDER
)
expect(result).toBe(FallbackMode.NOT_FOUND)
})
it('should return BLOCKING_STATIC_RENDER when dynamic params are enabled and root params exist', () => {
const result = calculateFallbackMode(
true,
['rootParam1', 'rootParam2'],
FallbackMode.PRERENDER
)
expect(result).toBe(FallbackMode.BLOCKING_STATIC_RENDER)
})
it('should return base fallback mode when dynamic params are enabled and no root params', () => {
const result = calculateFallbackMode(true, [], FallbackMode.PRERENDER)
expect(result).toBe(FallbackMode.PRERENDER)
})
it('should return base fallback mode when dynamic params are enabled and empty root params', () => {
const result = calculateFallbackMode(
true,
[],
FallbackMode.BLOCKING_STATIC_RENDER
)
expect(result).toBe(FallbackMode.BLOCKING_STATIC_RENDER)
})
it('should return NOT_FOUND when dynamic params are enabled but no base fallback mode provided', () => {
const result = calculateFallbackMode(true, [], undefined)
expect(result).toBe(FallbackMode.NOT_FOUND)
})
it('should prioritize root params over base fallback mode', () => {
const result = calculateFallbackMode(
true,
['rootParam'],
FallbackMode.PRERENDER
)
expect(result).toBe(FallbackMode.BLOCKING_STATIC_RENDER)
})
it('should handle single root param correctly', () => {
const result = calculateFallbackMode(
true,
['singleParam'],
FallbackMode.PRERENDER
)
expect(result).toBe(FallbackMode.BLOCKING_STATIC_RENDER)
})
})
This diff is collapsed.
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