I am building my routes like this:
import Component from "./Component.svelte";
import LangComponent from "./LangComponent.svelte";
const buildRoutes = [
{ name: "/", component: Component },
{
name: "hello-world",
component: LangComponent,
lang: { de: "hallo-welt" },
},
];
export default buildRoutes.map(route => ({
...route,
layout: PublicLayout,
}));
I want to generate a static sitemap.xml in my public folder for all the (good) bots in the world to crawl. It should have this format:
<?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">
<url>
<loc>https://www.mycoolsite.com</loc>
<xhtml:link
rel="alternate"
hreflang="de"
href="http://www.example.com/deutsch/page.html"/>
<lastmod>2020-06-04</lastmod>
</url>
</urlset>
So basically I have to add a <loc>
when there is an element with only a name
property and additionally a <xhtml:link
element when the route is also localized.
What is the best way to do this? I have tried so far:
- Writing a bash script which
greps
the routes and generates a new file --> Too complicated with this structure (also way above mygrep
expertise). - Using a custom
npm
commmand to import the array in a javascript file and use thefs
module --> Ran into problems with babel which seems to not like the imported svelte files.- Do the same as above, but in an imported javascript file in my App.svelte to not run into the babel problem. The problem is now that as it does not run in the browser, there is no
fs
I can use.
- Do the same as above, but in an imported javascript file in my App.svelte to not run into the babel problem. The problem is now that as it does not run in the browser, there is no
Is there a way to do this? Can this maybe be done in rollup? There seem to be some solutions in sapper, but I don't use it.