Skip to content

Commit

Permalink
perf: simplify getShortDynamicParamType on app-render
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Jul 6, 2023
1 parent f0fc83b commit bc190bd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
7 changes: 5 additions & 2 deletions packages/next/src/server/app-render/app-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ import { interopDefault } from './interop-default'
import { preloadComponent } from './preload-component'
import { FlightRenderResult } from './flight-render-result'
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 { getCssInlinedLinkTags } from './get-css-inlined-link-tags'
import { getPreloadableFonts } from './get-preloadable-fonts'
Expand Down Expand Up @@ -324,7 +327,7 @@ export async function renderToHTMLOrFlight(
if (!value) {
// Handle case where optional catchall does not have a value, e.g. `/dashboard/[...slug]` when requesting `/dashboard`
if (segmentParam.type === 'optional-catchall') {
const type = getShortDynamicParamType(segmentParam.type)
const type = dynamicParamTypes[segmentParam.type]
return {
param: key,
value: null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
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.
*/
export function getShortDynamicParamType(
type: DynamicParamTypes
): DynamicParamTypesShort {
switch (type) {
case 'catchall':
return 'c'
case 'optional-catchall':
return 'oc'
case 'dynamic':
return 'd'
default:
throw new Error('Unknown dynamic param type')
const short = dynamicParamTypes[type]
if (!short) {
throw new Error('Unknown dynamic param type')
}
return short
}

0 comments on commit bc190bd

Please sign in to comment.