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:
parent
b0205c1c11
commit
abf9f75821
3 changed files with 32 additions and 1 deletions
|
@ -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`
|
||||
)
|
||||
|
|
|
@ -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 <></>
|
||||
}
|
|
@ -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')
|
||||
})
|
||||
})
|
Loading…
Reference in a new issue