Skip to content

Commit bfa2920

Browse files
committed
feat(web): add sitemap
1 parent b6cd349 commit bfa2920

6 files changed

Lines changed: 91 additions & 2 deletions

File tree

‎packages/web/astro.config.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sitemap from '@astrojs/sitemap';
12
import lit from '@awesome.me/astro-lit';
23
import { defineConfig } from 'astro/config';
34
import { env } from 'node:process';
@@ -12,6 +13,7 @@ import { site } from './build/vite-plugin-site';
1213
const config = resolveConfig();
1314

1415
export default defineConfig({
16+
site: config.site.url,
1517
output: 'static',
1618
// Matches `trailingSlash: false` in firebase.json: `/speakers/abc` is `speakers/abc.html`.
1719
trailingSlash: 'never',
@@ -32,6 +34,7 @@ export default defineConfig({
3234
},
3335
},
3436
routes(config.site.features),
37+
sitemap({ filter: (page) => !page.endsWith('/offline') }),
3538
// Before the service worker, which precaches the pages it changes. Deploys build with
3639
// FIRESTORE_TARGET=production, and every other build may run on the emulators.
3740
csp(config.site as CspSite, { emulators: env['FIRESTORE_TARGET'] !== 'production' }),

‎packages/web/build/build.smoke.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,17 @@ describe('a production build of a minimal site', () => {
112112
});
113113
});
114114

115+
it('lists the home page in the sitemap, and links to it from robots.txt', () => {
116+
const locs = (file: string) =>
117+
[...build.read(file).matchAll(/<loc>([^<]*)<\/loc>/g)].map(([, loc]) => loc);
118+
119+
expect(locs('sitemap-index.xml')).toEqual(['https://minimal-site.web.app/sitemap-0.xml']);
120+
expect(locs('sitemap-0.xml')).toEqual(['https://minimal-site.web.app/']);
121+
expect(build.read('robots.txt')).toContain(
122+
'Sitemap: https://minimal-site.web.app/sitemap-index.xml',
123+
);
124+
});
125+
115126
it('precaches the scripts and the home and offline pages', () => {
116127
const worker = build.read('service-worker.js');
117128
const precached = [...worker.matchAll(/url:"([^"]+)"/g)].map((match) => match[1] ?? '');

‎packages/web/package-lock.json‎

Lines changed: 66 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/web/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"devDependencies": {
4040
"@astrojs/check": "^0.9.10",
4141
"@astrojs/rss": "^4.0.19",
42+
"@astrojs/sitemap": "^3.7.4",
4243
"@awesome.me/astro-lit": "^5.0.4",
4344
"@babel/core": "^8.0.6",
4445
"@babel/plugin-proposal-decorators": "^8.0.2",

‎packages/web/public/robots.txt‎

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { APIRoute } from 'astro';
2+
import { site } from 'virtual:hoverboard/site';
3+
4+
export const GET: APIRoute = () =>
5+
new Response(
6+
`User-agent: *\nAllow: /\n\nSitemap: ${new URL('sitemap-index.xml', site.url).href}\n`,
7+
{
8+
headers: { 'Content-Type': 'text/plain; charset=utf-8' },
9+
},
10+
);

0 commit comments

Comments
 (0)