-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add sitemap generation #120
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like a great first pass! Will be awesome to have this built in.
src/build.ts
Outdated
// build sitemap | ||
if (astroConfig.site) { | ||
const sitemap = generateSitemap( | ||
builtURLs.map((url) => ({ | ||
canonicalURL: new URL( | ||
path.extname(url) ? url : url.replace(/\/?$/, '/'), // add trailing slash if there’s no extension | ||
astroConfig.site | ||
).href, | ||
})) | ||
); | ||
await writeFile(new URL('./sitemap.xml', dist), sitemap, 'utf8'); | ||
} else { | ||
info(logging, 'tip', `Set your "site" in astro.config.mjs to generate a sitemap.xml`); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A --no-sitemap
CLI flag might be good to support here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or a config option.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. Updated to allow --no-sitemap
CLI flag as well as sitemap: false
in config. How are the CLI changes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
@@ -49,14 +52,14 @@ function mergeSet(a: Set<string>, b: Set<string>) { | |||
} | |||
|
|||
/** Utility for writing to file (async) */ | |||
async function writeFilep(outPath: URL, bytes: string | Buffer, encoding: 'utf-8' | null) { | |||
async function writeFilep(outPath: URL, bytes: string | Buffer, encoding: 'utf8' | null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a note: utf8
is the official Node encoding, not utf-8
. Most times it’s an alias, but there’s one API where utf-8
is invalid (something Buffer-related IIRC) and it always throws me. Anyway, that’s the reason for the change here.
if (flags.version) { | ||
return 'version'; | ||
return { cmd: 'version', options }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@matthewp needed to add additional flag context here; what are your thoughts on cleanliness? Happy to bikeshed this or reorganize.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gonna merge, but happy to incorporate post-merge feedback in a followup PR
f8c5fc9
to
4cd2b77
Compare
Changes
Generates a
sitemap.xml
in the root of your build folder:Only does so if
site
is set inastro.config.mjs
(required because sitemaps don’t work without the full URL).If you don’t have this set, it will remind you to do so:
Testing
Docs