File System Route API: config()
API
#33789
Unanswered
LekoArts
asked this question in
Umbrella Discussions
Replies: 1 comment 1 reply
-
Heyo - excited for this feature. But wondering about the ability to use it as a way to pass context down the the page API. For example because I want to add a nextPostId and previousPostId I am "stuck" using the createPages API in gatsby-node. It would be great if we could also use the config to pass additional context down to the pages and queries besides the id that is currently passed. Not sure if this is even possible but here is a rough example of what I am thinking of. export async function config() {
// Get all posts that were created before 2020-10-31
const { data } = graphql`
{
allSanityPost(
filter: { slug: { current: { ne: null } } }
sort: { fields: date, order: DESC }
) {
nodes {
id
slug {
current
}
}
}
}
`
// Array of all posts
const allPosts = result.data.allSanityPost.nodes || []
// Okay so this isn't perfect or even working code that would work but something like this, basically getting out the id, nextId, and
// previousId
// Inside of gatsby-node this code would be inside of a loop, would have to probably do it inside of a filter here maybe with the slug
// as a param
const id = post.id
const next = index === post.length - 1 ? null : allPosts[index + 1]
const previous = index === 0 ? null : allPosts[index - 1]
// Return a function that when called will return a config for FS route pages
// (right now only `defer` is supported)
return ({ params }) => {
return {
// Defer pages older than 2020-10-31
defer: oldPosts.has(params.fields__slug),
// Pass this context down so it is available in the GraphQL query
context: {
id,
previousId: previous ? previous.id : undefined,
nextId: next ? next.id : undefined,
},
}
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Description
With Gatsby v4.1 we've introduced a new API for File System Route API:
config()
. See release notes.Please post your feedback regarding the usage of the API, any observations regarding performance (build speed etc.) and general Developer Experience (DX) of the feature.
We really appreciate any feedback and want to further improve the
config()
API. Thanks!Beta Was this translation helpful? Give feedback.
All reactions