Menu

Frontend API

Docsta’s frontend is built using Next.js with support for both App Router and Pages Router. This gives you full control over rendering, routing, and dynamic data fetching using React’s native features and Next.js enhancements.

The "Frontend API" refers to the set of tools, utilities, and patterns used in your frontend codebase for accessing content, rendering pages, and interacting with user actions like search, theme toggle, and routing.

1. Data Fetching Patterns

Docsta supports both Server Components and Client Components in the App Router, and standard React logic in the Pages Router.

App Router Example (Server Component)

// app/docs/[slug]/page.tsx
import { fetchDocBySlug } from '@/lib/sanity'

export default async function DocPage({ params }) {
const doc = await fetchDocBySlug(params.slug)

return (
<div className="prose dark:prose-invert">
<h1>{doc.title}</h1>
<article>{/* Render PortableText or MDX here */}</article>
</div>
 )
}

Pages Router Example

// pages/docs/[slug].tsx
import { fetchDocBySlug } from '@/lib/sanity'

export async function getStaticProps({ params }) {
const doc = await fetchDocBySlug(params.slug)
return { props: { doc } }
}

export default function DocPage({ doc }) {
return <div>{doc.title}</div>
}
2. Using API Routes

Next.js allows you to build custom backend routes that the frontend can consume—perfect for search, feedback, forms, or dynamic content.

Example: Frontend Fetch to Internal API

// Calling your own route at /api/search
const res = await fetch('/api/search?q=dark mode')
const data = await res.json()

You can place route handlers in:

  • pages/api/* (Pages Router)
  • app/api/*/route.ts (App Router)

3. Theme Toggle API

Docsta includes a ThemeContext that exposes a simple API to toggle between light and dark modes:

const { theme, toggleTheme } = useTheme()
return <button onClick={toggleTheme}>Toggle Theme</button>

This is a client-only hook using React Context and localStorage.

4. Sidebar & Navigation

The sidebar is dynamically generated using a fetcher function (from CMS or file system) and rendered using a structured object or flat list.

export async function fetchSidebar() {
return await fetchDocsStructureFromSanity() // or local
}

Each item has a title, slug, and optionally, children or icon data.

5. Search API Integration

Frontend components can query search services like Algolia, Fuse.js, or your custom API.

Example (Client Component with debounce):

const [query, setQuery] = useState('')
const [results, setResults] = useState([])

useEffect(() => {
const timeout = setTimeout(async () => {
const res = await fetch(`/api/search?q=${query}`)
setResults(await res.json())
}, 300)

return () => clearTimeout(timeout)
}, [query])
6. Markdown / MDX Rendering

If you're using MDX:

import { MDXRemote } from 'next-mdx-remote'
<MDXRemote {...source} components={customComponents} />

If using Sanity with PortableText:

<PortableText value={doc.content} components={customComponents} />

Custom components are part of your frontend API surface.

7. Custom Hooks & Helpers

You can expose your own frontend utilities in hooks/ or lib/.

Example:

// hooks/useScrollTop.ts
export function useScrollTop() {
useEffect(() => {
window.scrollTo(0, 0)
}, [])
}

Use them in pages or components to abstract logic.

8. Loading & Error States

Good UI/UX means handling async API states:

if (loading) return <Spinner />
if (error) return <Error message="Something went wrong" />

Use Next.js loading states, suspense boundaries, or custom skeleton components.

9. Environment-Safe Access

Frontend-safe environment variables must start with NEXT_PUBLIC_:

const apiBase = process.env.NEXT_PUBLIC_API_URL

Avoid exposing secrets—only use frontend-safe vars in components.

Summary

The frontend API of Docsta includes:

  • ✅ Dynamic routing with slug-based pages
  • ✅ Flexible data fetching via Sanity, MDX, or API routes
  • ✅ Interactive components powered by React & Tailwind
  • ✅ Reusable hooks, theme context, and utility functions
  • ✅ Extensible and secure environment variable usage

👉 Next Up: Learn how to deploy your Docsta project with Vercel, and integrate search, analytics, or auth.

Let me know if you want code for:

  • A custom hook pack
  • A search autocomplete component
  • Or a live edit + preview setup next!

What did you think of this content?

like-icon

It was helpful

dislike-icon

It was not helpful

feedback-icon

I have a feedback