A sitemap helps search engines find and understand all pages on your website easily.
Sitemap generation in Astro
import { Sitemap } from 'astro:sitemap'; export async function GET() { return new Response(await Sitemap({ filter: (page) => !page.includes('private'), changefreq: 'daily', priority: 0.7 })); }
The Sitemap function generates the sitemap XML automatically.
You can customize which pages to include and how often they change.
import { Sitemap } from 'astro:sitemap'; export async function GET() { return new Response(await Sitemap()); }
import { Sitemap } from 'astro:sitemap'; export async function GET() { return new Response(await Sitemap({ filter: (page) => !page.includes('draft'), changefreq: 'weekly', priority: 0.5 })); }
This example creates a sitemap that excludes pages containing 'secret' in their URL. It sets the change frequency to monthly and priority to 0.8 to tell search engines how important and often pages change.
import { Sitemap } from 'astro:sitemap'; export async function GET() { return new Response(await Sitemap({ filter: (page) => !page.includes('secret'), changefreq: 'monthly', priority: 0.8 })); }
Make sure your sitemap URL is listed in your robots.txt file for better search engine discovery.
Update your sitemap whenever you add or remove pages to keep search engines informed.
Sitemaps help search engines find all your website pages.
Astro provides a simple Sitemap function to generate sitemaps automatically.
You can customize which pages to include and how often they change.
Practice
Solution
Step 1: Understand sitemap purpose
Sitemaps list all pages so search engines can find them easily.Step 2: Match purpose to options
Only To help search engines find and index all pages of the website describes helping search engines find pages.Final Answer:
To help search engines find and index all pages of the website -> Option CQuick Check:
Sitemap purpose = Help search engines [OK]
- Confusing sitemap with design or speed improvements
- Thinking sitemap adds animations
- Assuming sitemap affects page loading
Solution
Step 1: Recall correct import syntax
Astro exports Sitemap as a named export, so curly braces are needed.Step 2: Match syntax to options
import { Sitemap } from 'astro/sitemap'; uses named import with correct path and casing.Final Answer:
import { Sitemap } from 'astro/sitemap'; -> Option AQuick Check:
Named import with braces = import { Sitemap } from 'astro/sitemap'; [OK]
- Using default import syntax for named exports
- Incorrect module path or casing
- Importing with wrong variable name
export const get = () => {
return Sitemap({
filter: (page) => !page.includes('/drafts/'),
changefreq: 'weekly'
});
};What will be the effect of the
filter function?Solution
Step 1: Understand the filter function logic
The filter returns true only if the page path does NOT include '/drafts/'.Step 2: Determine which pages are included
Pages with '/drafts/' are excluded because filter returns false for them.Final Answer:
Exclude pages that have '/drafts/' in their path -> Option AQuick Check:
Filter excludes '/drafts/' pages = Exclude pages that have '/drafts/' in their path [OK]
- Thinking filter includes '/drafts/' pages
- Confusing filter logic with map or reduce
- Assuming syntax error without checking code
export const get = () => {
return Sitemap({
changefreq: 'daily',
filter: page => page.startsWith('/blog')
});
};Solution
Step 1: Check get function signature
Astro allows get to be sync or async; sync is valid here.Step 2: Verify Sitemap usage
Sitemap accepts an object with changefreq and filter keys as shown.Step 3: Confirm filter function returns boolean
page.startsWith('/blog') returns true or false, so filter is valid.Final Answer:
No error; code is correct -> Option DQuick Check:
Code syntax and logic are correct = No error; code is correct [OK]
- Assuming async is always required for get
- Thinking Sitemap needs an array argument
- Misunderstanding filter return value
changefreq to 'monthly'. Which code snippet correctly achieves this?Solution
Step 1: Understand filtering falsy values
Using!!pageconverts page to boolean, excluding falsy values like empty strings.Step 2: Check other options
export const get = () => Sitemap({ filter: page => page, changefreq: 'monthly' }); returns page directly, which may include falsy values; export const get = () => Sitemap({ filter: page => page.length > 0, changefreq: 'monthly' }); assumes page has length property which may cause errors if undefined; export const get = () => Sitemap({ filter: page => page === '', changefreq: 'monthly' }); includes only empty strings, which is opposite of desired filtering; export const get = () => Sitemap({ filter: page => !!page, changefreq: 'monthly' }); correctly filters out falsy values.Final Answer:
export const get = () => Sitemap({ filter: page => !!page, changefreq: 'monthly' }); -> Option BQuick Check:
Double negation filters falsy values = export const get = () => Sitemap({ filter: page => !!page, changefreq: 'monthly' }); [OK]
- Using filter that includes empty strings
- Not converting to boolean before filtering
- Assuming page.length works if page can be undefined
Ready to prove it?
Pass the test and this pattern moves unknown
