llms.txt مع Cloudflare Pages
نهجان: ضع ملفاً ثابتاً في دليل مخرجات البناء للنشر الفوري بلا إعداد، أو استخدم Cloudflare Pages Function للمحتوى الديناميكي.
آخر تحديث:
النهج 1، ملف ثابت في مخرجات البناء
الخيار الأبسط. تقدّم Cloudflare Pages كل ملف في دليل ناتج البناء مباشرةً من CDN العالمي لديها. لا Workers ولا Functions ولا تغييرات في الإعداد مطلوبة.
# For any framework deployed on Cloudflare Pages,
# place llms.txt in the directory that gets published.
#
# Framework → file location
# Astro → public/llms.txt
# Next.js → public/llms.txt
# SvelteKit → static/llms.txt
# Hugo → static/llms.txt
# Eleventy → _site root (copy via passthrough)
# Plain HTML → project root or output folder
# After deploy, Cloudflare serves it at /llms.txt from their global CDN.
# Verify:
curl -I https://your-domain.com/llms.txt
# Expected: HTTP/2 200 | Content-Type: text/plain | CF-Cache-Status: HIT يعتمد الموقع الصحيح على إطار العمل لديك:
- Astro →
public/llms.txt(منسوخ إلىdist/تلقائياً) - Next.js →
public/llms.txt(يدعم محول Cloudflare Pages لـ Next.js هذا) - SvelteKit →
static/llms.txt - Hugo →
static/llms.txt - Eleventy → أضف نسخة عابرة:
eleventyConfig.addPassthroughCopy("llms.txt") - HTML عادي → ضعه في المجلد الذي حددته كدليل مخرجات البناء
بعد النشر، تخدّم Cloudflare الملف من شبكتها الطرفية حول العالم.
Content-Type: text/plain يُضبط الرأس تلقائياً استناداً إلى
.txt الامتداد.
النهج 2، Cloudflare Pages Function
تتيح Cloudflare Pages Functions معالجة مسارات محددة بكود TypeScript يعمل على بيئة Cloudflare
Workers. أنشئ ملفاً في functions/llms.txt.ts ويعالج الطلبات تلقائياً إلى /llms.txt.
// functions/llms.txt.ts
// Cloudflare Pages Functions use the file path as the route.
// This file handles GET requests to /llms.txt
interface Env {
// Add KV namespace or D1 bindings here if needed
}
export const onRequestGet: PagesFunction<Env> = async (context) => {
// Build content, hardcode here or pull from KV / D1 / API
const content = [
'# My Site',
'',
'> One-sentence description of what this site is about.',
'',
'## Documentation',
'',
'- [Getting started](https://yoursite.com/docs/getting-started/): first steps.',
'- [API reference](https://yoursite.com/docs/api/): full endpoint catalog.',
'',
'## Optional',
'',
'- [Changelog](https://yoursite.com/changelog/): version history.',
].join('\n');
return new Response(content, {
headers: {
'Content-Type': 'text/plain; charset=utf-8',
// Cache at the edge for 1 hour, allow stale for 24h
'Cache-Control': 'public, max-age=3600, stale-while-revalidate=86400',
},
});
};
متى تستخدم هذا: عندما تريد توليد الملف من مصدر بيانات (قاعدة بيانات أو API لنظام إدارة محتوى أو مخزن KV) دون إعادة بناء الموقع كله. مثال مع مساحة أسماء KV:
// functions/llms.txt.ts, pulling content from KV
// Useful if you update the file from a CMS webhook without redeploying.
interface Env {
LLMS_TXT: KVNamespace;
}
export const onRequestGet: PagesFunction<Env> = async ({ env }) => {
const content = await env.LLMS_TXT.get('content');
if (!content) {
return new Response('# My Site\n\n> Content not configured yet.', {
status: 200,
headers: { 'Content-Type': 'text/plain; charset=utf-8' },
});
}
return new Response(content, {
headers: {
'Content-Type': 'text/plain; charset=utf-8',
'Cache-Control': 'public, max-age=300, stale-while-revalidate=3600',
},
});
};
مع نهج KV، يمكنك تحديث محتوى /llms.txt عن طريق الكتابة إلى مساحة أسماء KV (عبر API أو
لوحة التحكم أو خطاف ويب من CMS لديك) من دون تشغيل عملية نشر Pages كاملة.
البديل: Cloudflare Worker
إذا لم يكن موقعك على Cloudflare Pages لكنه يستخدم Cloudflare كوكيل CDN/DNS، فيمكنك اعتراض /llms.txt المسار مع Cloudflare Worker مستقل باستخدام
نمط المسار:
// Cloudflare Worker, wrangler.toml config
// Use this if you want a standalone Worker (not tied to Pages).
// wrangler.toml
// name = "llms-txt-worker"
// main = "src/index.ts"
// compatibility_date = "2024-09-01"
//
// [[routes]]
// pattern = "yoursite.com/llms.txt"
// zone_name = "yoursite.com"
// src/index.ts
export default {
async fetch(request: Request): Promise<Response> {
const content = `# My Site
> One-sentence description.
## Core pages
- [Getting started](https://yoursite.com/docs/getting-started/): first steps.
- [API reference](https://yoursite.com/docs/api/): full endpoint catalog.
`;
return new Response(content, {
headers: {
'Content-Type': 'text/plain; charset=utf-8',
'Cache-Control': 'public, max-age=3600',
},
});
},
} satisfies ExportedHandler;
ترويسات التخزين المؤقت وسلوك CDN
تخزّن Cloudflare الملفات الثابتة من مخرجات البناء مؤقتاً تلقائياً. وبالنسبة إلى الملفات الثابتة، لا تحتاج إلى ضبط ترويسات التخزين المؤقت؛ إذ تحترم Cloudflare مدة TTL الافتراضية من إعدادات مشروع Pages لديك.
بالنسبة إلى Pages Functions، اضبط Cache-Control مذكورة صراحةً في الاستجابة. موصى به:
-
public, max-age=3600، تخزيناً مؤقتاً على الحافة لمدة ساعة (مناسب للملفات المصانة يدوياً). -
public, max-age=3600, stale-while-revalidate=86400، وقدّم نسخة قديمة لمدة تصل إلى 24 ساعة أثناء إعادة التحقق في الخلفية. -
public, max-age=300، وتخزين مؤقت لمدة 5 دقائق لمحتوى مدعوم بـ KV قد يتحدث باستمرار.
عند نشر إصدار جديد، تُبطل Cloudflare ذاكرة التخزين المؤقت تلقائياً للملفات الثابتة التي تغيرت. أما Pages Functions، فاستخدم لوحة Cloudflare أو API لمسح عنوان URL المحدد إذا احتجت إلى إبطال فوري.
تحقّق بعد النشر
# Check headers, look for Content-Type and CF-Cache-Status
curl -I https://yoursite.com/llms.txt
# Check content
curl https://yoursite.com/llms.txt
# Purge Cloudflare cache if you deployed a new version:
# Dashboard → Caching → Configuration → Purge Everything
# or via API:
curl -X POST "https://api.cloudflare.com/client/v4/zones/{ZONE_ID}/purge_cache" \
-H "Authorization: Bearer {CF_API_TOKEN}" \
-H "Content-Type: application/json" \
--data '{"files":["https://yoursite.com/llms.txt"]}' بعد التحقق من الرؤوس، الصق عنوان URL المباشر في المدقّق للتأكد من توافق المواصفة: H1 واحد بالضبط، وصياغة روابط صالحة، وجميع عناوين URL مطلقة، ولا أقسام فارغة.