Skip to content

Commit

Permalink
fix: redirect chain behavior with nested query params
Browse files Browse the repository at this point in the history
Fixed a bug where nested values would get cut off during redirect chains, or when accessing pages with "?foo=bar?cat=nya syntax". It would previously cut off anything after "?cat" so "=nya" would go missing.

This commit also now handles missing/empty query values and persists the type being used when recursively calling _fly. (Url vs string)
  • Loading branch information
Pkmmte committed Jan 20, 2023
1 parent 1dc9d90 commit 54eaba8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/small-cheetahs-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@waveplay/pilot': patch
---

fix: redirect chain behavior with nested query params
2 changes: 1 addition & 1 deletion packages/pilot/src/api/get-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export async function handleGetProps(req: NextApiRequest, res: NextApiResponse,
query: route.query ?? {},
resolvedUrl: path
})
pilot.log('debug', `API: Loaded props for ${path}:`, props)
pilot.log('debug', `API: Loaded props for ${path}: ${JSON.stringify(props)}`)

// Cache props if this is a static page
if (getPropsType === 'getStaticProps' && props) {
Expand Down
11 changes: 6 additions & 5 deletions packages/pilot/src/client/core/pilot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,12 @@ export class Pilot {
action: async (path: string) => {
// Delegate to NextJS router if one exists; use internal router otherwise
if (this._config.nextRouter) {
// Account for updated path (e.g. hook overrides)
// Account for updated path (e.g. hook overrides, redirects)
if (typeof url !== 'string') {
const splitPath = path.split('?')
// Extract just the pathname from "path" along with any query params
url.pathname = splitPath[0]
url.query = splitPath[1]
const splitPath = path.split('?')
url.pathname = splitPath?.[0]
url.query = splitPath?.[1]
?.split('&')
.reduce((acc, param) => {
const [key, value] = param.split('=')
Expand Down Expand Up @@ -372,7 +372,8 @@ export class Pilot {
// If redirected, recursively call this function again with the new path after notifying hooks
if (result?.redirect) {
this._notify(path, { type: 'redirect' })
return await this._fly(result.redirect, options)
const redirect = typeof url === 'string' ? result.redirect : { ...url, pathname: result.redirect }
return await this._fly(redirect, options)
}
this._currentPage = result?.page
} catch (e) {
Expand Down
7 changes: 4 additions & 3 deletions packages/pilot/src/client/core/radix-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ const parseQuery = (path: string): { [key: string]: any } => {
// Parse out queries!
const queries = path.substring(path.indexOf('?') + 1).split('&')
for (let queryPair of queries) {
const query = queryPair.split('=')
const key = query[0]
result[key] = query[1].split(',') || []
const queryPairSplit = queryPair.indexOf('=')
const key = queryPair.substring(0, queryPairSplit)
const value = queryPair.substring(queryPairSplit + 1)
result[key] = value?.split(',') || []

for (let i = 0; i < result[key].length; i++) {
result[key][i] = decodeURIComponent(result[key][i])
Expand Down

0 comments on commit 54eaba8

Please sign in to comment.