Generate the llms.txt file for a site
seo-in-nextjs can generate the llms.txt file for your Next.js site with automatic route detection.
Enabling llms.txt generation
Section titled “Enabling llms.txt generation”There are two ways to generate the llms.txt file.
Build-time generation (Recommended)
Section titled “Build-time generation (Recommended)”-
Add the generation to your build script in
package.json.package.json {"scripts": {"build": "node --input-type=module -e \"import('@dlcastillop/seo-in-nextjs/scripts').then(m => m.generateLlmsTxt())\" && next build"}} -
After the build finishes, navigate to
/llms.txton your website to view the generated file.
Manual generation
Section titled “Manual generation”-
Add a separate script to generate the file on demand.
package.json {"scripts": {"generate-llmstxt": "node --input-type=module -e \"import('@dlcastillop/seo-in-nextjs/scripts').then(m => m.generateLlmsTxt())\""}} -
Then run it manually.
Terminal window npm run generate-llmstxtTerminal window yarn generate-llmstxtTerminal window pnpm generate-llmstxt -
Open http://localhost:3000/llms.txt in your browser to see the
llms.txtfile.
Manual routes
Section titled “Manual routes”Automatic route detection works by analyzing your project’s file system. However, not all routes in a Next.js application are explicitly represented in the file system.
To include these routes in your llms.txt file, you need to manually specify them in the manualRoutes property of the seo.config.ts or seo.config.js file.
import { defineSeoConfig } from "@dlcastillop/seo-in-nextjs";
export default defineSeoConfig({ baseUrl: "https://example.com", siteName: "Example", defaultOgImg: "/og-default.png", manualRoutes: ["/users/1", "/users/2", "/users/3"],});In the manualRoutes property you must include:
- Dynamic segments: Routes with parameters like
/users/[id],/posts/[slug] - Catch-all routes: Routes using
[...slug]or[[...slug]]syntax - Parallel routes: Routes defined with
@folderconvention - Intercepting routes: Routes using
(.)folderor(..)foldersyntax - Programmatically generated routes: Routes created at runtime or build time
- Data-driven routes: Routes that depend on database, CMS, or API content