fix: catch more bots with redirects (#1174)

This commit is contained in:
Daniel Roe 2023-01-15 14:10:20 +00:00 committed by GitHub
parent 18ad1c2333
commit ccad8bf22b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,5 +1,7 @@
import { sendRedirect } from 'h3' import { sendRedirect } from 'h3'
const BOT_RE = /bot\b|index|spider|facebookexternalhit|crawl|wget|slurp|mediapartners-google/i
export default defineNuxtPlugin(async (nuxtApp) => { export default defineNuxtPlugin(async (nuxtApp) => {
const route = useRoute() const route = useRoute()
if (!route.params.server) if (!route.params.server)
@ -10,7 +12,7 @@ export default defineNuxtPlugin(async (nuxtApp) => {
if (!userAgent) if (!userAgent)
return return
const isOpenGraphCrawler = /twitterbot|discordbot|facebookexternalhit|googlebot|msnbot|baidu|ahrefsbot/i.test(userAgent) const isOpenGraphCrawler = BOT_RE.test(userAgent)
if (isOpenGraphCrawler) { if (isOpenGraphCrawler) {
// Redirect bots to the original instance to respect their social sharing settings // Redirect bots to the original instance to respect their social sharing settings
await sendRedirect(nuxtApp.ssrContext!.event, `https:/${route.path}`, 301) await sendRedirect(nuxtApp.ssrContext!.event, `https:/${route.path}`, 301)