Skip to content
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

Merged
merged 4 commits into from
Apr 21, 2021
Merged

Add sitemap generation #120

merged 4 commits into from
Apr 21, 2021

Conversation

drwpow
Copy link
Member

@drwpow drwpow commented Apr 21, 2021

Changes

Generates a sitemap.xml in the root of your build folder:

Screen Shot 2021-04-21 at 11 58 18

Only does so if site is set in astro.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:

Screen Shot 2021-04-21 at 11 45 19

Testing

  • Tests are passing
  • Tests updated where necessary

Docs

  • Docs / READMEs updated
  • Code comments added where helpful

Copy link
Member

@natemoo-re natemoo-re left a 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
Comment on lines 237 to 251
// 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`);
}

Copy link
Member

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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or a config option.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not both

Copy link
Member Author

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?

src/build/sitemap.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@matthewp matthewp left a 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) {
Copy link
Member Author

@drwpow drwpow Apr 21, 2021

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 };
Copy link
Member Author

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.

Copy link
Member Author

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

@drwpow drwpow force-pushed the sitemap branch 2 times, most recently from f8c5fc9 to 4cd2b77 Compare April 21, 2021 19:26
@drwpow drwpow merged commit a718573 into main Apr 21, 2021
@drwpow drwpow deleted the sitemap branch April 21, 2021 20:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants