llms.txt في Next.js

أضف ملف llms.txt إلى مشروع Next.js خلال دقائق، إما كأصل ثابت أو Route Handler ديناميكي.

آخر تحديث:

نهجان

يمنحك Next.js طريقتين واضحتين لتقديم llms.txt في /llms.txt:

  1. ملف ثابت في /public/, وهو النهج الأبسط. اكتب الملف يدوياً أو أنشئه وقت البناء. ويعمل مع App Router وPages Router، دون حمل وقت تشغيل.
  2. معالج مسار App Router, ملف TypeScript في app/llms.txt/route.ts الذي يولّد المحتوى ديناميكياً من مصادر بياناتك (ملفات MDX أو CMS أو قاعدة بيانات). وهو الأفضل عندما يتغير المحتوى كثيراً.

بالنسبة إلى معظم المواقع، الملف الثابت في /public/ هو الخيار الصحيح. استخدم معالج المسار فقط إذا أردت إنشاء المحتوى تلقائياً من بيانات موقعك وقت البناء أو الطلب.

الطريقة 1: ملف ثابت في /public/

يقدّم Next.js كل ملف في /public/ في جذر نطاقك. يوجد ملف في /public/llms.txt متاح على https://yourdomain.com/llms.txt دون إعداد إضافي.

  1. أنشئ الملف: touch public/llms.txt
  2. اكتب llms.txt المحتوى (راجع دليل إجرائي و المولّد للتنسيق الصحيح):
# Your Site Name

> One-sentence description of your site for LLM context.

## Core pages

- [Page title](https://yourdomain.com/page/): brief description.
- [Another page](https://yourdomain.com/other/): brief description.

## Optional

- [About](https://yourdomain.com/about/): who maintains this site.
  1. التزم بالملف وانشره. انتهى.

الطريقة 2: Route Handler في App Router

إذا أردت إنشاء llms.txt برمجياً، مثلاً بقراءة كل ملفات MDX في دليل المحتوى لديك، استخدم Route Handler.

أنشئ الملف app/llms.txt/route.ts:

import { NextResponse } from 'next/server';

export const dynamic = 'force-static'; // generate at build time

export async function GET() {
  // Build your content. Here: hardcoded; in practice: read from MDX, CMS, etc.
  const content = `# Your Site Name

> Description of your site.

## Core pages

- [Getting started](https://yourdomain.com/docs/getting-started/): installation and first steps.
- [API reference](https://yourdomain.com/docs/api/): full endpoint reference.
`;

  return new NextResponse(content, {
    headers: {
      'Content-Type': 'text/plain; charset=utf-8',
      // Optional: cache for 1 hour in production
      'Cache-Control': 'public, max-age=3600, stale-while-revalidate=86400',
    },
  });
}

إعداد dynamic = 'force-static' يخبر Next.js بعرض هذا المسار وقت البناء بدلاً من كل طلب. أزل هذا السطر إذا كنت تحتاج إلى محتوى ديناميكي فعلاً (يُجلب من قاعدة بيانات مباشرة وقت الطلب).

بديل Pages Router

إذا كنت تستخدم Pages Router في Next.js (قبل App Router)، فلا يزال أسهل مسار هو الملف الثابت في /public/. ولا يوجد عرف ملف Route Handler إلا في App Router.

للتوليد الديناميكي مع Pages Router، يمكنك استخدام خادم مخصص (Express أو Fastify) أو getServerSidePropsصفحة مدعومة بنوع محتوى مخصص، لكن ذلك يضيف تعقيداً كبيراً مقارنة بالملف الثابت. التزم بـ /public/llms.txt لمشروعات Pages Router.

إضافة llms-full.txt

llms-full.txt يضمّن المحتوى الكامل لصفحاتك الرئيسية في ملف واحد. وفي Next.js App Router:

// app/llms-full.txt/route.ts
import { NextResponse } from 'next/server';
import fs from 'fs';
import path from 'path';

export const dynamic = 'force-static';

export async function GET() {
  // Example: read MDX files from content/docs and concatenate them
  const docsDir = path.join(process.cwd(), 'content/docs');
  const files = fs.readdirSync(docsDir).filter(f => f.endsWith('.mdx'));

  const sections = files.map(file => {
    const content = fs.readFileSync(path.join(docsDir, file), 'utf8');
    const slug = file.replace('.mdx', '');
    return `## https://yourdomain.com/docs/${slug}/\n\n${content}`;
  });

  const body = `# yourdomain.com, full content\n\n${sections.join('\n\n---\n\n')}`;

  return new NextResponse(body, {
    headers: { 'Content-Type': 'text/plain; charset=utf-8' },
  });
}

التحقق من الإعداد

بعد النشر، تحقّق من أن ملفك متاح وسليم البنية:

  1. زُر https://yourdomain.com/llms.txt في متصفحك، ينبغي أن ترى نصاً عادياً.
  2. افحص ترويسات HTTP: curl -I https://yourdomain.com/llms.txt ينبغي أن يعرض Content-Type: text/plain ورمز الحالة 200.
  3. الصق عنوان URL في أداة التحقق في llmtxt.info للتحقق من توافق المواصفة.
  4. تحقّق من أن llms.txt غير محظور في public/robots.txt.

واصل القراءة

المصادر