-
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
fix(docs): improve Gatsby Image docs #13170
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e592d83
revisit import/static folder docs
1cb4767
add redirect for adding-images-fonts-files page
925243a
improve formatting in import doc, add link
1441fc0
remove stub from docs sidebar
1f16fdb
add start to using-gatsby-image docs overhaul
fd6c405
Merge remote-tracking branch 'origin/master' into docs-images
b823da5
rename/rearrange docs to be more descriptive
9c086a4
copy updates
00f2ee0
update comment to reflect page change
9c5f727
Merge remote-tracking branch 'origin/master' into docs-images
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
--- | ||
title: Images, Video, and Files | ||
title: Images, Files, and Video | ||
overview: true | ||
--- | ||
|
||
Gatsby provides multiple solutions for adding images, video, and files into your projects. This section will walk you through several common patterns for handling media in Gatsby projects. | ||
Gatsby provides multiple solutions for adding images, video, and files into your projects. This section will walk you through several common patterns for handling media with Gatsby. | ||
|
||
[[guidelist]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
--- | ||
title: "Importing Assets Directly Into Files" | ||
--- | ||
|
||
There are two major ways to import assets, such as images, fonts, and files, into a Gatsby site. The default path is to import the file directly into a Gatsby template, page, or component. The alternative path, which makes sense for some edge cases, is to use the [static folder](/docs/static-folder). | ||
|
||
## Importing assets with Webpack | ||
|
||
With Webpack you can **`import` a file right in a JavaScript module**. This tells Webpack to include that file in the bundle. Unlike CSS imports, importing a file gives you a string value. This imported file's value is the final path you can reference in your code, e.g. as the `src` attribute of an image or the `href` of a link to a PDF. | ||
|
||
To reduce the number of requests to the server, importing images that are less than 10,000 bytes returns a | ||
[data URI](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs) instead of a path. This applies to the following file extensions: `svg`, `jpg`, `jpeg`, `png`, `gif`, `mp4`, `webm`, `wav`, `mp3`, `m4a`, `aac`, and `oga`. | ||
|
||
Here's an example: | ||
|
||
```js | ||
import React from "react" | ||
import logo from "./logo.png" // Tell Webpack this JS file uses this image | ||
|
||
console.log(logo) // /logo.84287d09.png | ||
|
||
function Header() { | ||
// Import result is the URL of your image | ||
return <img src={logo} alt="Logo" /> | ||
} | ||
|
||
export default Header | ||
``` | ||
|
||
This ensures that when the project is built, Webpack will correctly move the images into the public folder, and provide us with correct paths. | ||
|
||
You can reference files in CSS to import them, too: | ||
|
||
```css | ||
.Logo { | ||
background-image: url(./logo.png); | ||
} | ||
``` | ||
|
||
Webpack finds all relative module references in CSS (they start with `./`) and replaces them with the final paths from the compiled bundle. If you make a typo or accidentally delete an important file, you will see a compilation error, just like when you import a non-existent JavaScript module. The final filenames in the compiled bundle are generated by Webpack from content hashes. If the file content changes in the future, Webpack will give it a different name in production so you don’t need to worry about long-term caching of assets. | ||
|
||
If you're using SCSS the imports are relative to the entry SCSS file. | ||
|
||
Please be advised that this is also a custom feature of Webpack. | ||
|
||
## Querying for a `File` in GraphQL using gatsby-source-filesystem | ||
|
||
You can also import files using GraphQL by querying for them in your data layer, which will trigger copying of those files to the public directory. Querying for the `publicURL` field of `File` nodes will provide URLs you can use in your JavaScript components, pages and templates. | ||
|
||
### Examples: | ||
|
||
1. Copy all `.pdf` files you have in your data layer to your build directory and return URLs to them: | ||
|
||
```graphql | ||
{ | ||
allFile(filter: { extension: { eq: "pdf" } }) { | ||
edges { | ||
node { | ||
publicURL | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
Reference those PDF files in a page with useStaticQuery: | ||
|
||
```jsx | ||
import React from "react" | ||
import { useStaticQuery, graphql } from "gatsby" | ||
|
||
import Layout from "../components/layout" | ||
|
||
const DownloadsPage = () => { | ||
const data = useStaticQuery(graphql` | ||
{ | ||
allFile(filter: { extension: { eq: "pdf" } }) { | ||
edges { | ||
node { | ||
publicURL | ||
name | ||
} | ||
} | ||
} | ||
} | ||
`) | ||
return ( | ||
<Layout> | ||
<h1>All PDF Downloads</h1> | ||
<ul> | ||
{data.allFile.edges.map((file, index) => { | ||
return ( | ||
<li key={`pdf-${index}`}> | ||
<a href={file.node.publicURL} download> | ||
{file.node.name} | ||
</a> | ||
</li> | ||
) | ||
})} | ||
</ul> | ||
</Layout> | ||
) | ||
} | ||
export default DownloadsPage | ||
``` | ||
|
||
2. Link to attachments in the frontmatter of your Markdown files: | ||
|
||
```markdown | ||
--- | ||
title: "Title of article" | ||
attachments: | ||
- "./assets.zip" | ||
- "./presentation.pdf" | ||
--- | ||
|
||
Hi, this is a great article. | ||
``` | ||
|
||
In an article template component file, you can then query for the attachments: | ||
|
||
```graphql | ||
query($slug: String!) { | ||
markdownRemark(fields: { slug: { eq: $slug } }) { | ||
html | ||
frontmatter { | ||
title | ||
attachments { | ||
publicURL | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
Read more about querying for files in [`gatsby-source-filesystem`](/packages/gatsby-source-filesystem/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe add a link to package gatsby-source-filesystem in the following paragraph...