Skip to content
This repository was archived by the owner on May 10, 2021. It is now read-only.

Commit e0b2a12

Browse files
committed
Add support for query string parameters
Query string parameters are now passed to the nextRouter Netlify function. This means that they are now accessible in getInitialProps() via the query parameter. Fixes #9.
1 parent f2db852 commit e0b2a12

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

cypress/fixtures/pages/shows/[...params].js

+20-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fetch from 'isomorphic-unfetch'
22
import Error from 'next/error'
33
import Link from 'next/link'
44

5-
const CatchAll = ({ errorCode, show, params }) => {
5+
const CatchAll = ({ errorCode, show, params, queryStringParams }) => {
66

77
// If show item was not found, render 404 page
88
if (errorCode) {
@@ -22,13 +22,28 @@ const CatchAll = ({ errorCode, show, params }) => {
2222
[{index}]: {param}<br/>
2323
</span>
2424
))}
25-
<br/>
25+
</p>
26+
27+
<p>
2628
Refresh the page to see server-side rendering in action.
2729
<br/>
2830
You can also try changing the URL to something random,
2931
such as /shows/{show.id}/whatever/path/you/want
3032
</p>
3133

34+
<p>
35+
You can also use query string parameters, for example:
36+
/shows/{show.id}/whatever/path/you/want?search=dog
37+
<br/>
38+
If you do, they will show up below:
39+
<br/>
40+
{Object.keys(queryStringParams).map(key => (
41+
<span key={key}>
42+
[{key}]: {queryStringParams[key]}<br/>
43+
</span>
44+
))}
45+
</p>
46+
3247
<hr/>
3348

3449
<h1>Show #{show.id}</h1>
@@ -46,8 +61,8 @@ const CatchAll = ({ errorCode, show, params }) => {
4661
}
4762

4863
CatchAll.getInitialProps = async ({ res: req, query }) => {
49-
// Get the params to render
50-
const { params } = query
64+
// Get the params and query parameters to render
65+
const { params, ...queryStringParams } = query
5166

5267
// Get the ID to render
5368
const id = params[0]
@@ -59,7 +74,7 @@ CatchAll.getInitialProps = async ({ res: req, query }) => {
5974
// Set error code if show item could not be found
6075
const errorCode = res.status > 200 ? res.status : false
6176

62-
return { errorCode, show: data, params }
77+
return { errorCode, show: data, params, queryStringParams }
6378
}
6479

6580
export default CatchAll

cypress/integration/default_spec.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -83,28 +83,38 @@ describe('dynamic SSR page', () => {
8383
})
8484

8585
describe('dynamic catch-all SSR page', () => {
86-
it('displays all URL parameters', () => {
87-
cy.visit('/shows/94/this-is-all/being/captured/yay')
86+
it('displays all URL parameters, including query string parameters', () => {
87+
cy.visit('/shows/94/this-is-all/being/captured/yay?search=dog&custom-param=cat')
8888

89+
// path parameters
8990
cy.get('p').should('contain', '[0]: 94')
9091
cy.get('p').should('contain', '[1]: this-is-all')
9192
cy.get('p').should('contain', '[2]: being')
9293
cy.get('p').should('contain', '[3]: captured')
9394
cy.get('p').should('contain', '[4]: yay')
9495

96+
// query string parameters
97+
cy.get('p').should('contain', '[search]: dog')
98+
cy.get('p').should('contain', '[custom-param]: cat')
99+
95100
cy.get('h1').should('contain', 'Show #94')
96101
cy.get('p').should('contain', 'Defiance')
97102
})
98103

99-
it('displays all URL parameters when SSR-ing', () => {
100-
cy.visit('/shows/94/this-is-all/being/captured/yay')
104+
it('displays all URL parameters when SSR-ing, including query string parameters', () => {
105+
cy.visit('/shows/94/this-is-all/being/captured/yay?search=dog&custom-param=cat')
101106

107+
// path parameters
102108
cy.get('p').should('contain', '[0]: 94')
103109
cy.get('p').should('contain', '[1]: this-is-all')
104110
cy.get('p').should('contain', '[2]: being')
105111
cy.get('p').should('contain', '[3]: captured')
106112
cy.get('p').should('contain', '[4]: yay')
107113

114+
// query string parameters
115+
cy.get('p').should('contain', '[search]: dog')
116+
cy.get('p').should('contain', '[custom-param]: cat')
117+
108118
cy.get('h1').should('contain', 'Show #94')
109119
cy.get('p').should('contain', 'Defiance')
110120
})

lib/routerTemplate.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ exports.handler = (event, context, callback) => {
4646
...event,
4747
// Required. Otherwise, compat() will complain
4848
requestContext: {},
49-
// Optional: Pass additional parameters to NextJS
50-
multiValueQueryStringParameters: {}
49+
// Pass query string parameters to NextJS
50+
multiValueQueryStringParameters: event.queryStringParameters
5151
},
5252
context,
5353
callback

0 commit comments

Comments
 (0)