-
Notifications
You must be signed in to change notification settings - Fork 27.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added support for prefetch segments when deployed
- Loading branch information
Showing
15 changed files
with
282 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
packages/next/src/server/lib/router-utils/build-prefetch-segment-data-route.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { buildPrefetchSegmentDataRoute } from './build-prefetch-segment-data-route' | ||
|
||
describe('buildPrefetchSegmentDataRoute', () => { | ||
it('should build a prefetch segment data route', () => { | ||
const route = buildPrefetchSegmentDataRoute( | ||
'/blog/[...slug]', | ||
'/$c$slug$[slug]/__PAGE__' | ||
) | ||
|
||
expect(route).toMatchInlineSnapshot(` | ||
{ | ||
"destination": "/blog/[...slug].segments/$c$slug$[slug]/__PAGE__.segment.rsc", | ||
"source": "^/blog/(?<nxtPslug>.+?)\\.segments/\\$c\\$slug\\$\\k<nxtPslug>/__PAGE__\\.segment\\.rsc$", | ||
} | ||
`) | ||
}) | ||
}) |
39 changes: 39 additions & 0 deletions
39
packages/next/src/server/lib/router-utils/build-prefetch-segment-data-route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import path from '../../../shared/lib/isomorphic/path' | ||
import { normalizePagePath } from '../../../shared/lib/page-path/normalize-page-path' | ||
import { getNamedRouteRegex } from '../../../shared/lib/router/utils/route-regex' | ||
import { | ||
RSC_SEGMENT_SUFFIX, | ||
RSC_SEGMENTS_DIR_SUFFIX, | ||
} from '../../../lib/constants' | ||
|
||
export const SEGMENT_PATH_KEY = 'nextSegmentPath' | ||
|
||
export type PrefetchSegmentDataRoute = { | ||
source: string | ||
destination: string | ||
} | ||
|
||
export function buildPrefetchSegmentDataRoute( | ||
page: string, | ||
segmentPath: string | ||
): PrefetchSegmentDataRoute { | ||
const pagePath = normalizePagePath(page) | ||
|
||
const destination = path.posix.join( | ||
`${pagePath}${RSC_SEGMENTS_DIR_SUFFIX}`, | ||
`${segmentPath}${RSC_SEGMENT_SUFFIX}` | ||
) | ||
|
||
const { namedRegex } = getNamedRouteRegex(destination, { | ||
prefixRouteKeys: true, | ||
includePrefix: true, | ||
includeSuffix: true, | ||
excludeOptionalTrailingSlash: true, | ||
backreferenceDuplicateKeys: true, | ||
}) | ||
|
||
return { | ||
destination, | ||
source: namedRegex, | ||
} | ||
} |
Oops, something went wrong.