Expose named exports (of top-level pages)
See original GitHub issueFeature request
Is your feature request related to a problem? Please describe.
We’re using next.js and MDX together to build a simple documentation site, and our page components are .mdx
files. MDX supports meta data, which is attached to MDX components via named exports (see https://github.com/mdx-js/mdx#exports), but the next.js renderer doesn’t currently expose anything other than the default export, so there’s no way to access this meta data in _app.js
.
Describe the solution you’d like
From looking at the next.js source code I think the easiest solution would be to attach the named exports to the Component function, in server/render.js
:
let [ComponentExports, Document, App] = await Promise.all([
requirePage(page, {dir, dist}),
require(documentPath),
require(appPath)
])
const Component = ComponentExports.default || ComponentExports
Component.exports = ComponentExports
…and in server/build/plugins/pages-plugin.js
:
source.add(`__NEXT_REGISTER_PAGE('${routeName}', function() {
var comp =
`)
source.add(modules)
source.add(`
var page = comp.default
page.exports = comp
return { page: page }
})
`)
Alternatively, instead of attached the named exports to the default export (the component) they could be passed through the renderer independently, but that would be a bigger change.
Describe alternatives you’ve considered
We’ve looked at tackling the issue by modifying the MDX library, but whilst most of our pages will be .mdx
files, not all of them will. The solution suggested above would support a consistent approach in both MDX and regular page components.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:6
- Comments:6 (2 by maintainers)
Top GitHub Comments
Next.js has exposed all exports for a few versions. Closing as solved.
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.