Skip to content

Commit

Permalink
feat: add ability to redirect to source post when using default server
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshix-1 committed Jan 8, 2024
1 parent 3adf92e commit 6185b1f
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions plugins/social.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@ const BOT_RE = /bot\b|index|spider|facebookexternalhit|crawl|wget|slurp|mediapar

export default defineNuxtPlugin(async (nuxtApp) => {
const route = useRoute()
if (!('server' in route.params))
return

let routePath
if (!route.params['server']) {
const server = nuxtApp.$config.public.defaultServer
if (!server)
return
routePath = `/${server}${route.path}`
}
else {
routePath = route.path
}

const userAgent = useRequestHeaders()['user-agent']
if (!userAgent)
Expand All @@ -14,6 +23,6 @@ export default defineNuxtPlugin(async (nuxtApp) => {
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)
await sendRedirect(nuxtApp.ssrContext!.event, `https:/${routePath}`, 301)
}
})

0 comments on commit 6185b1f

Please sign in to comment.