I've created a React static query hook that requests site and page level meta data. At the moment I'm duplicating this code on every page build as 1 part of the query changes from page to page allProductsJson, allProductJson etc etc. I was wondering if it is possible to remove this duplication by using the querying capabilities more efficiently? I've read that Gatsby static queries don't support arguments, but maybe there's a way to query all meta and filter by page?
Example of a duplicated query hook:
import { graphql, useStaticQuery } from 'gatsby';
import pageMetadataSelector from 'utils/page-metadata-selector';
import siteMetadataSelector from 'utils/site-metadata-selector';
import getAbsoluteUrl from 'utils/get-absolute-url-path';
const QUERY = graphql`
{
pageMetadata: allProductsJson {
nodes {
metadata {
title
description
image {
publicURL
}
robots
}
}
}
site {
siteMetadata {
siteUrl
}
}
}
`;
const usePageMetadataQuery = ({ location }) => {
const { pageMetadata, site } = useStaticQuery(QUERY);
const pageData = pageMetadataSelector(pageMetadata);
const { siteUrl } = siteMetadataSelector(site);
return {
metadata: {
...pageData,
siteUrl: getAbsoluteUrl(siteUrl, location.pathname),
image: getAbsoluteUrl(siteUrl, pageData.image.publicURL)
}
};
};
export default usePageMetadataQuery;