Reputation: 83
I am trying to learn Gatsby by writing a plugin using the lifecycle API. I am using the Gatsby default starter and I put in my local plugin's gatsby-node.js
some debug statements. Here's the output of gatsby build
:
- creating node: [Site] children: []
- creating node: [Directory] children: []
- creating page: [/404/] json name [404-22d]
- creating page: [/] json name [index]
- creating page: [/page-2/] json name [page-2-fbc]
- creating node: [SitePage] path: [/404/] children: []
- creating node: [SitePage] path: [/] children: []
- creating node: [SitePage] path: [/page-2/] children: []
- creating page: [/404.html] json name [404-html-516]
- creating node: [SitePage] path: [/404.html] children: []
Here's my gatsby-node.js
in my local plugin:
function onCreateNode({node, loadNodeContent}) {
let message = `- creating node: [${node.internal.type}]`;
if (node.path) {
message += ` path: [${node.path}]`;
}
if (node.children) {
message += " children: " + JSON.stringify(node.children);
}
if (node.owner) {
message += ` owner: [${node.internal.owner}]`;
}
console.log(message);
}
function onCreatePage({page, actions}) {
let message = `- creating page: [${page.path}] json name [${page.jsonName}]`;
console.log(message);
}
exports.onCreateNode = onCreateNode;
exports.onCreatePage = onCreatePage;
I don't understand the followings:
/404/
and the other for /404.html
?creating page
precedes creating node
). Why is that? Shouldn't it be the other way around? (maybe the callbacks are done in a different order?)Thanks in advance.
Upvotes: 1
Views: 707
Reputation: 11577
By default all gatsby generated pages, except for index
, is put in a folder with a index.html
file in it, i.e my-domain.com/page1
will have a page1
folder with index.html
in it. Why? I'm guessing that if someone turns off javascript they'd still get the nice url (my-domain.com/page1/
instead of my-domain.com/page1.html
.
The reason why 404
is copied from 404/
to 404.html
is because
many static site hosts expect site 404 pages to be named this. — source
This has tripped me off a few times too & I'm still not too sure why. The createPages
api is definitely called after sourceNodes
, so here's my guess: onCreateNode
& onCreatePage
are triggered asynchronously; however, gatsby will call these hooks of each individual plugin serially. Perhaps by the time it's your turn (your gatsby-node.js
hooks will be called last), gatsby has already moved on to the next steps (again, just speculation).
Also, keep in mind that when createPage
is run, gatsby doesn't actually write out the page with data, it just keeps track of the page's metadata stuff.
Here's some helpful resouces:
Not really. The user are the one who creates relationship between node and page by writing graphql queries that request data for a page.
By default, gatsby includes this plugin for all site, which turns files in src/pages/
into pages. User'd still have to query the data for those pages themselves.
So except for the node created for each pages by the internal plugin data bridge, there's no relationship between node & page.
Upvotes: 3