2023-01-14 23:51:12 +00:00
|
|
|
import { sendRedirect } from 'h3'
|
|
|
|
|
2023-01-15 14:10:20 +00:00
|
|
|
const BOT_RE = /bot\b|index|spider|facebookexternalhit|crawl|wget|slurp|mediapartners-google/i
|
|
|
|
|
2023-01-14 23:51:12 +00:00
|
|
|
export default defineNuxtPlugin(async (nuxtApp) => {
|
|
|
|
const route = useRoute()
|
2023-02-05 12:10:19 +00:00
|
|
|
if (!('server' in route.params))
|
2023-01-14 23:51:12 +00:00
|
|
|
return
|
|
|
|
|
2023-01-15 14:43:16 +00:00
|
|
|
const userAgent = useRequestHeaders()['user-agent']
|
2023-01-14 23:51:12 +00:00
|
|
|
if (!userAgent)
|
|
|
|
return
|
|
|
|
|
2023-01-15 14:10:20 +00:00
|
|
|
const isOpenGraphCrawler = BOT_RE.test(userAgent)
|
2023-01-15 00:47:11 +00:00
|
|
|
if (isOpenGraphCrawler) {
|
|
|
|
// Redirect bots to the original instance to respect their social sharing settings
|
2023-01-14 23:51:12 +00:00
|
|
|
await sendRedirect(nuxtApp.ssrContext!.event, `https:/${route.path}`, 301)
|
2023-01-15 00:47:11 +00:00
|
|
|
}
|
2023-01-14 23:51:12 +00:00
|
|
|
})
|