“Unlock the Future of Web Development: Discover the Hidden Powers of Headless WordPress!”

"Unlock the Future of Web Development: Discover the Hidden Powers of Headless WordPress!"

We can modify the [...slug].astro file with getStaticPaths, allowing Astro to know all the possible URLs/paths at build time.

It also allows us to use the data directly in the template:

import { graphQLClient, GET_POSTS } from '../../lib/graphql';
import type { WordPressPostsResponse, WordPressTerm } from '../../lib/types';
import BlogPost from '../../layouts/BlogPost.astro';
import { formatDate } from '../../lib/utils';

export async function getStaticPaths() {
  const allPaths = [];
  let afterCursor = null;
  let hasNextPage = true;

  while (hasNextPage) {
    const response: WordPressPostsResponse = await graphQLClient.request<WordPressPostsResponse>(GET_POSTS, {
      first: 10, // Fetch in batches of 10
      after: afterCursor,
    });

    allPaths.push(
      ...response.posts.nodes.map(post => ({
        params: { slug: post.slug },
        props: { post },
      }))
    );

    hasNextPage = response.posts.pageInfo.hasNextPage;
    afterCursor = response.posts.pageInfo.endCursor;
  }

  return allPaths; // Return an array as required by Astro
}

const { post } = Astro.props;

if (!post) {
	return Astro.redirect('/404');
}

// Format the post data to match the BlogPost component props
const postData = {
	title: post.title,
	description: post.excerpt.replace(/<[^>]*>/g, ''), // Strip HTML tags
	pubDate: formatDate(post.date),
	updatedDate: formatDate(post.date),
	heroImage: post.featuredImage?.node.sourceUrl || undefined,
};

// Get the post content
const content = post.content || '';

// Get additional post metadata
const author = post.author?.node;
const categories = post.categories?.nodes || [];
const tags = post.tags?.nodes || [];

We can now run npm run build && npm run preview, which will convert all our static paths (e.g., blog posts) into HTML files and serve the static files from a local dev server:

Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

You May Have Missed