llms.txt في Next.js
أضف ملف llms.txt إلى مشروع Next.js خلال دقائق، إما كأصل ثابت أو Route Handler ديناميكي.
آخر تحديث:
نهجان
يمنحك Next.js طريقتين واضحتين لتقديم llms.txt في /llms.txt:
- ملف ثابت في
/public/, وهو النهج الأبسط. اكتب الملف يدوياً أو أنشئه وقت البناء. ويعمل مع App Router وPages Router، دون حمل وقت تشغيل. - معالج مسار App Router, ملف TypeScript في
app/llms.txt/route.tsالذي يولّد المحتوى ديناميكياً من مصادر بياناتك (ملفات MDX أو CMS أو قاعدة بيانات). وهو الأفضل عندما يتغير المحتوى كثيراً.
بالنسبة إلى معظم المواقع، الملف الثابت في /public/ هو الخيار الصحيح. استخدم معالج المسار
فقط إذا أردت إنشاء المحتوى تلقائياً من بيانات موقعك وقت البناء أو الطلب.
الطريقة 1: ملف ثابت في /public/
يقدّم Next.js كل ملف في /public/ في جذر نطاقك. يوجد ملف في /public/llms.txt متاح على https://yourdomain.com/llms.txt
دون إعداد إضافي.
-
أنشئ الملف:
touch public/llms.txt -
اكتب
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.
- التزم بالملف وانشره. انتهى.
الطريقة 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' },
});
}
التحقق من الإعداد
بعد النشر، تحقّق من أن ملفك متاح وسليم البنية:
-
زُر
https://yourdomain.com/llms.txtفي متصفحك، ينبغي أن ترى نصاً عادياً. -
افحص ترويسات HTTP:
curl -I https://yourdomain.com/llms.txtينبغي أن يعرضContent-Type: text/plainورمز الحالة 200. - الصق عنوان URL في أداة التحقق في llmtxt.info للتحقق من توافق المواصفة.
- تحقّق من أن
llms.txtغير محظور فيpublic/robots.txt.
واصل القراءة
- كيفية إنشاء llms.txt, دليل عام يتضمن قوالب لجميع الأطر.
- ما هو llms-full.txt؟, الملف المصاحب للمجموعة الكاملة.
- المدقّق، طابق ملفك مع المواصفة.
- أفضل الممارسات, وما ينبغي تضمينه وما ينبغي تجنبه.