With Tina, your content is stored in Git along with your codebase. Tina provides a Content API in front of your repo-based content, so that you can interact with your files as if they're in a database.
You can:
To interface with the API, you can use Tina's type-safe client for data-fetching, or manually write custom GraphQL queries and hit the API yourself.
The Tina client is the easiest way to fetch your site's content. The client can be configured the tina/config.<js|ts>
in the defineConfig
function.
Note: token, clientId and branch are not used in local-mode. To setup these values for production see this doc
// tina/config.{js,ts,tsx}export default defineConfig({schema,token: '***',clientId: '***',branch: 'main',});
When working locally, the client is built with the local url (http://localhost:4001/graphql
). When in production mode, clientId
, branch
and token
are used to query TinaCloud.
Tina client provides a type-safe query builder, that is auto-generated based on your site's schema:
import { client } from '../[pathToTina]/tina/__generated__/client';const myPost = await client.queries.post({ relativePath: 'HelloWorld.md' });console.log(myPost.data.title);
The above client.queries.post
query is not built-in to Tina's API. This is an example of a query based on your defined schema, (where you have a "post" collection defined).
On a page that displays a list of posts, you can fetch the posts like so:
const postsResponse = await client.queries.postConnection();const posts = postsResponse.data.postConnection.edges.map((post) => {return { slug: post.node._sys.filename };});// This would return an array like: [ { slug: 'HelloWorld.md'}, /*...*/ ]
For more information on manually writing queries for your specific schema, check out our "Writing Custom Queries" docs.
If you're using SSG, creating a new page in TinaCMS means the content updates in TinaCloud and your Git repository instantly. However, your site (based off main
or another branch) won't show this new page immediately, often resulting in a 404 error. This is because SSG sites pre-build all pages at deployment time; your server doesn't know about the new page until your site is rebuilt.
Solutions for New SSG Pages:
fallback: true
or fallback: 'blocking'
in getStaticPaths
to generate pages on demand.For server-rendered pages in TinaCMS, you can dynamically fetch content from the active editorial or preview branch by passing the active branch name stored in the x-branch
cookie as a header when querying content.
To retrieve cookies in Next.js, refer to the Next.js cookies documentation.
const data = await client.queries.post({relativePath: `${params.filename?.join('/')}.md`,},{fetchOptions: {headers: {/* Retrieve the active branch from cookie */'x-branch': cookieStore.get('x-branch')?.value,},},});
When developing locally, it's often beneficial to talk to the content on your local file-system, rather than talk to the hosted content API. Tina provides a CLI tool that lets you run the Content API locally next to your site. This allows all of your content to be made available through the same expressive GraphQL API during development.
If you setup Tina via@tinacms/cli init
, or used one of our starters, this should be setup by default. (Read about the CLI here
For those who prefer to learn from video, you can check out a snippet on "Data Fetching" from our "TinaCMS Deep Dive" series.