Skip to content

Commit

Permalink
Fix multi-value query params being lost (#1150)
Browse files Browse the repository at this point in the history
* Fix multi-value query params being lost

* Update CHANGELOG.md
  • Loading branch information
bendvc authored May 2, 2023
1 parent 00cbf8c commit 580ed58
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/pwa-kit-react-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## v2.8.0-dev (Mar 03, 2023)
- Fix `multi-value` params being lost [#1150](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/1150)
## v2.7.0 (Mar 03, 2023)
## v2.6.0 (Jan 25, 2023)

Expand Down
29 changes: 27 additions & 2 deletions packages/pwa-kit-react-sdk/src/ssr/server/react-rendering.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,31 @@ const logAndFormatError = (err) => {
}
}

// Because multi-value params are not supported in `aws-serverless-express` create a proper
// search string using the `query` property. We pay special attention to the order the params
// as best as we can.
const getLocationSearch = (req) => {
const [_, search] = req.originalUrl.split('?')
const params = new URLSearchParams(search)
const newParams = new URLSearchParams()
const orderedKeys = [...new Set(params.keys())]

// Maintain the original order of the parameters by iterating the
// ordered list of keys, and using the `req.query` object as the source of values.
orderedKeys.forEach((key) => {
const value = req.query[key]
const values = Array.isArray(value) ? value : [value]

values.forEach((v) => {
newParams.append(key, v)
})
})
const searchString = newParams.toString()

// Update the location objects reference.
return searchString ? `?${searchString}` : ''
}

/**
* This is the main react-rendering function for SSR. It is an Express handler.
*
Expand All @@ -93,10 +118,10 @@ export const render = async (req, res, next) => {
const routes = getRoutes(res.locals)
const WrappedApp = routeComponent(App, false, res.locals)

const [pathname, search] = req.originalUrl.split('?')
const [pathname] = req.originalUrl.split('?')
const location = {
pathname,
search: search ? `?${search}` : ''
search: getLocationSearch(req)
}

// Step 1 - Find the match.
Expand Down

0 comments on commit 580ed58

Please sign in to comment.