{"id":1370,"date":"2026-07-10T18:08:48","date_gmt":"2026-07-10T11:08:48","guid":{"rendered":"https:\/\/sumberlaba.com\/index.php\/2026\/07\/10\/build-type-safe-static-sites-with-contentlayer-a-practical-tutorial\/"},"modified":"2026-07-10T18:08:48","modified_gmt":"2026-07-10T11:08:48","slug":"build-type-safe-static-sites-with-contentlayer-a-practical-tutorial","status":"publish","type":"post","link":"https:\/\/sumberlaba.com\/index.php\/2026\/07\/10\/build-type-safe-static-sites-with-contentlayer-a-practical-tutorial\/","title":{"rendered":"Build Type-Safe Static Sites with Contentlayer: A Practical Tutorial"},"content":{"rendered":"<h1>Build Type-Safe Static Sites with Contentlayer: A Practical Tutorial<\/h1>\n<p>Contentlayer bridges the gap between your content files (Markdown, MDX, JSON, YAML) and your static site code. It generates typed data from your content, enabling autocompletion and error checking in editors like VS Code. This makes static site development faster and more reliable.<\/p>\n<p>To get started, install Contentlayer in your Next.js, Gatsby, or Astro project. For Next.js, run <code>npm install contentlayer next-contentlayer<\/code> and add it to your <code>next.config.js<\/code>. After the second paragraph, insert the image placeholder as specified.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/via.placeholder.com\/800x600\/4a90d9\/ffffff?text=how%20to%20use%20Contentlayer%20for%20static%20sites\" alt=\"Article illustration\" style=\"display:block;margin:20px auto;max-width:100%;height:auto;border-radius:8px;\" \/><\/p>\n<h2>Define Your Content Schema<\/h2>\n<p>Create a <code>contentlayer.config.ts<\/code> file at your project root. Define a <code>Document<\/code> type for each content collection. For example, a blog post might include fields like <code>title<\/code>, <code>date<\/code>, and <code>tags<\/code>:<\/p>\n<ul>\n<li>Write <code>defineDocumentType<\/code> with <code>name: 'Post'<\/code><\/li>\n<li>Set <code>filePathPattern: '**\/*.md'<\/code><\/li>\n<li>Define fields using <code>field: { type: 'string', required: true }<\/code><\/li>\n<\/ul>\n<h2>Place Content and Generate Types<\/h2>\n<p>Store your Markdown files in a dedicated content folder (e.g., <code>\/content\/posts<\/code>). Run <code>npx contentlayer build<\/code> to generate typed data and a JSON representation of your content. Contentlayer creates a <code>.contentlayer<\/code> folder with <code>generated<\/code> files you can import directly into your components.<\/p>\n<h2>Query Content in Components<\/h2>\n<p>Import the generated <code>allPosts<\/code> (or your collection name) into any page or component. Example in Next.js:<\/p>\n<ul>\n<li><code>import { allPosts } from 'contentlayer\/generated'<\/code><\/li>\n<li>Map over <code>allPosts<\/code> to render a list of blog cards<\/li>\n<li>Access fields with autocompletion: <code>post.title<\/code>, <code>post.date<\/code><\/li>\n<\/ul>\n<p>Contentlayer also supports MDX with embedded React components, and live reload via watch mode (<code>npx contentlayer dev<\/code>).<\/p>\n<h2>Conclusion<\/h2>\n<p>Contentlayer simplifies static site development by providing type-safe content that integrates seamlessly with your preferred framework. Start small\u2014define one collection, generate types, and enjoy the productivity boost of a fully typed content pipeline.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Build Type-Safe Static Sites with Contentlayer: A Practical Tutorial Contentlayer bridges the gap between your content files (Markdown, MDX, JSON, YAML) and your static site code. It generates typed data from your content, enabling autocompletion and error checking in editors like VS Code. This makes static site development faster and more reliable. To get started, &hellip; <\/p>\n","protected":false},"author":2716,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-1370","post","type-post","status-publish","format-standard","hentry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/1370","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/users\/2716"}],"replies":[{"embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/comments?post=1370"}],"version-history":[{"count":0,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/1370\/revisions"}],"wp:attachment":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/media?parent=1370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/categories?post=1370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/tags?post=1370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}