Skip to content
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

Add way to gatsby-source-filesystem for querying the "public" URL of a file #3538

Closed
KyleAMathews opened this issue Jan 15, 2018 · 4 comments
Labels
good first issue Issue that doesn't require previous experience with Gatsby

Comments

@KyleAMathews
Copy link
Contributor

This is a fairly common feature request. E.g. someone is using a frontmatter field in markdown to link to a file then wants to query that file to get public link to the file.

To do this, we'd need gatsby-source-filesystem to extend the default File schema to add a publicURL field https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-source-filesystem/src/gatsby-node.js

This would use https://www.gatsbyjs.org/docs/node-apis/#setFieldsOnGraphQLNodeType

When this field is queried, the graphql resolver should copy the file to public/static with the hash of the file added to its file name (similar to what we do with images) and return the new path.

@KyleAMathews KyleAMathews added the good first issue Issue that doesn't require previous experience with Gatsby label Jan 15, 2018
@xiaoxinghu
Copy link
Contributor

Is there a way to get publicURL within a transformer plugin? I have links to relative path files in the content (org-mode), I tried to get publicURL from file nodes, but got nothing. I assume it's not ready at that point ( in extend-node-type). I saw the implementation in gatsby-remark-copy-linked-files, it's good to not having to rewrite something similar, since gatsby-source-filesystem is already doing it.

@KyleAMathews
Copy link
Contributor Author

You could refactor the filesystem implementation so it's a function you could use in other plugins.

@xiaoxinghu
Copy link
Contributor

Had a look at this #3669 , the way gatsby-transformer-sharp was implemented is a easier way for me. but it is a little concerning, since it hard code the publicURL in there. If the original one (in filesystem) changed, it will break.

@pieh
Copy link
Contributor

pieh commented Feb 15, 2018

publicURL is field with resolver which means it will generate value (and copy file to build directory) on demand when queried and not really accessible with js code. You should propably look into refactoring that code into separate function and export it from gatsby-source-filesystem as @KyleAMathews suggested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Issue that doesn't require previous experience with Gatsby
Projects
None yet
Development

No branches or pull requests

3 participants