spike: experiment with automatic sitemap generation
This commit is contained in:
parent
1ec3f28b63
commit
2937c81a8c
5 changed files with 50 additions and 0 deletions
|
@ -3,6 +3,8 @@ export interface Props {
|
|||
title: string;
|
||||
}
|
||||
const { title } = Astro.props;
|
||||
const routes = await Astro.generateRouteManifest();
|
||||
console.log(routes);
|
||||
---
|
||||
|
||||
<style lang="scss">
|
||||
|
|
|
@ -11,6 +11,7 @@ import configAliasVitePlugin from '../vite-plugin-config-alias/index.js';
|
|||
import markdownVitePlugin from '../vite-plugin-markdown/index.js';
|
||||
import jsxVitePlugin from '../vite-plugin-jsx/index.js';
|
||||
import fetchVitePlugin from '../vite-plugin-fetch/index.js';
|
||||
import sitemapVitePlugin from '../vite-plugin-sitemap/index.js';
|
||||
import { resolveDependency } from './util.js';
|
||||
|
||||
// Some packages are just external, and that’s the way it goes.
|
||||
|
@ -56,6 +57,7 @@ export async function createVite(inlineConfig: ViteConfigWithSSR, { astroConfig,
|
|||
jsxVitePlugin({ config: astroConfig, logging }),
|
||||
astroPostprocessVitePlugin({ config: astroConfig, devServer }),
|
||||
fetchVitePlugin(),
|
||||
sitemapVitePlugin({ config: astroConfig })
|
||||
],
|
||||
publicDir: fileURLToPath(astroConfig.public),
|
||||
root: fileURLToPath(astroConfig.projectRoot),
|
||||
|
|
|
@ -284,6 +284,10 @@ export function createAstro(fileURLStr: string, site: string): AstroGlobalPartia
|
|||
return {
|
||||
site: new URL(site),
|
||||
fetchContent,
|
||||
async generateRouteManifest() {
|
||||
const { default: routes } = await import('virtual:@astrojs/sitemap');
|
||||
return routes;
|
||||
},
|
||||
// INVESTIGATE is there a use-case for multi args?
|
||||
resolve(...segments: string[]) {
|
||||
return segments.reduce((u, segment) => new URL(segment, u), url).pathname;
|
||||
|
|
3
packages/astro/src/vite-plugin-sitemap/README.md
Normal file
3
packages/astro/src/vite-plugin-sitemap/README.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
# vite-plugin-fetch
|
||||
|
||||
Adds fetch support in SSR contexts.
|
39
packages/astro/src/vite-plugin-sitemap/index.ts
Normal file
39
packages/astro/src/vite-plugin-sitemap/index.ts
Normal file
|
@ -0,0 +1,39 @@
|
|||
import type { Plugin } from '../core/vite';
|
||||
import type { AstroConfig } from '../@types/astro';
|
||||
|
||||
import { createRouteManifest } from '../core/ssr/routing.js';
|
||||
|
||||
export default function pluginSitemap({ config }: { config: AstroConfig }): Plugin {
|
||||
const VIRTUAL_FILE_ID = `virtual:@astrojs/sitemap`;
|
||||
|
||||
return {
|
||||
name: '@astrojs/vite-plugin-sitemap',
|
||||
resolveId(id) {
|
||||
if (id === VIRTUAL_FILE_ID) {
|
||||
return VIRTUAL_FILE_ID
|
||||
}
|
||||
return null
|
||||
},
|
||||
load(id) {
|
||||
if (id === VIRTUAL_FILE_ID) {
|
||||
const routeManifest = createRouteManifest({ config });
|
||||
const sitemap: Record<string, any> = {};
|
||||
for (const route of routeManifest.routes) {
|
||||
if (route.pathname) {
|
||||
sitemap[route.pathname] = {
|
||||
component: '/' + route.component,
|
||||
dynamic: false
|
||||
}
|
||||
} else {
|
||||
const pathname = route.component.replace(/^src\/pages/, '').replace(/\.(md|astro)$/, '');
|
||||
sitemap[pathname] = {
|
||||
component: '/' + route.component,
|
||||
dynamic: true
|
||||
}
|
||||
}
|
||||
}
|
||||
return `export default ${JSON.stringify(sitemap)}`
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue