Skip to content

Commit

Permalink
Improve errors for invalid component export
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Oct 22, 2022
1 parent 392c4d0 commit 0d0b2bf
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
27 changes: 27 additions & 0 deletions packages/next/server/app-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,33 @@ export async function renderToHTMLOrFlight(
? interopDefault(layoutOrPageMod)
: undefined

if (dev) {
const { isValidElementType } = require('next/dist/compiled/react-is')
if (!isValidElementType(Component)) {
throw new Error(
`The default export is not a React Component in page: "${pathname}"`
)
}

if (ErrorComponent && !isValidElementType(ErrorComponent)) {
throw new Error(
`The default export of error is not a React Component in page: ${segment}`
)
}

if (Loading && !isValidElementType(Loading)) {
throw new Error(
`The default export of loading is not a React Component in ${segment}`
)
}

if (NotFound && !isValidElementType(NotFound)) {
throw new Error(
`The default export of notFound is not a React Component in ${segment}`
)
}
}

// Handle dynamic segment params.
const segmentParam = getDynamicParamFromSegment(segment)
/**
Expand Down
10 changes: 10 additions & 0 deletions test/e2e/app-dir/rsc-errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,14 @@ describe('app dir - rsc basics', () => {
'This module cannot be imported from a Server Component module. It should only be used from a Client Component.'
)
})

it('should error when page component export is not valid', async () => {
const html = await renderViaHTTP(
next.url,
'/server-with-errors/page-export'
)
expect(html).toContain(
'The default export is not a React Component in page: \\"/server-with-errors/page-export\\"'
)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'invalid-page-export'
2 changes: 0 additions & 2 deletions test/lib/react-channel-require-hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ const mod = require('module')
// The value will be '17' or 'exp' to alias the actual react channel
const reactVersion = process.env.__NEXT_REACT_CHANNEL

console.log('reactVersion', reactVersion)

const reactDir = `react-${reactVersion}`
const reactDomDir = `react-dom-${reactVersion}`

Expand Down

0 comments on commit 0d0b2bf

Please sign in to comment.