Generating Sitemaps with Adonis Ace

Tue Dec 19 2023

I recently released my first project of the year, a job board for my home town, and knew that SEO was going to be key to getting visitors to the site. This made me wonder if a sitemap would help to improve SEO and set about trying to generate one using AdonisJS’ Ace CLI framework.

Below is an example of the the code I used to generate the sitemap using the sitemap npm package. I can now run this manually or with a cron job which I plan to do on the site weekly.

try {
  const links = [
    { url: "/", changefreq: "monthly", priority: 1 },
    { url: "/search", changefreq: "monthly", priority: 0.9 },
  ];

  const jobs = await Job.query().select(["id", "business_id"]);

  jobs.map((job) => {
    links.push({ url: `/jobs/${job.id}`, changefreq: "monthly", priority: 0.5 });
  });

  await simpleSitemapAndIndex({
    hostname: "<https://examplejobs.com>",
    destinationDir: "./build/public/",
    sourceData: links,
    gzip: false,
  }).then(() => {
    this.logger.success("[✔] Sitemap generated.");
  });
} catch (e) {
  this.logger.error("[❌] Sitemap generated.");
  this.logger.error(e);
}

This command generates a sitemap-0.xml in your /build/public folder and is accessible at http://localhost:3333/sitemap-0.xml

Table of Contents