llms.txt لـ Gatsby

نهجان في Gatsby: ملف ثابت في static/ لا يحتاج إلى إعداد، أو إنشاء برمجي في gatsby-node.js باستخدام طبقة بيانات GraphQL لبناء الروابط من محتواك.

آخر تحديث:

الخيار 1: ملف ثابت في static/

ينسخ Gatsby كل شيء في static/ الدليل مباشرةً إلى public/ ناتج البناء. ضع ملفك في static/llms.txt وسيصبح متاحاً على /llms.txt بعد gatsby build.

static/llms.txt, directory structure
# Gatsby static file approach
#
# Place your file at: static/llms.txt
# Gatsby copies everything in static/ directly to the public/ build output.
#
# Project structure:
# your-gatsby-site/
# ├── static/
# │   └── llms.txt   ← add this
# ├── src/
# └── gatsby-config.js

لا يتطلب هذا النهج تغييرات في الكود ويعمل مع جميع أهداف نشر Gatsby: Netlify, Vercel, Cloudflare Pages, AWS Amplify, ومستضاف ذاتياً.

الخيار 2: التوليد في gatsby-node.js

بالنسبة إلى المواقع التي تأتي صفحات توثيقها من Markdown أو MDX أو CMS عبر طبقة بيانات Gatsby، استخدم onPostBuild خطاف دورة الحياة في gatsby-node.js لتوليد llms.txt بعد اكتمال البناء. ويضمن ذلك بقاء قائمة الروابط متزامنة دائماً مع محتواك.

gatsby-node.js
// gatsby-node.js
// Generate llms.txt from your GraphQL data layer
const { writeFileSync } = require('fs');
const { resolve } = require('path');

exports.onPostBuild = async ({ graphql }) => {
  // Query your documentation pages (adjust the query to your data model)
  const result = await graphql(`
    query {
      allMarkdownRemark(
        filter: { frontmatter: { collection: { eq: "docs" } } }
        sort: { frontmatter: { order: ASC } }
      ) {
        nodes {
          frontmatter {
            title
            description
          }
          fields {
            slug
          }
        }
      }
    }
  `);

  if (result.errors) {
    throw result.errors;
  }

  const docs = result.data.allMarkdownRemark.nodes;
  const siteUrl = 'https://yoursite.com';

  const links = docs
    .map(
      (doc) =>
        `- [${doc.frontmatter.title}](${siteUrl}/docs/${doc.fields.slug}/): ${doc.frontmatter.description ?? ''}`
    )
    .join('\n');

  const content = [
    '# Your Site',
    '',
    '> One-sentence description of your project.',
    '',
    '## Documentation',
    '',
    links,
    '',
    '## Optional',
    '',
    `- [Changelog](${siteUrl}/changelog/): Release history.`,
  ].join('\n');

  // Write to public/ (Gatsby's build output directory)
  writeFileSync(resolve(__dirname, 'public', 'llms.txt'), content, 'utf-8');
};

الـ onPostBuild تتلقى الخطاف نفسه graphql الدالة المستخدمة في استعلامات الصفحات. عدّل الاستعلام ليلائم نموذج بياناتك؛ يستخدم المثال allMarkdownRemark لكن يمكنك الاستعلام من أي إضافة مصدر لـ Gatsby (allMdx, allContentfulPageوما إلى ذلك).

نهج إضافة Gatsby

إذا كنت تفضّل حلاً جاهزاً، فإن عدة إضافات مجتمعية تولّد llms.txt تلقائياً. ابحث في npm عن gatsby-plugin-llms-txt. وبدلاً من ذلك، يوجد النهج أعلاه في gatsby-node.js مباشر بما يكفي بحيث تضيف إضافة مخصصة قيمة قليلة لمعظم المشاريع.

يمكنك أيضاً استخدام gatsby-plugin-sitemap كمرجع، فهو يستخدم النحو نفسه onPostBuild نمطاً لكتابة ملف XML إلى public/.

تحقّق

Build and verify
# Build
gatsby build

# Check generated file
cat public/llms.txt | head -10

# After deploying, verify the live URL
curl -I https://yoursite.com/llms.txt
# Expected: content-type: text/plain; charset=utf-8

أدلة ذات صلة

المصادر