SEO
Hi everyone
It's possible to generate the
It's possible to generate the
robots.txt and sitemap.xml files on Solid Start project?sitemap.xml routerobots.txtrobots.txt manually or programmatically:robots.txt file in your directory:sitemap.xml and robots.txt are well-optimized for quick response times.robots.txt file by defining custom routes. This gives you flexibility and control over how these files are generated, especially in dynamic environments.createAsync inside the route? Or the cache() function that I have in my requests will work without the createAsync?
src/routessitemap.xml.tsxsitemap.xml.tsx/sitemap.xmlpublic/User-agent: *
Allow: /
Sitemap: https://yourwebsite.com/sitemap.xmlrobots.txt.tsxhttp://localhost:3000/sitemap.xmlhttp://localhost:3000/robots.txtAPI// src/routes/sitemap.xml.tsx
import { APIEvent } from "solid-start";
export async function GET({ request }: APIEvent) {
const baseUrl = new URL(request.url).origin;
const routes = [
"/",
"/about",
"/contact",
// Add more static routes here
];
const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${routes
.map(
(route) => `
<url>
<loc>${baseUrl}${route}</loc>
<lastmod>${new Date().toISOString()}</lastmod>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
</url>
`
)
.join("")}
</urlset>`;
return new Response(sitemap, {
headers: {
"Content-Type": "application/xml",
},
});
}// Example to add dynamic routes
const posts = await fetch("https://api.yourwebsite.com/posts").then(res => res.json());
const routes = [
"/",
"/about",
...posts.map(post => `/blog/${post.slug}`),
];// src/routes/robots.txt.tsx
import { APIEvent } from "solid-start";
export function GET({ request }: APIEvent) {
const baseUrl = new URL(request.url).origin;
const robotsTxt = `
User-agent: *
Allow: /
Sitemap: ${baseUrl}/sitemap.xml
`;
return new Response(robotsTxt.trim(), {
headers: {
"Content-Type": "text/plain",
},
});
}// src/routes/api/generate-sitemap.tsx
import { APIEvent } from "solid-start";
import fs from "fs";
import path from "path";
export async function GET({ request }: APIEvent) {
const baseUrl = new URL(request.url).origin;
const routes = [
"/",
"/about",
"/contact",
// Add more static routes or fetch dynamic routes
];
const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${routes
.map(
(route) => `
<url>
<loc>${baseUrl}${route}</loc>
<lastmod>${new Date().toISOString()}</lastmod>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
</url>
`
)
.join("")}
</urlset>`;
// Save to public directory
const sitemapPath = path.join(process.cwd(), "public", "sitemap.xml");
fs.writeFileSync(sitemapPath, sitemap);
return new Response("Sitemap generated and saved.", {
status: 200,
});
}