-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is it possible to have also non-component files under pages/ directory? #26554
Comments
Why would you want to do that? The |
Because I find that clean scalable architecture. I keep the files as close as possible to where they are used, instead of putting them in some shared directories, even though they are used only in a single place. I also do the same for components that are only used locally on some page - are not library components, which are shared between multiple pages. For example:
It's a possible solution, but I don't like this approach. It keeps things far from it's usage, which makes understanding the relations between files/resources and the maintenance harder. For example if I want to remove that page completely, in the current architecture, I just need to remove Another advantage is that when I'm browsing the code I have all (or at least more) the related files in one directory, instead of needing to jump between multiple different directories ( That being said I still have some stuff in shared directories like |
Not sure if this helps you, but I tend to handle this by having a e.g.
|
Yeah, it's a possible solution and thanks for sharing that. Generally it's the same approach as what I'm doing now, only I keep that structure under But first I want to wait for an answer to my question: Is what I'm doing an illegal thing, or a bug that could be addressed and fixed. If it's the first, I will use the same pattern as you. If it's a bug, then I'll just wait for a fix with some temporary fix for my failing build used in a meanwhile. |
Gatsby by default adds instance of https://www.gatsbyjs.com/plugins/gatsby-plugin-page-creator/ plugin pointing to But there is a way to add more to the ignore list (thanks to not really documented feature implemented in #17420) To make use of that feature you would need to add following to your const path = require(`path`)
module.exports = {
plugins: [
{
// this overwrites default instance of `page-creator` and adds custom `ignore` options
resolve: `gatsby-plugin-page-creator`,
options: {
// the path is important - it needs to point exactly to `<project-dir>/src/pages`,
// if it doesn't, it will then add another instance of the plugin
path: path.join(__dirname, `src/pages`),
ignore: [`someOtherFile.js`]
}
}
]
} The |
Thanks. That's a useful information. That assures me that Gatsby is meant to have only page component files in
// src/pages/page-1.js
export { default } from "~features/Page1"; // src/features/Page1/Page1.js
function Page1() {
(...)
}
export default Page1; // src/features/Page1/index.js
export { default } from "./Page1"; |
I just described current state of things and what you could try using existing features - there is always opportunity to add some additional / nicer ways of marking some files as not meant to be pages - but we would have to be careful about this to not cause any breaking changes here. First thing that comes to mind is to possibly use something like "directive" in the component file content (similar to gatsby/packages/gatsby/src/utils/validate-page-component.ts Lines 53 to 103 in 9456b98
So adding additional check for directive like that wouldn't add much of overhead. It does gets kind of tricky, because the error come from But this is just one idea, there likely are better (or just different) ways to improve DX here. |
Having separate directory |
I'm trying to follow this pattern but how do I deal with page level GraphQL? If I keep in at the page level I move it into the
|
I have been adding additional files just as @robertjk wanted to. Weird things have happened since. One of which: updating things in these files sometimes did not change what dev was presenting. I tried multiple things to get the dev ui to update:
Nothing helped. Until I found this issue and renamed my files to begin with underscores. Maybe this can help others encountering the weird problems. |
Summary
Is it really invalid to have a non-component JavaScript file anywhere under
src/pages/
?Relevant information
Today I separated some constants from a page component into a separate file, and put that file in the same directory as page component.
Directory structure
This is the structure of the directory with that page component:
File contents
And these are the contents of the files in there:
Now build fails
After doing that the build started to fail with the following error message:
As soon as I remove
someOtherFile.js
the build starts to succeed again. So I assume the problem is that Gatsby treatssomeOtherFile.js
as a page component and tries to build a page from it.Question
Is that expected behavior and what I'm doing is simply illegal, or is that a bug that should be reported and fixed?
The text was updated successfully, but these errors were encountered: