<!-- Generated from ar/llms-txt-nextjs/index.html. The canonical document is the HTML page. -->

- [ الرئيسية ](/ar/) 
/
- [ كيفية إنشاء llms.txt ](/ar/how-to-create/) 
/
- Next.js            
# `llms.txt` في Next.js

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

آخر تحديث: 22 أبريل 2026

في هذه الصفحة

- [ نهجان ](#overview)
- [ الطريقة 1: ملف ثابت في /public/ ](#static)
- [ الطريقة 2: معالج مسار App Router ](#route-handler)
- [ بديل Pages Router ](#pages-router)
- [ إضافة llms-full.txt ](#llms-full)
- [ التحقق من الإعداد ](#verify)            
## نهجان

يمنحك 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` المحتوى (راجع [دليل إجرائي](/ar/how-to-create/) و
[المولّد](/ar/generator/) للتنسيق الصحيح):   
```
# 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.

```

- التزم بالملف وانشره. انتهى.     
مثال

ترويسة Content-Type

سيقدّم Next.js  /public/llms.txt  مع  Content-Type: text/plain 
تلقائياً (استناداً إلى  .txt  امتداد). لا حاجة إلى إعداد رؤوس إضافية.

## الطريقة 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 بعرض هذا المسار وقت البناء بدلاً من كل طلب.
أزل هذا السطر إذا كنت تحتاج إلى محتوى ديناميكي فعلاً (يُجلب من قاعدة بيانات مباشرة وقت الطلب).

ملاحظة

التوليد التلقائي من ملفات MDX

لتوليد قائمة الصفحات تلقائياً من دليل محتوى MDX، استبدل السلسلة الثابتة بمنطق يقرأ مصادر المحتوى
لديك:
const files = fs.readdirSync('content/docs')  ← استخرج العناوين والأسماء المختصرة ← ابنِ
قائمة Markdown. وهذا يبقي  llms.txt  متزامناً مع صفحاتك الفعلية تلقائياً.

## بديل 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`](/ar/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](/ar/validator/) للتحقق من توافق المواصفة. 
- تحقّق من أن `llms.txt` غير محظور في `public/robots.txt`.   
## واصل القراءة

- [كيفية إنشاء llms.txt](/ar/how-to-create/), دليل عام يتضمن قوالب لجميع الأطر. 
- [ما هو llms-full.txt؟](/ar/llms-full-txt/), الملف المصاحب للمجموعة الكاملة. 
- [المدقّق](/ar/validator/)، طابق ملفك مع المواصفة. 
- [أفضل الممارسات](/ar/best-practices/), وما ينبغي تضمينه وما ينبغي تجنبه.        
## المصادر

- [ توثيق Next.js، الأصول الثابتة في /public/ ](https://nextjs.org/docs/app/api-reference/file-conventions/public-folder)
- [ توثيق Next.js، Route Handlers ](https://nextjs.org/docs/app/api-reference/file-conventions/route)
- [ llmstxt.org، مرجع المواصفة ](https://llmstxt.org/)
