Cursorcursorintermediate

Add Dynamic Sitemap & Robots.txt to Next.js (Cursor Prompt)

Cursor prompt to add a dynamic sitemap.ts and robots.ts for Next.js App Router with Supabase data.

What you'll get

Dynamic sitemap.xml and robots.txt generated at build/request time from Supabase data.

The Prompt

Add a dynamic sitemap and robots.txt to this Next.js App Router project.

FILES TO CREATE:
- src/app/sitemap.ts — Dynamic sitemap generation
- src/app/robots.ts — Robots.txt configuration

IMPLEMENTATION:
1. In sitemap.ts, export a default async function that returns MetadataRoute.Sitemap.
2. Include static routes with their priorities and change frequencies:
   - Home (/): priority 1.0, changeFrequency 'weekly'
   - About, Pricing, Contact: priority 0.8, changeFrequency 'monthly'
3. Fetch dynamic routes from Supabase:
   - Blog posts: query "blog_posts" for published posts, map to /blog/[slug] with priority 0.7
   - Product/listing pages: query relevant tables, map to /[type]/[slug] with priority 0.6
   - Set lastModified from the updated_at column
4. Combine static and dynamic routes into one array.
5. In robots.ts, export a default function returning MetadataRoute.Robots:
   - Allow all crawlers on all public paths
   - Disallow: /admin/*, /api/*, /auth/*
   - Point sitemap to: https://yourdomain.com/sitemap.xml
6. Add a sitemap index if you have more than 50,000 URLs by splitting into multiple sitemap files.

DO NOT:
- Include auth pages, admin pages, or API routes in the sitemap
- Include draft or unpublished content
- Hardcode the domain — use NEXT_PUBLIC_SITE_URL environment variable
- Use third-party sitemap generators

ENVIRONMENT VARIABLES:
- NEXT_PUBLIC_SITE_URL (e.g., https://yourdomain.com)

Replace these variables

VariableReplace with
[SITE_URL]Production URL of the site
[DYNAMIC_ROUTES]Tables to query for dynamic sitemap entries

Tips for best results

Use NEXT_PUBLIC_SITE_URL for the domain — don't hardcode it so it works in staging and production.

Submit your sitemap to Google Search Console after deploying to speed up indexing.

Follow-up prompts

Add structured data

Add JSON-LD structured data (Organization, WebSite, BreadcrumbList) to the root layout for enhanced Google search results.

Add OG images

Add dynamic Open Graph images using Next.js generateMetadata and ImageResponse for social sharing previews.

Related prompts