Unverified Commit eb3d7487 authored by Yagiz Nizipli's avatar Yagiz Nizipli Committed by GitHub

perf: simplify getShortDynamicParamType on app-render (#52355)

Removes the unnecessary switch case statement and provide a fast path for `optional-catchall` edge case.
parent 27217146
...@@ -56,7 +56,10 @@ import { interopDefault } from './interop-default' ...@@ -56,7 +56,10 @@ import { interopDefault } from './interop-default'
import { preloadComponent } from './preload-component' import { preloadComponent } from './preload-component'
import { FlightRenderResult } from './flight-render-result' import { FlightRenderResult } from './flight-render-result'
import { createErrorHandler } from './create-error-handler' import { createErrorHandler } from './create-error-handler'
import { getShortDynamicParamType } from './get-short-dynamic-param-type' import {
getShortDynamicParamType,
dynamicParamTypes,
} from './get-short-dynamic-param-type'
import { getSegmentParam } from './get-segment-param' import { getSegmentParam } from './get-segment-param'
import { getCssInlinedLinkTags } from './get-css-inlined-link-tags' import { getCssInlinedLinkTags } from './get-css-inlined-link-tags'
import { getPreloadableFonts } from './get-preloadable-fonts' import { getPreloadableFonts } from './get-preloadable-fonts'
...@@ -324,7 +327,7 @@ export async function renderToHTMLOrFlight( ...@@ -324,7 +327,7 @@ export async function renderToHTMLOrFlight(
if (!value) { if (!value) {
// Handle case where optional catchall does not have a value, e.g. `/dashboard/[...slug]` when requesting `/dashboard` // Handle case where optional catchall does not have a value, e.g. `/dashboard/[...slug]` when requesting `/dashboard`
if (segmentParam.type === 'optional-catchall') { if (segmentParam.type === 'optional-catchall') {
const type = getShortDynamicParamType(segmentParam.type) const type = dynamicParamTypes[segmentParam.type]
return { return {
param: key, param: key,
value: null, value: null,
......
import { DynamicParamTypes, DynamicParamTypesShort } from './types' import { DynamicParamTypes, DynamicParamTypesShort } from './types'
export const dynamicParamTypes: Record<
DynamicParamTypes,
DynamicParamTypesShort
> = {
catchall: 'c',
'optional-catchall': 'oc',
dynamic: 'd',
}
/** /**
* Shorten the dynamic param in order to make it smaller when transmitted to the browser. * Shorten the dynamic param in order to make it smaller when transmitted to the browser.
*/ */
export function getShortDynamicParamType( export function getShortDynamicParamType(
type: DynamicParamTypes type: DynamicParamTypes
): DynamicParamTypesShort { ): DynamicParamTypesShort {
switch (type) { const short = dynamicParamTypes[type]
case 'catchall': if (!short) {
return 'c' throw new Error('Unknown dynamic param type')
case 'optional-catchall':
return 'oc'
case 'dynamic':
return 'd'
default:
throw new Error('Unknown dynamic param type')
} }
return short
} }
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