question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ISR with WPGraphQL Data

See original GitHub issue

I’m attempting to add incremental static regeneration (ISR) to the [slugParent]/[[slugChild]].js file in order to regenerate pages where the CMS content has changed.

ISR is triggered according to my build logs, but the page content never updates on the frontend after I’ve updated it within the backend of WordPress… maybe it’s not recognizing the WPGraphQL data as having changed? Any help at all would be appreciated!

[slugParent]/[[slugChild]].js

export async function getStaticProps({ params = {} } = {}) {
  const { slugParent, slugChild } = params;

  // We can use the URI to look up our page and subsequently its ID, so
  // we can first contruct our URI from the page params

  let pageUri = `/${slugParent}/`;

  // We only want to apply deeper paths to the URI if we actually have
  // existing children

  if (Array.isArray(slugChild) && slugChild.length > 0) {
    pageUri = `${pageUri}${slugChild.join('/')}/`;
  }

  const { page } = await getPageByUri(pageUri);

  // In order to show the proper breadcrumbs, we need to find the entire
  // tree of pages. Rather than querying every segment, the query should
  // be cached for all pages, so we can grab that and use it to create
  // our trail

  const { pages } = await getAllPages();

  const breadcrumbs = getBreadcrumbsByUri(pageUri, pages);

  return {
    props: {
      page,
      breadcrumbs,
    },
    revalidate: 10,
  };
}

export async function getStaticPaths() {
  const { pages } = await getAllPages();

  // Take all the pages and create path params. The slugParent will always be
  // the top level parent page, where the slugChild will be an array of the
  // remaining segments to make up the path or URI

  const paths = pages
    .filter(({ uri }) => typeof uri === 'string')
    .map(({ uri }) => {
      const segments = uri.split('/').filter((seg) => seg !== '');

      return {
        params: {
          slugParent: segments.shift() || '',
          slugChild: segments,
        },
      };
    });

  return {
    paths,
    fallback: 'blocking',
  };
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
kblizeckcommented, Dec 10, 2021

Hey @idpelago! Check out Setting a Fetch Policy, and I misspoke above it’s actually network-only, not network-first.

For example:

import { ApolloClient, HttpLink, InMemoryCache, gql } from '@apollo/client';

const QUERY_ALL_POSTS = gql`
	query AllPosts($count: Int) {
		posts(first: $count) {
			edges {
				node {
					id
					content
					date
					excerpt
					postId
					title
					slug
				}
			}
		}
	}
`;

const apolloClient = new ApolloClient({
	link: new HttpLink(),
	cache: new InMemoryCache(),
});

const data = await apolloClient.query({
	query: QUERY_ALL_POSTS,
	fetchPolicy: 'network-only',
});
1reaction
kblizeckcommented, Dec 3, 2021

Hey @colbyfayock! Thank you for the reply - turns out it was a caching issue HA! I’m using ApolloClient and it was just returning the cached Apollo results instead of the new data. Had to tweak my caching keys, and changed a few to network-first and ISR appears to be working as expected. Closing as a non-issue!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Security - WPGraphQL
Anything that is publicly exposed by WordPress is publicly exposed by WPGraphQL, and any data that requires a user to be authenticated to...
Read more >
Users - WPGraphQL
User Mutations​ ... WPGraphQL provides mutations (the ability to change data through GraphQL) for Users. WPGraphQL adheres to core access control rights, ensuring ......
Read more >
Intro to GraphQL - WPGraphQL
So, GraphQL is a specification for a Query Language for interacting with API data as a data graph. GraphQL isn't tied to any...
Read more >
Authentication and Authorization - WPGraphQL
These plugins make use of sending data in the Headers of requests and validating the credentials and setting the user before execution of...
Read more >
WPGraphQL Concepts
WPGraphQL has a “Model Layer”, or a series of PHP Classes that take WordPress objects, such as WP_Post , WP_Comment , etc and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found