Skip to content

Commit

Permalink
feat: safely parse JSON in _app
Browse files Browse the repository at this point in the history
  • Loading branch information
dleard committed Mar 4, 2021
1 parent 00b7d26 commit 04e4f05
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions app/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,25 @@ export default class App extends NextApp<AppProps> {
})
);

// Safely parse from URL query string, returns {} if query string is invalid JSON
const safeParse = (toParse) => {
try {
return JSON.parse(toParse as string);
} catch (e) {
return {};
}
};

// This is part of our query infrastructure.
//
// The relayVars query string in the URL contains all filters to apply to the Relay query variables.
// It allows navigation to keep track of the query, so the form doesn't get reset.
const relayVars = router.query.relayVars
? JSON.parse(String(router.query.relayVars))
? safeParse(router.query.relayVars)
: {};

const pageVars = router.query.pageVars
? JSON.parse(String(router.query.pageVars))
? safeParse(router.query.pageVars)
: {};

return (
Expand Down

0 comments on commit 04e4f05

Please sign in to comment.