accessing File's publicURL field resolver without using gatsby-source-filesystem for local content #4993
Replies: 12 comments
-
With this and #4904, maybe there's a case for useful plugin tools? |
Beta Was this translation helpful? Give feedback.
-
Agree about splitting out |
Beta Was this translation helpful? Give feedback.
-
I nearly wrote something similar. 👍 to a single purpose package. |
Beta Was this translation helpful? Give feedback.
-
Due to the high volume of issues, we're closing out older ones without recent activity. Please open a new issue if you need help! |
Beta Was this translation helpful? Give feedback.
-
I recently ran into this problem while writing a custom source plugin for a custom API of mine. I have some images that I wanted to import via my source plugin, so I used I can confirm that the "hack" (of using the plugin and pointing it at the config file) that @pieh suggests works perfectly fine. But I really dislike the fact that I can't bundle it up into my custom plugin. I would need to instruct others to modify their own gatsby config. To work around this I've implemented a The following code adds a custom const path = require('path')
const fs = require('fs-extra')
exports.createResolvers = ({ createResolvers, pathPrefix }) => {
const publicStaticDir = path.join(
process.cwd(),
'public',
'static'
)
createResolvers({
File: {
publicUrl: {
type: 'String',
description: `Copy file to static directory and return public url to it`,
resolve: async src => {
const { absolutePath } = src
const { ext, name } = path.parse(absolutePath)
const filename = name + "--" + src.internal.contentDigest + ext
const newPath = path.join(publicStaticDir, filename)
try {
const exists = await fs.exists(newPath)
if (!exists) await fs.copy(absolutePath, newPath)
return `${pathPrefix}/static/${filename}`
} catch (e) {
console.error(`error copying file from ${absolutePath} to ${newPath}`, e)
return null
}
}
}
}
})
} |
Beta Was this translation helpful? Give feedback.
-
As I'm working on this, I realized there is another workaround. When you're already creating a plugin, or you just have a I got this idea when reading the source code of In my own plugin, which already requires exports.setFieldsOnGraphQLNodeType = require("gatsby-source-filesystem/extend-file-node") |
Beta Was this translation helpful? Give feedback.
-
One thing you can do right now (that wasn't possible back when this issue was first created) is to add Gatsby started to support nested gatsby-config files in I would not advise to import internals from |
Beta Was this translation helpful? Give feedback.
-
Yeah pointing to internals isn't the greatest idea. But it beats implementing the I actually tried putting a After removing the import to the internals of module.exports = {
plugins: [
{
resolve: "gatsby-source-filesystem",
options: {
path: `${__dirname}/gatsby-config.js`,
},
}
]
} I first ran
|
Beta Was this translation helpful? Give feedback.
-
@pieh any idea as to why the nested config isn't working? |
Beta Was this translation helpful? Give feedback.
-
Hiya! This issue has gone quiet. Spooky quiet. 👻 We get a lot of issues, so we currently close issues after 30 days of inactivity. It’s been at least 20 days since the last update here. Thanks for being a part of the Gatsby community! 💪💜 |
Beta Was this translation helpful? Give feedback.
-
I whipped up a plugin that packages the extend-file-node logic from Also has a few options for fingerprinting and filtering by media type |
Beta Was this translation helpful? Give feedback.
-
I confirm that https://www.gatsbyjs.org/packages/gatsby-transformer-remote-filesystem/ is an excellent temporary solution (until this issue is properly solved by Gatsby) also for files sourced from Drupal. |
Beta Was this translation helpful? Give feedback.
-
When using wordpress or drupal source plugins and not using gatsby-source-filesystem for local files
publicURL
field is not available. To get access to it we need to add gatsby-source-filesystem to gatsby-config.js even if we don't need it to pull local files data. My issue is that we need to specifypath
for gatsby-source-filesystem to watch. I've used this entry as a kind of workaroundPossible solutions:
publicURL
field resolver to separate plugingatsby-source-filesystem
to skip watching filesI think moving to separate plugin makes more sense because in situation described above we use that plugin not as a source but as extender of
File
node (so this is mostly based on source/transformer convention). After moving it to separate plugin we could maintain this feature in gatsby-source-filesystem by depending on newly created package to not break sites using it.Thoughts?
Beta Was this translation helpful? Give feedback.
All reactions