Snippets

Utils

A collection of utility functions for React.

Construct Metadata

Inspired by JoshTriedCoding this function builds the Next.js Metadata for website. Works great with the generateMetadata function.

src/lib/utils.ts
export type SiteConfig = typeof siteConfig
 
export const siteConfig = {
  name: 'Docs',
  description: 'Docs for my code.',
  mainNav: [],
  links: {
    github: 'https://github.com/heystevegray/docs',
  },
  appURL: 'https://docs.stevegray.io',
}
 
export function constructMetadata({
  title = siteConfig.name,
  description = siteConfig.description,
  image = '/logo.jpg',
  icons = '/favicon.ico',
  noIndex = false,
}: {
  title?: string
  description?: string
  image?: string
  icons?: string
  noIndex?: boolean
} = {}): Metadata {
  return {
    title,
    description,
    openGraph: {
      url: new URL(siteConfig.appURL),
      title,
      description,
      images: [
        {
          url: image,
        },
      ],
    },
    twitter: {
      card: 'summary_large_image',
      title,
      description,
      images: [image],
      creator: MY_WEBSITE_URL,
    },
    icons,
    metadataBase: new URL(siteConfig.appURL),
    ...(noIndex && {
      robots: {
        index: false,
        follow: false,
      },
    }),
  }
}

isDevelopmentEnvironment

src/lib/utils.ts
export const isDevelopmentEnvironment = () => process?.env?.NODE_ENV === 'development'

Capitalize First Letter

src/lib/utils.ts
export const capitalizeFirstLetter = (value: string) => {
  return value.charAt(0).toUpperCase() + value.slice(1)
}