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

Improve errors for invalid component export #41657

Merged
merged 5 commits into from Oct 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 27 additions & 0 deletions packages/next/server/app-render.tsx
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
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\\"'
)
})
})
@@ -0,0 +1 @@
export default 'invalid-page-export'
2 changes: 0 additions & 2 deletions test/lib/react-channel-require-hook.js
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