llms.txt مع Astro

نهجان: ضع ملفاً في public/ للنشر الفوري، أو أنشئه ديناميكياً من مجموعات محتواك عبر نقطة نهاية TypeScript.

آخر تحديث:

النهج 1: ملف ثابت في public/

أبسط خيار. أنشئ ملفاً نصياً عادياً في public/llms.txt في جذر مشروعك. وينسخ Astro الدليل الكامل public/ الدليل حرفياً إلى dist/ أثناء astro build، حتى يُقدَّم الملف في /llms.txt من دون أي إعداد.

static approach, public/llms.txt
# Place the file at: public/llms.txt
# Astro copies everything in public/ to the output directory as-is.
# Result: served at https://yoursite.com/llms.txt, no code changes needed.

# After deploy, verify:
curl -I https://yoursite.com/llms.txt
# Expected: HTTP/2 200 | Content-Type: text/plain

متى تستخدم هذا: إذا كان محتواك لا يتغير كثيراً، أو كنت تريد انعدام العبء وقت البناء، أو كنت تدير الملف يدوياً. ويعمل بالطريقة نفسها على Cloudflare Pages وVercel وNetlify وأي مضيف Astro آخر.

النهج 2، نقطة نهاية TypeScript مع getCollection()

يطابق Astro الامتداد المزدوج .txt.ts ويتعامل مع src/pages/llms.txt.ts كنقطة نهاية API تستجيب على العنوان /llms.txt. في وضع SSG (الإعداد الافتراضي)، يعيد Astro تصييره مسبقاً إلى ملف ثابت dist/llms.txt وقت البناء، من دون كلفة وقت التشغيل.

src/pages/llms.txt.ts
// src/pages/llms.txt.ts
// Astro treats src/pages/foo.ext.ts as a route for /foo.ext
// In SSG mode (default), it pre-renders to dist/llms.txt at build time.

import type { APIRoute } from 'astro';
import { getCollection } from 'astro:content';

export const GET: APIRoute = async () => {
  // Replace 'docs' with your actual content collection name
  const docs = await getCollection('docs');

  const lines = [
    '# My Site',
    '',
    '> One-sentence description of what this site is about.',
    '',
    '## Documentation',
    '',
    ...docs
      .filter((d) => !d.data.draft)
      .sort((a, b) => (a.data.order ?? 999) - (b.data.order ?? 999))
      .map((d) => `- [${d.data.title}](https://yoursite.com/${d.slug}/): ${d.data.summary ?? ''}`.trimEnd()),
    '',
    '## Optional',
    '',
    '- [Changelog](https://yoursite.com/changelog/): version history.',
  ];

  return new Response(lines.join('\n'), {
    headers: { 'Content-Type': 'text/plain; charset=utf-8' },
  });
};

النقاط الأساسية:

  • استبدل 'docs' مع اسم مجموعتك الفعلي من src/content/config.ts.
  • استبعد المسودات قبل إنشاء الخريطة: !d.data.draft.
  • يتضمن التعليق في كل سطر (: short note) اختياري لكنه موصى به بشدة، لأنه يمنح نماذج اللغة الكبيرة سياقاً عن كل صفحة من دون أن تضطر إلى جلبها.
  • اضبط دائماً Content-Type: text/plain; charset=utf-8 في الاستجابة.

إضافة llms-full.txt

الـ llms-full.txt يضم الملف المصاحب متن كل صفحة بالكامل، وهو مفيد لمسارات RAG وعملاء نماذج اللغة الذين يريدون المجموعة كلها دفعة واحدة. أنشئ نقطة نهاية موازية:

src/pages/llms-full.txt.ts
// src/pages/llms-full.txt.ts
// Generates the full-content companion file.
// Each entry gets its full Markdown body inlined, useful for RAG pipelines.

import type { APIRoute } from 'astro';
import { getCollection } from 'astro:content';

export const GET: APIRoute = async () => {
  const docs = await getCollection('docs');
  const sections: string[] = [];

  for (const doc of docs.filter((d) => !d.data.draft)) {
    sections.push(
      `# ${doc.data.title}`,
      '',
      `URL: https://yoursite.com/${doc.slug}/`,
      '',
      doc.body,   // raw Markdown, no HTML, ideal for LLMs (Astro 4+)
      '',
      '---',
      '',
    );
  }

  return new Response(sections.join('\n'), {
    headers: { 'Content-Type': 'text/plain; charset=utf-8' },
  });
};

doc.body يحتوي على Markdown خام (متاح منذ Astro 4)، وهو مثالي لنماذج اللغة الكبيرة: لا وسوم HTML، ونص دلالي نظيف. وفي Astro 3، استخدم render() مساعداً وإزالة الوسوم بتعبير نمطي بسيط.

تحقّق بعد البناء

verify
# Build the project
npx astro build

# Check static output (works for both approaches in SSG mode)
cat dist/llms.txt

# Or start preview server and curl:
npx astro preview &
curl http://localhost:4321/llms.txt
curl http://localhost:4321/llms-full.txt

بعد النشر، الصق عنوان URL المباشر في المدقّق لتأكيد مطابقة المواصفة: عنوان H1 واحد بالضبط، وصياغة روابط صالحة (- [title](https://...))، جميع عناوين URL مطلقة ولا أقسام فارغة.

أي نهج تختار

  • ثابت public/llms.txt، وهو الأفضل للمواقع التي تتحدث نادراً. بلا عبء، ولا TypeScript مطلوب، ويعمل في كل مكان.
  • نقطة نهاية TypeScript src/pages/llms.txt.ts، وهو الأنسب لمواقع التوثيق ذات الصفحات المنشأة تلقائياً بكثرة. ويبقى الملف متزامناً تلقائياً عند كل بناء من دون تعديلات يدوية.

دليل الإنشاء الكامل · دليل Next.js · دليل WordPress

المصادر