Unverified Commit a15897be authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

[errors]: default export error should distinguish between layout/page (#76294)

When you forget to export a component in the layout, the error message
points you to the page component, which is confusing.

This ensures the error points to the module that's missing the export. 

Closes NDX-874
parent 0b136b27
......@@ -178,7 +178,7 @@ async function createComponentTreeInternal({
const isLayout = typeof layout !== 'undefined'
const isPage = typeof page !== 'undefined'
const { mod: layoutOrPageMod } = await getTracer().trace(
const { mod: layoutOrPageMod, modType } = await getTracer().trace(
NextNodeServerSpan.getLayoutOrPageModule,
{
hideSpan: !(isLayout || isPage),
......@@ -347,10 +347,10 @@ async function createComponentTreeInternal({
if (process.env.NODE_ENV === 'development') {
const { isValidElementType } = require('next/dist/compiled/react-is')
if (
(isPage || typeof MaybeComponent !== 'undefined') &&
typeof MaybeComponent !== 'undefined' &&
!isValidElementType(MaybeComponent)
) {
errorMissingDefaultExport(pagePath, 'page')
errorMissingDefaultExport(pagePath, modType ?? 'page')
}
if (
......
......@@ -23,6 +23,25 @@ describe('Undefined default export', () => {
)
})
it('should error if layout component does not have default export', async () => {
await using sandbox = await createSandbox(
next,
new Map([
['app/(group)/specific-path/server/layout.js', 'export const a = 123'],
[
'app/(group)/specific-path/server/page.js',
'export default function Page() { return <div>Hello</div> }',
],
]),
'/specific-path/server'
)
const { session } = sandbox
await session.assertHasRedbox()
expect(await session.getRedboxDescription()).toInclude(
'The default export is not a React Component in "/specific-path/server/layout"'
)
})
it('should error if not-found component does not have default export when trigger not-found boundary', async () => {
await using sandbox = await createSandbox(
next,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment