Reputation: 3
We know that Gatsby builds pages in the directory src/pages/ automatically with the path of the same name as the .js file. For example, I have a file src/pages/example.js, when I run the command gatsby develop
in the terminal, the webpage will be available on the site “localhost:8000/example”.
However, given that I am not allowed to build the webpage with the path “/example”, is there a way to blacklist the path “/example”, stop the build/develop process whenever we try to build using a blacklisted path and report an error like “You are not allowed to build the page with the blacklist path: /example” in the terminal?
To give you more context, I now have some website built in Docusaurus running on the url “www.example-domain.com/example”. I want Gatsby to report an error when building the site on “www.example-domain.com/example” and tell the developer that this path is already in use so that the developer can change the name of example.js to other names like another-example.js.
Upvotes: 0
Views: 569
Reputation: 40104
I suppose you might be able to use the onCreatePage event for this purpose.
In your gatsby-node.js
:
exports.onCreatePage = ({ page, actions, reporter }) => {
// Page is an object which amongst other things contains the path
if (page.path.match(/example/) {
reporter.panic('Stop! in the name of love...')
}
}
Not sure wha the best approach is as to the action there. If you print to console it might be missed. You can process.exit(1)
or potentially rename the file. Perhaps this is enough to get you on right track.
Upvotes: 2