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

Fix normalization of .rsc extension #46043

Merged
merged 2 commits into from
Feb 17, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/next/src/shared/lib/router/utils/app-paths.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { normalizeRscPath } from './app-paths'

describe('normalizeRscPath', () => {
describe('enabled', () => {
it('should normalize url with .rsc', () => {
expect(normalizeRscPath('/test.rsc', true)).toBe('/test')
})
it('should normalize url with .rsc and searchparams', () => {
expect(normalizeRscPath('/test.rsc?abc=def', true)).toBe('/test?abc=def')
})
})
describe('disabled', () => {
it('should normalize url with .rsc', () => {
expect(normalizeRscPath('/test.rsc', false)).toBe('/test.rsc')
})
it('should normalize url with .rsc and searchparams', () => {
expect(normalizeRscPath('/test.rsc?abc=def', false)).toBe(
'/test.rsc?abc=def'
)
})
})
})
12 changes: 11 additions & 1 deletion packages/next/src/shared/lib/router/utils/app-paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ export function normalizeAppPath(route: string) {
)
}

/**
* Strips the `.rsc` extension if it's in the pathname.
* Since this function is used on full urls it checks `?` for searchParams handling.
*/
export function normalizeRscPath(pathname: string, enabled?: boolean) {
return enabled ? pathname.replace(/\.rsc($|\?)/, '') : pathname
return enabled
? pathname.replace(
/\.rsc($|\?)/,
// $1 ensures `?` is preserved
'$1'
)
: pathname
}