Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug with "Circular Structure" error #23905

Merged
merged 4 commits into from Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/next/pages/_document.tsx
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
@@ -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
Copy link
Contributor Author

@Skn0tt Skn0tt Apr 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAICT, this error really only occurs when props are mutated during rendering. Normal JSON-misuse (e.g. returning bigints from getStaticProps) is catched before by custom Next.js logic.
Mutating during render is a big no-no, so errors that aren't about "circular structure" could even be used to indicate the mistake and point towards the React docs.

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