1
0
mirror of https://github.com/elk-zone/elk synced 2024-11-30 15:58:06 +09:00
elk/plugins/social.server.ts
2023-01-15 14:10:20 +00:00

21 lines
630 B
TypeScript

import { sendRedirect } from 'h3'
const BOT_RE = /bot\b|index|spider|facebookexternalhit|crawl|wget|slurp|mediapartners-google/i
export default defineNuxtPlugin(async (nuxtApp) => {
const route = useRoute()
if (!route.params.server)
return
const req = nuxtApp.ssrContext!.event.node.req
const userAgent = req.headers['user-agent']!
if (!userAgent)
return
const isOpenGraphCrawler = BOT_RE.test(userAgent)
if (isOpenGraphCrawler) {
// Redirect bots to the original instance to respect their social sharing settings
await sendRedirect(nuxtApp.ssrContext!.event, `https:/${route.path}`, 301)
}
})