How to extend MDX with custom layouts on Shipixen
Extend MDX with custom layouts on Shipixen by creating layout components in the components directory and configuring them in your MDX files using frontmatter. Shipixen automatically detects and applies custom layouts to your content pages.
Prerequisites
- Basic React/TypeScript knowledge
- Existing Shipixen project
- Familiarity with MDX syntax
- Understanding of Next.js layouts
Step-by-Step Instructions
Create a Custom Layout Component
components/layouts/CustomLayout.tsx. Define your layout component with props for children and frontMatter:export default function CustomLayout({ children, frontMatter }) {
return (
<div className="custom-layout">
<header>{frontMatter.title}</header>
<main>{children}</main>
</div>
)
}Register Layout in MDX Configuration
next.config.js and add your custom layout to the MDX configuration. Import your layout and include it in the components object:import CustomLayout from './components/layouts/CustomLayout'
const withMDX = require('@next/mdx')({
options: {
remarkPlugins: [],
rehypePlugins: [],
}
})
module.exports = withMDX({
pageExtensions: ['js', 'jsx', 'mdx', 'md', 'ts', 'tsx']
})Configure Layout Mapping
components/MDXComponents.tsx to map layout names to components. This allows you to specify layouts in MDX frontmatter:import CustomLayout from './layouts/CustomLayout'
import DefaultLayout from './layouts/DefaultLayout'
const layoutComponents = {
custom: CustomLayout,
default: DefaultLayout
}
export default layoutComponentsCreate Layout Wrapper Logic
pages/_app.tsx or create a new components/LayoutWrapper.tsx, add logic to dynamically select layouts based on frontmatter:import layoutComponents from '../components/MDXComponents'
function LayoutWrapper({ children, frontMatter }) {
const Layout = layoutComponents[frontMatter.layout] || layoutComponents.default
return <Layout frontMatter={frontMatter}>{children}</Layout>
}Update MDX Files with Layout Frontmatter
layout property to your MDX files' frontmatter to specify which custom layout to use:---
title: "My Custom Page"
layout: "custom"
description: "Page using custom layout"
---
# Your MDX Content Here
This page will use the CustomLayout component.Style Your Custom Layout
styles/CustomLayout.module.css and import it in your layout component:import styles from '../../styles/CustomLayout.module.css'
export default function CustomLayout({ children, frontMatter }) {
return (
<div className={styles.container}>
<aside className={styles.sidebar}>Navigation</aside>
<main className={styles.content}>{children}</main>
</div>
)
}Test and Deploy Custom Layouts
npm run dev to test your custom layouts locally. Navigate to pages using your custom layout and verify they render correctly. Check responsive behavior and ensure all frontmatter data displays properly. Deploy using Shipixen's deployment options when satisfied with the results.Common Issues & Troubleshooting
Layout not applying to MDX pages
Verify the layout name in frontmatter matches exactly with the key in your layoutComponents object. Check for typos and case sensitivity.
Frontmatter data not available in layout
Ensure you're passing the frontMatter prop correctly through your LayoutWrapper and that your MDX processing configuration includes frontmatter parsing.
Styles not loading in custom layout
Check that CSS imports are correct and that your CSS modules or styled-components are properly configured in next.config.js.
Build errors with custom layouts
Verify all imports are correct, TypeScript types are properly defined, and that your layout components don't use client-side only features during SSR.