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 the error message when custom export fields are used in an entry #42221

Merged
merged 6 commits into from Nov 1, 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
11 changes: 11 additions & 0 deletions errors/invalid-segment-export.md
@@ -0,0 +1,11 @@
# Invalid Layout or Page Export

#### Why This Error Occurred

Your [layout](https://beta.nextjs.org/docs/api-reference/file-conventions/layout) or [page](https://beta.nextjs.org/docs/api-reference/file-conventions/page) inside the app directory exports an invalid field. In these files, you're only allowed to export a default React component, or [Segment Configuration Options](https://beta.nextjs.org/docs/api-reference/segment-config) for layout and pages, such as `revalidate`, `generateStaticParams`, etc.

Other custom exports are not allowed to catch misspelt configuration options and prevent conflicts with future options.

#### Possible Ways to Fix It

You can create a new file and co-locate it with the page or layout. In the new file, you can export any custom fields and import it from anywhere.
4 changes: 4 additions & 0 deletions errors/manifest.json
Expand Up @@ -753,6 +753,10 @@
{
"title": "google-fonts-missing-subsets",
"path": "/errors/google-fonts-missing-subsets.md"
},
{
"title": "invalid-segment-export",
"path": "/errors/invalid-segment-export.md"
}
]
}
Expand Down
4 changes: 2 additions & 2 deletions packages/next/lib/typescript/diagnosticFormatter.ts
Expand Up @@ -116,9 +116,9 @@ function getFormattedLayoutAndPageDiagnosticMessageText(
if (filepathAndInvalidExport) {
const main = `${type} "${chalk.bold(
relativeSourceFile
)}" exports invalid field "${chalk.bold(
)}" exports an invalid "${chalk.bold(
filepathAndInvalidExport[2]
)}". Only "default" and other configuration exports are allowed.`
)}" field. ${type} should only export a default React component and configuration options. Learn more: https://nextjs.org/docs/messages/invalid-segment-export`
return main
}
break
Expand Down