@exabytellc/utils
Version:
EB react utils to make everything a little easier!
30 lines (28 loc) • 1.08 kB
JavaScript
import buildFilePlugin from "./buildFilePlugin.js";
/**
* Generates a `robots.txt` file for SEO and web crawlers.
*
* @param {Object} options - Configuration options.
* @param {string} options.host - The base URL of the website (used for sitemap).
* @param {string} [options.userAgent="*"] - Specifies which web crawlers the rules apply to.
* @param {string} [options.allow="/"] - Defines which parts of the site are accessible.
* @param {boolean} [options.sitemap=false] - If true, adds a sitemap entry.
* @returns {import("vite").Plugin} Vite plugin to generate `robots.txt`.
*/
export default function generateRobotsPlugin({
host,
userAgent = "*",
allow = "/",
sitemap = false,
} = {}) {
return buildFilePlugin("vite-generate-robots-plugin", {
filename: "robots.txt",
content: (config) => {
let robots = "";
robots += `User-agent: ${userAgent ?? "*"}\n`;
robots += `Allow: ${allow ?? "/"}\n`;
if (sitemap) robots += `Sitemap: ${host}${config.base}sitemap.xml\n`;
return robots;
},
});
}