Skip to content

Commit aca9a8e

Browse files
piehKyleAMathews
authored andcommitted
add basic documentation about publicURL field on File node (#3752)
* add basic documentation about copying files from File nodes to build directory * A few copy edits * Format
1 parent 8798f08 commit aca9a8e

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

docs/docs/adding-images-fonts-files.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,58 @@ production so you don’t need to worry about long-term caching of assets.
5151

5252
Please be advised that this is also a custom feature of Webpack.
5353

54-
An alternative way of handling static assets is described in the next section.
54+
Two alternative ways of handling static assets is described in the next sections.
55+
56+
## Query for `File` in GraphQL queries using gatsby-source-filesystem
57+
58+
You can query the `publicURL` field of `File` nodes found in your data layer to trigger copying those files to the public directory and get URLs to them.
59+
60+
Examples:
61+
62+
* Copy all `.pdf` files you have in your data layer to your build directory and return URLs to them:
63+
64+
```graphql
65+
{
66+
allFile(filter: { extension: { eq: "pdf" } }) {
67+
edges {
68+
node {
69+
publicURL
70+
}
71+
}
72+
}
73+
}
74+
```
75+
76+
* Copy post attachments defined in your Markdown files:
77+
78+
Link to your attachments in the markdown frontmatter:
79+
80+
```markdown
81+
---
82+
title: "Title of article"
83+
attachments:
84+
- "./assets.zip"
85+
- "./presentation.pdf"
86+
---
87+
88+
Hi, this is a great article.
89+
```
90+
91+
In the article template component file, you can query for the attachments:
92+
93+
```graphql
94+
query TemplateBlogPost($slug: String!) {
95+
markdownRemark(fields: { slug: { eq: $slug } }) {
96+
html
97+
frontmatter {
98+
title
99+
attachments {
100+
publicURL
101+
}
102+
}
103+
}
104+
}
105+
```
55106

56107
## Using the `static` Folder
57108

packages/gatsby-source-filesystem/src/extend-file-node.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = ({ type, getNodeAndSavePathDependency, pathPrefix = `` }) => {
1111
publicURL: {
1212
type: GraphQLString,
1313
args: {},
14+
description: `Copy file to static directory and return public url to it`,
1415
resolve: (file, fieldArgs, context) => {
1516
const details = getNodeAndSavePathDependency(file.id, context.path)
1617
const fileName = `${file.name}-${file.internal.contentDigest}${

0 commit comments

Comments
 (0)