Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

2.6 KB
Raw Blame
import _isString from 'lodash/isString'
import _last from 'lodash/last'
import _map from 'lodash/map'
import { useTranslation } from 'react-i18next'
import { redirect, data } from 'react-router'
import type { LoaderFunctionArgs, MetaFunction } from 'react-router'
import type { SitemapFunction } from 'remix-sitemap'

import {
  isSelfhosted,
  getOgImageUrl,
  isDisableMarketingPages,
  API_URL,
} from '~/lib/constants'
import Post from '~/pages/Blog/Post'
import { getSlugFromFilename, getDateFromFilename } from '~/utils/blog'
import { getPost } from '~/utils/getPosts.server'
import { getDescription, getPreviewImage, getTitle } from '~/utils/seo'
import { blogArticleMeta, blogImageUrl } from '~/utils/blogMetadata'

export const meta: MetaFunction<typeof loader> = ({ loaderData: data }) => {
  // eslint-disable-next-line react-hooks/rules-of-hooks
  const { t } = useTranslation('common')

  const title = data?.seoTitle || data?.title || 'Blog'
  const intro = data?.seoDescription || data?.intro || t('description.blog')
  const ogImageUrl = blogImageUrl(data?.image) || getOgImageUrl(title, intro)

  return [
    ...getTitle(title, title.length <= 50),
    ...getDescription(intro),
    ...getPreviewImage(ogImageUrl),
    ...blogArticleMeta(data),
  ]
}

// @ts-expect-error
export const sitemap: SitemapFunction = async () => {
  const response = await fetch(`${API_URL}v1/blog/sitemap`)
  const files = await response.json()

  if (isSelfhosted || isDisableMarketingPages) {
    return {
      exclude: true,
    }
  }

  return _map(files, (file) => {
    let handle: string
    let date: string

    if (_isString(file)) {
      handle = getSlugFromFilename(file)
      date = getDateFromFilename(file)
    } else {
      const _file = _last(file) as string
      const firstElement = file[0]

      if (firstElement === 'blog') {
        if (file.length === 2) {
          handle = `blog/${getSlugFromFilename(_file)}`
        } else {
          const category = file[1]
          handle = `blog/${category}/${getSlugFromFilename(_file)}`
        }
      } else {
        handle = `${firstElement}/${getSlugFromFilename(_file)}`
      }

      date = getDateFromFilename(_file)
    }

    return {
      loc: `/${handle}`,
      lastmod: date,
      changefreq: 'weekly',
    }
  })
}

export async function loader({ request, params }: LoaderFunctionArgs) {
  if (isSelfhosted || isDisableMarketingPages) {
    return redirect('/dashboard', 302)
  }

  if (!params.slug) {
    return data(null, {
      status: 404,
    })
  }

  const post = await getPost(request, params.slug)

  if (!post) {
    return data(null, {
      status: 404,
    })
  }

  return post
}

export default Post