Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: simplify getShortDynamicParamType on app-render #52355

Merged
merged 3 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}