Skip to content

Commit

Permalink
fix: Support ? characters in serializer base
Browse files Browse the repository at this point in the history
Closes #812.
  • Loading branch information
franky47 committed Dec 20, 2024
1 parent f92b26b commit 4da30d6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
13 changes: 13 additions & 0 deletions packages/nuqs/src/serializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,17 @@ describe('serializer', () => {
const result = serialize({ str: 'foo', int: 1, bool: true })
expect(result).toBe('?s=foo&i=1&b=true')
})
it('supports ? in the values', () => {
const serialize = createSerializer(parsers)
const result = serialize({ str: 'foo?bar', int: 1, bool: true })
expect(result).toBe('?str=foo?bar&int=1&bool=true')
})
it('supports & in the base', () => {
// Repro for https://github.com/47ng/nuqs/issues/812
const serialize = createSerializer(parsers)
const result = serialize('https://example.com/path?issue=is?here', {
str: 'foo?bar'
})
expect(result).toBe('https://example.com/path?issue=is?here&str=foo?bar')
})
})
4 changes: 2 additions & 2 deletions packages/nuqs/src/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ function isBase(base: any): base is Base {

function splitBase(base: Base) {
if (typeof base === 'string') {
const [path = '', search] = base.split('?')
return [path, new URLSearchParams(search)] as const
const [path = '', ...search] = base.split('?')
return [path, new URLSearchParams(search.join('?'))] as const
} else if (base instanceof URLSearchParams) {
return ['', new URLSearchParams(base)] as const // Operate on a copy of URLSearchParams, as derived classes may restrict its allowed methods
} else {
Expand Down

0 comments on commit 4da30d6

Please sign in to comment.