I am trying out Next.js and build a small app which fetches posts from a headless WordPress app with GraphQL installed. Then I use Apollo/Client to get GraphQL content:
apollo-client.js
import { ApolloClient, InMemoryCache } from "@apollo/client";
const client = new ApolloClient({
uri: process.env.WORDPRESS_GRAPHQL_ENDPOINT,
cache: new InMemoryCache(),
});
export default client;
In index I grab the posts:
index.js
import Head from "next/head";
import styles from "../styles/Home.module.css";
import { gql } from "@apollo/client";
import Link from "next/link";
import client from "../apollo-client";
function Home(props) {
const { posts } = props;
return (
<div className={styles.container}>
<Head>
<title>Wordpress blog posts</title>
<meta
name="description"
content="Wordpress blog posts with Apollo Client"
/>
<link rel="icon" href="/favicon.ico" />
</Head>
<main className={styles.main}>=
<div className={styles.grid}>
{posts.map((post) => (
<a
key={post.node.databaseId}
href={`/blog/${post.node.slug}`}
className={styles.card}
>
<h2>{post.node.title}</h2>
<div dangerouslySetInnerHTML={{ __html: post.node.excerpt }} />
</a>
))}
</div>
</main>
</div>
);
}
export async function getStaticProps() {
const { data } = await client.query({
query: gql`
query Posts {
posts {
edges {
node {
title
databaseId
slug
excerpt(format: RENDERED)
}
}
}
}
`,
});
if (data.posts.edges === 0) {
return { notFound: true };
}
return {
props: {
posts: data.posts.edges,
},
revalidate: 10,
};
}
export default Home;
Then for the single post page:
/blog/[slug].js
import Link from "next/link";
import { gql } from "@apollo/client";
import client from "../../apollo-client";
export default function BlogPage(props) {
const { post } = props;
if (!post) {
return <p>Loading...</p>;
}
return (
<div>
<h1>{post.title}</h1>
<div dangerouslySetInnerHTML={{ __html: post.content }} />
<Link href="/">
<a>← back to home</a>
</Link>
</div>
);
}
export async function getStaticProps({ params }) {
const { slug } = params;
const result = await client.query({
query: gql`
query GetWordPressPostBySlug($id: ID!) {
post(id: $id, idType: SLUG) {
title
content
}
}
`,
variables: { id: slug },
});
if (!result.data.post) {
return { notFound: true };
}
return {
props: {
post: result.data.post,
},
revalidate: 10,
};
}
export async function getStaticPaths() {
const result = await client.query({
query: gql`
query GetWordPressPosts {
posts {
nodes {
slug
}
}
}
`,
});
return {
paths: result.data.posts.nodes.map(({ slug }) => {
return {
params: { slug },
};
}),
fallback: true,
};
}
When adding a new post it works, once I delete it, it does not get removed. This happens both when doing npm run dev
and npm run build
then npm start
I might be getting something wrong here in how ISR and revalidate works. Or I might be missing something in my code? Any help would be appreciated.
-- edit --
Meanwhile there are a couple of more threads, here on Stackoverflow and the Next.js github repository, related to what I'm experiencing. Related pages:
Next.js does not delete dynamic page deleted in CMS
https://github.com/vercel/next.js/issues/25470
Next.js ISR page not being deleted after deleting it in CMS
How to clear NextJs GetStaticPaths cache / "unpublish" a dynamic route?
https://github.com/vercel/next.js/discussions/18967
Next. js caching is automatically configured for you.
js is a minimalistic framework for server-rendered React applications as well as statically exported React apps. Manage your Next. js application content with a powerful headless CMS.
Next. js allows you to create or update static pages after you've built your site. Incremental Static Regeneration (ISR) enables you to use static-generation on a per-page basis, without needing to rebuild the entire site.
Incremental Static Regeneration (ISR) enables developers and content editors to use static-generation on a per-page basis, without needing to rebuild the entire site. With ISR, you can retain the benefits of static while scaling to millions of pages.
I am not sure if this matches your use-case, but in one of the projects I worked on, if the source data is deleted, we return a 404.
export async function getStaticPaths() {
// Get the paths we want to pre-render based on posts
// We do not need to generate anything during build
return {
paths: [],
fallback: true,
};
}
export async function getStaticProps({ params: { slug } }) {
// Run your data fetching code here
try {
const data = await getData(slug);
return {
props: { data, slug },
revalidate: 30,
notFound: !data,
};
} catch (e) {
return {
props: { data: null, slug },
revalidate: 30,
notFound: true,
};
}
}
Docs: https://nextjs.org/blog/next-10#redirect-and-notfound-support-for-getstaticprops--getserversideprops
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With