Fix bug with "Circular Structure" error (#23905)

* 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 https://github.com/blitz-js/babel-plugin-superjson-next/issues/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 https://github.com/blitz-js/babel-plugin-superjson-next/issues/63.

Co-authored-by: JJ Kasper <jj@jjsweb.site>
This commit is contained in:
Simon Knott 2022-02-10 03:28:24 +01:00 committed by GitHub
parent b0205c1c11
commit abf9f75821
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 1 deletions

View file

@ -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`
)

View file

@ -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 <></>
}

View file

@ -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')
})
})