How to inject certain context into all pages in Gatsby?
Using gatsby-node.js and createPages I can query something using graphql and create pages using that something as page context. So those pages can use that something as parameter in their queries.
The problem I'm facing is that I don't want to use createPages. I'm completely fine with pages that are created by default (from gatsby-plugin-page-creator), I just want all of them to have something from graphql as a context.
Basically I want a global context (which I get from gatsby graphql) to be available for all pages.
There is onCreatePage hook but unfortunately graphql is not available there according to https://github.com/gatsbyjs/gatsby/issues/3121#issuecomment-348781341.
gatsby
add a comment |
Using gatsby-node.js and createPages I can query something using graphql and create pages using that something as page context. So those pages can use that something as parameter in their queries.
The problem I'm facing is that I don't want to use createPages. I'm completely fine with pages that are created by default (from gatsby-plugin-page-creator), I just want all of them to have something from graphql as a context.
Basically I want a global context (which I get from gatsby graphql) to be available for all pages.
There is onCreatePage hook but unfortunately graphql is not available there according to https://github.com/gatsbyjs/gatsby/issues/3121#issuecomment-348781341.
gatsby
add a comment |
Using gatsby-node.js and createPages I can query something using graphql and create pages using that something as page context. So those pages can use that something as parameter in their queries.
The problem I'm facing is that I don't want to use createPages. I'm completely fine with pages that are created by default (from gatsby-plugin-page-creator), I just want all of them to have something from graphql as a context.
Basically I want a global context (which I get from gatsby graphql) to be available for all pages.
There is onCreatePage hook but unfortunately graphql is not available there according to https://github.com/gatsbyjs/gatsby/issues/3121#issuecomment-348781341.
gatsby
Using gatsby-node.js and createPages I can query something using graphql and create pages using that something as page context. So those pages can use that something as parameter in their queries.
The problem I'm facing is that I don't want to use createPages. I'm completely fine with pages that are created by default (from gatsby-plugin-page-creator), I just want all of them to have something from graphql as a context.
Basically I want a global context (which I get from gatsby graphql) to be available for all pages.
There is onCreatePage hook but unfortunately graphql is not available there according to https://github.com/gatsbyjs/gatsby/issues/3121#issuecomment-348781341.
gatsby
gatsby
edited Nov 16 '18 at 9:24
MnZrK
asked Nov 16 '18 at 8:30
MnZrKMnZrK
586418
586418
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
What about this... you can try using the onCreateNode() hook instead inside your gatsby-nodejs file. E.g.:
const allMyPageNodes = ;
exports.onCreateNode = ( node, actions, getNode ) =>
const createNodeField = actions;
if(...) //whatever filtering you need to select JUST pages in your site
const mySpecialContext = "blah"; //whatever your global context settings are
createNodeField( node, name: "myglobal", value: mySpecialContext );
allMyPageNodes.push(node);
```
Then in all graphql queries thereafter, for pages, the graphql payload should have edge.node.fields.myglobal populated and you should be able to do whatever you like with it. Also have a look at https://www.gatsbyjs.org/docs/static-query/ for querying directly from inside a component.
HTH.
Thank you for suggestion. Unfortunately it won't work for me because the context that I need also should come from graphql, and I can't use graphql inside onCreateNode. Moreover, I need to be able to use this context as parameter for page query, not as payload.
– MnZrK
Nov 17 '18 at 8:30
add a comment |
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334054%2fhow-to-inject-certain-context-into-all-pages-in-gatsby%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
What about this... you can try using the onCreateNode() hook instead inside your gatsby-nodejs file. E.g.:
const allMyPageNodes = ;
exports.onCreateNode = ( node, actions, getNode ) =>
const createNodeField = actions;
if(...) //whatever filtering you need to select JUST pages in your site
const mySpecialContext = "blah"; //whatever your global context settings are
createNodeField( node, name: "myglobal", value: mySpecialContext );
allMyPageNodes.push(node);
```
Then in all graphql queries thereafter, for pages, the graphql payload should have edge.node.fields.myglobal populated and you should be able to do whatever you like with it. Also have a look at https://www.gatsbyjs.org/docs/static-query/ for querying directly from inside a component.
HTH.
Thank you for suggestion. Unfortunately it won't work for me because the context that I need also should come from graphql, and I can't use graphql inside onCreateNode. Moreover, I need to be able to use this context as parameter for page query, not as payload.
– MnZrK
Nov 17 '18 at 8:30
add a comment |
What about this... you can try using the onCreateNode() hook instead inside your gatsby-nodejs file. E.g.:
const allMyPageNodes = ;
exports.onCreateNode = ( node, actions, getNode ) =>
const createNodeField = actions;
if(...) //whatever filtering you need to select JUST pages in your site
const mySpecialContext = "blah"; //whatever your global context settings are
createNodeField( node, name: "myglobal", value: mySpecialContext );
allMyPageNodes.push(node);
```
Then in all graphql queries thereafter, for pages, the graphql payload should have edge.node.fields.myglobal populated and you should be able to do whatever you like with it. Also have a look at https://www.gatsbyjs.org/docs/static-query/ for querying directly from inside a component.
HTH.
Thank you for suggestion. Unfortunately it won't work for me because the context that I need also should come from graphql, and I can't use graphql inside onCreateNode. Moreover, I need to be able to use this context as parameter for page query, not as payload.
– MnZrK
Nov 17 '18 at 8:30
add a comment |
What about this... you can try using the onCreateNode() hook instead inside your gatsby-nodejs file. E.g.:
const allMyPageNodes = ;
exports.onCreateNode = ( node, actions, getNode ) =>
const createNodeField = actions;
if(...) //whatever filtering you need to select JUST pages in your site
const mySpecialContext = "blah"; //whatever your global context settings are
createNodeField( node, name: "myglobal", value: mySpecialContext );
allMyPageNodes.push(node);
```
Then in all graphql queries thereafter, for pages, the graphql payload should have edge.node.fields.myglobal populated and you should be able to do whatever you like with it. Also have a look at https://www.gatsbyjs.org/docs/static-query/ for querying directly from inside a component.
HTH.
What about this... you can try using the onCreateNode() hook instead inside your gatsby-nodejs file. E.g.:
const allMyPageNodes = ;
exports.onCreateNode = ( node, actions, getNode ) =>
const createNodeField = actions;
if(...) //whatever filtering you need to select JUST pages in your site
const mySpecialContext = "blah"; //whatever your global context settings are
createNodeField( node, name: "myglobal", value: mySpecialContext );
allMyPageNodes.push(node);
```
Then in all graphql queries thereafter, for pages, the graphql payload should have edge.node.fields.myglobal populated and you should be able to do whatever you like with it. Also have a look at https://www.gatsbyjs.org/docs/static-query/ for querying directly from inside a component.
HTH.
answered Nov 16 '18 at 21:06
yenyen
11110
11110
Thank you for suggestion. Unfortunately it won't work for me because the context that I need also should come from graphql, and I can't use graphql inside onCreateNode. Moreover, I need to be able to use this context as parameter for page query, not as payload.
– MnZrK
Nov 17 '18 at 8:30
add a comment |
Thank you for suggestion. Unfortunately it won't work for me because the context that I need also should come from graphql, and I can't use graphql inside onCreateNode. Moreover, I need to be able to use this context as parameter for page query, not as payload.
– MnZrK
Nov 17 '18 at 8:30
Thank you for suggestion. Unfortunately it won't work for me because the context that I need also should come from graphql, and I can't use graphql inside onCreateNode. Moreover, I need to be able to use this context as parameter for page query, not as payload.
– MnZrK
Nov 17 '18 at 8:30
Thank you for suggestion. Unfortunately it won't work for me because the context that I need also should come from graphql, and I can't use graphql inside onCreateNode. Moreover, I need to be able to use this context as parameter for page query, not as payload.
– MnZrK
Nov 17 '18 at 8:30
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334054%2fhow-to-inject-certain-context-into-all-pages-in-gatsby%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
