Skip to content

Commit

Permalink
Fix normalization of .rsc extension (#46043)
Browse files Browse the repository at this point in the history
Fixes #45883
Fixes NEXT-541

The reason the url after normalization in middleware ended up being
`/somethingabc=def` instead of `/something?abc=def` is that the regex
matches `?` so the `?` will be removed. Since it's in a capture group
the only change needed was applying that by adding `$1` to the
replacement.

Added some unit tests for this function, going to add additional
integration tests now.

<!--
Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:
-->

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ]
[e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
  • Loading branch information
timneutkens authored Feb 17, 2023
1 parent c16e287 commit 465f984
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
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
}

0 comments on commit 465f984

Please sign in to comment.