Skip to main content
Module

x/fresh_seo/src/sitemap.ts

Create Sitemap for your next Fresh project.
Go to Latest
File
import { basename, day, dirname, extname } from "./deps.ts";
export class SitemapContext { #url: string; #manifest: any; #globalIgnore = ["sitemap.xml"]; #additionalRoutes: Array<string> = [];
constructor(url: string, manifest: any) { this.#url = url; this.#manifest = manifest; }
get routes() { return [ ...Object.entries(this.#manifest.routes) .filter(([path]) => { const isRootRoute = "./routes" === dirname(path); const file = basename(path); const fileName = file.replace(extname(file), ""); const isDynamic = !!fileName.match(/^\[.+\]$/)?.length;
if ( isDynamic || fileName.startsWith("_") || this.#globalIgnore.includes(fileName) ) { return false; }
return true; }) .map(([path, route]) => { return path .replace(extname(path), "") .replace("./routes", "") .replace("index", ""); // We remove index as it's consider a "/" in Fresh }),
...this.#additionalRoutes, ]; }
add(route: string) { this.#additionalRoutes.push(route); return this; }
generate() { return `<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml"> ${ this.routes.map((route) => { return `<url> <loc>${this.#url}${route}</loc> <lastmod>${day().format("YYYY-MM-DD")}</lastmod> <changefreq>daily</changefreq> <priority>0.8</priority> </url>`; }) .join("\n") } </urlset>`; }
render() { return new Response(this.generate(), { headers: { "Content-Type": "application/xml" }, }); }
// async save() { // const outPath = join(this.#staticDir, "sitemap.xml");
// try { // const content = this.generate();
// await ensureFile(outPath);
// return Deno.writeTextFile(outPath, content); // } catch (e) { // console.warn(e.message); // return null; // } // }}
// const handler: Handlers = {// GET(req, ctx) {// return new Response();// },// };