Refreshing the Next.js App Router When Your Markdown Content Changes - Steve Ruiz
Are you writing MDX content for a Next.js blog and want to see live reloads when the content changes? Here's how to do it. Install some dependencies: Create a watcher.ts file in the root of your Next.js project. Create a script in your package.json that starts the watcher on dev. Create a component named AutoRefresh. Wrap your root layout.tsx in the AutoRefresh component. Vercel provides excellent documentation of using Markdown / MDX together with Next.js (including with its new App Router). While it's possible to have .mdx files map 1-1 to pages using the file-based routing system, this is impractical for most projects and especially for projects with many dozens or hundreds of content files. For these projects, the answer has been to use remote MDX via the Hashicorp library next-mdx-remote, where content files are created server-side upon first request. This is a great solution, but it has one drawback: the Next.js App Router does not refresh when content in the folder changes. This
Are you writing MDX content for a Next.js blog and want to see live reloads when the content changes? Here's how to do it. The Solution Install some dependencies: npm i -D ws concurrently tsx Create a watcher.ts file in the root of your Next.js project. import fs from "fs" ; import { WebSocketServer } from "ws" ; const CONTENT_FOLDER = "content" ; fs . watch ( CONTENT_FOLDER , { persistent : true , recursive : true } , async ( eventType , fileName ) => { clients . forEach ( ( ws ) => { // do any pre-processing you want to do here... ws . send ( "refresh" ) ; } ) ; } , ) ; const wss = new WebSo
Explore this link on the map →related reading
- How Notion pulled itself back from the brink of failure | Figma Blogfigma.com
- Building a single-page application with Next.js and React Routercolinhacks.com
- Getting Started: Layouts and Pages | Next.jsnextjs.org
- GitHub - emanuelefavero/next-js: A Next.js cheat sheet repository · GitHubgithub.com
- One Year with Next.js App Router — Why We're Moving Onpaperclover.net
- App Router: Navigating Between Pages | Next.jsnextjs.org
- Cursor Docs — Agent, Rules, MCP, Skills & CLIdocs.cursor.com
- Pages Router | Next.jsnextjs.org
- Getting Started: Layouts and Pages | Next.jsnextjs.org
- File-system conventions: loading.js | Next.jsnextjs.org
- The Content Operating System for the AI era | Sanitysanity.io
- next.js/examples at canary · vercel/next.js · GitHubgithub.com