-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Set up redirects based on 404 data & filter sitemap #1976
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ac5ed73
ec7894b
637d24e
948b975
eb09c90
8b9574a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import AstroSitemap from '@astrojs/sitemap'; | ||
import type { AstroIntegration } from 'astro'; | ||
import { normalizeLangTag } from '../src/i18n/bcp-normalize'; | ||
import languages from '../src/i18n/languages'; | ||
|
||
const langTags = Object.keys(languages); | ||
|
||
/** Set matching our `/[lang]/something.astro` redirect routes. */ | ||
const blocklist = new Set([ | ||
...langTags.map((lang) => `/${lang}/`), | ||
...langTags.map((lang) => `/${lang}/install/`), | ||
...langTags.map((lang) => `/${lang}/tutorial/`), | ||
]); | ||
|
||
/** Match a pathname starting with “lighthouse” or one of our language tags. */ | ||
const ValidRouteRE = new RegExp(`^/(lighthouse|${langTags.join('|')})/`); | ||
|
||
/** Test a pathname is not in our blocklist and starts with a valid prefix. */ | ||
const isValidPath = (path: string) => !blocklist.has(path) && ValidRouteRE.test(path); | ||
|
||
/** Get a preconfigured instance of the `@astrojs/sitemap` integration. */ | ||
export function sitemap(): AstroIntegration { | ||
return AstroSitemap({ | ||
filter: (page) => isValidPath(new URL(page).pathname), | ||
i18n: { | ||
defaultLocale: 'en', | ||
locales: Object.fromEntries(langTags.map((lang) => [lang, normalizeLangTag(lang)])), | ||
}, | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
import languages from '../../i18n/languages'; | ||
|
||
export const getStaticPaths = () => Object.keys(languages).map((lang) => ({ params: { lang } })); | ||
|
||
const { lang } = Astro.params; | ||
--- | ||
|
||
<meta http-equiv="refresh" content={`0;url=/${lang}/tutorial/0-introduction/`} /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this show up in the sitemap? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Excellent question. The answer is: yes! https://deploy-preview-1976--astro-docs-2.netlify.app/sitemap-0.xml We can filter sitemap URLs I believe. Let me look at that — we should probably exclude our other similar redirects too. (Probably doesn’t matter too much — these redirect pages are completely empty so are very unlikely to be indexed, but still.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, question back time: does it matter if it is in sitemap? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its usually best to keep redirects out of the sitemap, but that can be tackled in a separate PR 👍 Its really not a huge deal in general, I think it's pretty common for redirects to linger in a sitemap for a while (or forever on some sites) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK — pushed a fix for the low-hanging fruit here. Our There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Engaged morning brain and implemented a much better filtering set-up so now the sitemap shouldn’t include any of our redirect routes! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!