From fa42d7f86acb8e1d57c33561c356410c2af2c0cc Mon Sep 17 00:00:00 2001 From: Joshix Date: Sun, 7 Jan 2024 20:00:00 +0000 Subject: [PATCH] feat: add ability to redirect to source post when using default server --- plugins/social.server.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/plugins/social.server.ts b/plugins/social.server.ts index a4944475..2a75168c 100644 --- a/plugins/social.server.ts +++ b/plugins/social.server.ts @@ -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) @@ -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) } })