π SitemapΒΆ
Producing and serving a sitemap in Duck is straightforward. A sitemap is an XML file that lists all the pages of your website so search engines like Google can efficiently crawl your content.
In Duck, you can lazily generate and serve a sitemap using the duck.etc.apps.essentials.blueprint.Sitemap blueprint.
The sitemap is generated the first time
/sitemap.xmlis visited.Subsequent visits return a cached response, thanks to the
duck.views.cached_viewdecorator.This ensures the sitemap is only generated once per session, improving performance.
Learn more about cached views here.
βοΈ ConfigurationΒΆ
To start serving the sitemap, add the blueprint in your settings:
# settings.py
BLUEPRINTS = [
# Other blueprints
"duck.etc.apps.essentials.blueprint.Sitemap",
]
This is enough for basic sitemap serving, but for fine-grained control, Duck provides several settings:
SITEMAP_EXTRA_URLS = SETTINGS.get("SITEMAP_EXTRA_URLS", [])
SITEMAP_EXCLUDE_PATTERNS = SETTINGS.get("SITEMAP_EXCLUDE_PATTERNS", [])
SITEMAP_DEFAULT_PRIORITY = SETTINGS.get("SITEMAP_DEFAULT_PRIORITY", 0.5)
SITEMAP_CHANGE_FREQUENCY = SETTINGS.get("SITEMAP_CHANGE_FREQUENCY", "monthly")
SITEMAP_SAVE_TO_FILE = SETTINGS.get("SITEMAP_SAVE_TO_FILE", False)
SITEMAP_FILEPATH = SETTINGS.get("SITEMAP_FILEPATH", None)
SITEMAP_APPLY_DEFAULT_EXCLUDES = SETTINGS.get("SITEMAP_APPLY_DEFAULT_EXCLUDES", True)
π Setting DetailsΒΆ
Setting |
Description |
Notes / Tips |
|---|---|---|
|
Extra URLs to include beyond registered routes |
Can include absolute URLs (https://β¦) or relative paths (/about) |
|
List of URL strings or regexes to exclude |
Example: |
|
Default |
Higher priority signals importance to search engines |
|
Default |
Examples: |
|
Whether to save sitemap XML to disk |
Set |
|
Path to save sitemap |
Example: |
|
Apply Duckβs default exclusions |
Includes static files, |
Tip: Duck automatically excludes static files like CSS, JS, images, fonts, and media by default. You can safely include PDFs and videos by customizing
SITEMAP_EXCLUDE_PATTERNS.
π Best PracticesΒΆ
Include PDFs in your sitemap if they contain meaningful content, like manuals or guides.
Videos should only be included if they are valuable content. For large or auxiliary media, consider a separate video sitemap.
Avoid including test, temporary, or duplicate URLs to maintain a clean sitemap.
Use explicit
SITEMAP_EXTRA_URLSfor pages that are not registered as routes.Keep your sitemap under ~50k URLs per file and under 50MB uncompressed for SEO performance.
π Using SitemapBuilderΒΆ
The SitemapBuilder class provides a programmatic interface for building sitemaps:
from duck.contrib.sitemap import SitemapBuilder
builder = SitemapBuilder(
server_url=None, # Automatically resolved if None
save_to_file=True,
filepath="/etc/sitemap.xml",
extra_urls=["/about", "https://example.com/contact"],
exclude_patterns=["^/admin", "https://example.com/secret", "^/api/.*"],
default_priority=0.5,
default_changefreq="monthly",
)
xml = builder.build(return_content=True) # Returns sitemap XML as string
πΉ FeaturesΒΆ
Walks Duckβs
RouteRegistryand collects valid routes.Filters out dynamic routes (e.g.,
/user/<id>) and regex-like patterns.Supports extra URLs and exclude patterns (plain or regex).
Builds XML using Duck components (
duck.html.components.to_component).Can persist the sitemap to a file if configured.
β‘ Exclude Patterns ExampleΒΆ
Duck provides default exclusions to avoid indexing unnecessary content:
DEFAULT_EXCLUDES = [
# Static files and folders
r"(?ix)^(?:.*.(?:css|js|map|ico|mp3|mp4|png|jpe?g|gif|svg|webp|avif|bmp|tiff?|woff2?|ttf|eot|otf)$|.*/(?:static|assets|media)/.*)$",
# Dynamic or admin paths
r"^/sitemap.xml$", # Exclude the sitemap itself
r"^/ws/lively.*", # Anything starting with /ws/lively
r"^/admin/.*", # Any subpath under /admin/
r"^/admin$", # Strictly /admin itself
r"^/api/.*", # Any subpath under /api/
r"^/api$", # Strictly /api itself
]
These exclusions ensure static assets, admin routes, API routes, and the sitemap itself are not included in the generated sitemap.
Generating sitemap from CMDΒΆ
Yes, you can generate a sitemap from the command line. This is made possible by the duck sitemap command. Here is
an example:
duck sitemap create
The above command will generate sitemap.xml located at etc/sitemap.xml. You can
also customize options by providing arguments to the command. Use command duck sitemap --help to view more
more information on this command usage.
β SummaryΒΆ
Duckβs sitemap system is:
Lazy β generated only when first requested.
Cached β subsequent requests are fast.
Flexible β add extra URLs, define exclusions, adjust priorities and change frequency.
SEO-friendly β automatically avoids unnecessary static files and system routes.
By combining
SitemapBuilderwithSITEMAP_EXTRA_URLSandSITEMAP_EXCLUDE_PATTERNS, you can fully control what appears in your sitemap while keeping it optimized for search engines.