Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
13 changes: 13 additions & 0 deletions packages/e2e/shared/specs/native-array.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,17 @@ export const testNativeArray = createTest('parseAsNativeArray', ({ path }) => {
cy.get('#add-button').click()
cy.location('search').should('eq', '?test=1&test=2')
})
it('works with the browser back button', () => {
cy.visit(path)
cy.contains('#hydration-marker', 'hydrated').should('be.hidden')
cy.get('#client-name').should('be.empty')
cy.get('#add-button').click()
cy.get('#add-button').click()
cy.get('#add-button').click()
cy.location('search').should('eq', '?test=1&test=2&test=3')
cy.get('#client-name').should('have.text', '1 - 2 - 3')
cy.go('back')
cy.location('search').should('eq', '?test=1&test=2')
cy.get('#client-name').should('have.text', '1 - 2')
})
})
4 changes: 3 additions & 1 deletion packages/e2e/shared/specs/native-array.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import { parseAsInteger, parseAsNativeArrayOf, useQueryState } from 'nuqs'
import { Display } from '../components/display'

export const parser = parseAsNativeArrayOf(parseAsInteger)
export const parser = parseAsNativeArrayOf(parseAsInteger).withOptions({
history: 'push'
})

export function NativeArray() {
const [state, setState] = useQueryState('test', parser)
Expand Down
15 changes: 10 additions & 5 deletions packages/nuqs/src/useQueryStates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,15 @@ export function useQueryStates<KeyMap extends UseQueryStatesKeysMap>(
setInternalState(state)
}
queryRef.current = Object.fromEntries(
Object.values(resolvedUrlKeys).map(urlKey => [
urlKey,
initialSearchParams?.get(urlKey) ?? null
])
Object.entries(resolvedUrlKeys).map(([key, urlKey]) => {
const parser = keyMap[key]
return [
urlKey,
parser?.type === 'multi'
? initialSearchParams?.getAll(urlKey)
: (initialSearchParams?.get(urlKey) ?? null)
]
})
Comment on lines -156 to +164
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here, I’m trying to address a caching issue where we were putting the wrong values into the cachedQuery ( = queryRef.current) cache for multi parsers, because we were only reading the first search param with .get

Now, we are reading with .getAll for multi-parsers

@franky47 I’m not really sure how / where to add a test for this. I did add an e2e test for issue #1162

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah the e2e test should be enough for this, thanks!

)
}

Expand All @@ -182,7 +187,7 @@ export function useQueryStates<KeyMap extends UseQueryStatesKeysMap>(
}
}, [
Object.values(resolvedUrlKeys)
.map(key => `${key}=${initialSearchParams?.get(key)}`)
.map(key => `${key}=${initialSearchParams?.getAll(key)}`)
.join('&'),
JSON.stringify(queuedQueries)
])
Expand Down
Loading