Skip to content

Commit

Permalink
Fix bug with "Circular Structure" error (#23905)
Browse files Browse the repository at this point in the history
* Fix bug with "Circular Structure" error

Since `-1` is truthy, every JSON.stringify error is mistaken to be `circular structure`. This commit fixes that behaviour, so that other errors like `Do not know how to serialize Bigint` (see blitz-js/babel-plugin-superjson-next#63) aren't swallowed.

* Add integration test

This may be thought of as being a pretty contrived example, but it's exactly what happened in blitz-js/babel-plugin-superjson-next#63.

Co-authored-by: JJ Kasper <jj@jjsweb.site>
  • Loading branch information
Skn0tt and ijjk authored Feb 10, 2022
1 parent b0205c1 commit abf9f75
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/next/pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ export class NextScript extends Component<OriginProps> {

return htmlEscapeJsonString(data)
} catch (err) {
if (isError(err) && err.message.indexOf('circular structure')) {
if (isError(err) && err.message.indexOf('circular structure') !== -1) {
throw new Error(
`Circular structure in "getInitialProps" result of page "${__NEXT_DATA__.page}". https://nextjs.org/docs/messages/circular-structure`
)
Expand Down
16 changes: 16 additions & 0 deletions test/integration/json-serialize-original-error/pages/bigint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export async function getStaticProps() {
return {
props: {
topics: [
{
number: '22',
},
],
},
}
}

export default function Repro(props) {
props.topics[0].number = 22n // basically what happened in https://github.com/blitz-js/babel-plugin-superjson-next/issues/63
return <></>
}
15 changes: 15 additions & 0 deletions test/integration/json-serialize-original-error/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint-env jest */
import { nextBuild } from 'next-test-utils'
import { join } from 'path'

jest.setTimeout(1000 * 60 * 2)

const appDir = join(__dirname, '..')

describe('JSON Serialization', () => {
test('should fail with original error', async () => {
const { code, stderr } = await nextBuild(appDir, [], { stderr: true })
expect(code).toBe(1)
expect(stderr).toContain('Do not know how to serialize a BigInt')
})
})

0 comments on commit abf9f75

Please sign in to comment.