Comments (6)
@fabien0102 that's super helpful. Thanks for the response. I invested a fair amount of time this weekend to figure out how the parts work together and am looking forward to deploying a site based on your starter. Using /___graphql?query= is huge time saver. Thanks again.
from gatsby-starter.
@i001962 If you have a specific semantic for each images, you can separate your queries to give a name to each image.
Two seperate queries (not tested, but you should have the idea)
fragment ImageFragment on ImageSharp {
edges {
node {
id
... on ImageSharp {
responsiveResolution(width: 1000, height: 600) {
src
srcSet
}
}
}
}
}
query ProductsPage {
laborRateImg: ImageSharp(filter: {id: {regex: "/LaborRate/"}}) {
...ImageFragment
}
scenarioImg: ImageSharp(filter: {id: {regex: "/Scenario/"}}) {
...ImageFragment
}
}
or you can build a little helper function to get your specific image (with your previous query)
const getImage = (images, name) => {
/* find your image by name and return the good one */
return {src: "", srcSet: ""}
}
Lastly, you need to use the srcset
attribute like this <img src={cover.src} srcset={cover.srcSet} />
if you want to have all resolutions availables ;)
Regarding "good practices", just find your way to do this in your context (something simple to maintained and understand) and stick with it š
from gatsby-starter.
@fabien0102 got it! The Two seperate queries approach is working nicely. Thanks for the generous reply and highlighting the missing srcset attribute. Iām making progress and starting to figure out more on my own so hopefully these questions turn into PRs before too long.
Next area for me is figureing out how to replicate the blog page/post functionality (with tagcards) to pull from csv instead of md. Almost got it!
from gatsby-starter.
Hello @i001962
First of all, carefull my starter is not really up to date with the last version of gatsby! (so you can have some misalignment with the current official gatsby documentation).
You can find have some image insertion example into the blogpost page:
The query:
https://github.com/fabien0102/gatsby-starter/blob/master/src/pages/blog.tsx#L133
The usage into a react component:
https://github.com/fabien0102/gatsby-starter/blob/master/src/pages/blog.tsx#L35
Logically, every images inside your project is parse by the gatby plugin here: https://github.com/fabien0102/gatsby-starter/blob/master/gatsby-config.js#L53
So you can have access to your image in multiple resolutions (thanks to sharp).
The "easier" way to understand what happen is to add an image in your project (where you want, the choice is on your side ;) ) and go to the graphQL playground tools (the url is on the console when you start the server). After you should have access to your image into allImageSharp
(or something like this).
I will try to make you a better tutorial if needed ^^ I just have switched to a new computer so I'm trying to give you an idea my memories š
But yes, the main idea is to use graphql to get your image, so you can use the responsiveResolution
and all optimisations from the Sharp plugin.
from gatsby-starter.
@fabien0102 Looks like I do need a bit more in the way of an image insertion tutorial. If you wouldn't mind, please assume there are more than one image per page.
Here's how I was thinking about it and where I got stuck:
1 - Create images directory and add a few images - done ImageSharp will see those at build time.
2 - using the graphiql query figure out the graphql to select the images for the new page (let's call this 'products' and assume it's generated with plop.
3 - This query return the images I want to insert into the new products page
query ProductsPage {
pageimages: allImageSharp(filter: {id: {regex: "/siteimages/"}}) {
edges {
node {
id
... on ImageSharp {
responsiveResolution(width: 1000, height: 600) {
src
srcSet
}
}
}
}
}
}
Returns:
{
"data": {
"pageimages": {
"edges": [
{
"node": {
"id": "/Users/Admin/aws/gatsby1/gatsby-starter-CTY/data/siteimages/LaborRates.png absPath of file >> ImageSharp",
"responsiveResolution": {
"src": "/static/LaborRates-5f6e68bffcbed1be89ef06216f9d960d-536e4.png",
"srcSet": "/static/LaborRates-5f6e68bffcbed1be89ef06216f9d960d-536e4.png 1x"
}
}
},
{
"node": {
"id": "/Users/Admin/aws/gatsby1/gatsby-starter-CTY/data/siteimages/ScenarioPlanning.png absPath of file >> ImageSharp",
"responsiveResolution": {
"src": "/static/ScenarioPlanning-ba0d253e43a3dee5750f2eb0a2c7e3ce-9d697.png",
"srcSet": "/static/ScenarioPlanning-ba0d253e43a3dee5750f2eb0a2c7e3ce-9d697.png 1x"
}
}
}
]
}
}
}
4 - Here's the first place I get thrown off as your example is using then MarkdownRemarkConnection and I'm not. Can we just use this?
import { ImageSharp } from "../graphql-types";
I'm trying to modify your line of code but I can't quite figure this out as there is no 'frontmatter' equivelant in this case. I see what needs to be done I just don't know how to get there.
**const cover = get(frontmatter, "image.children.0.responsiveResolution", {});**
Any guidance would be appreciated.
from gatsby-starter.
@fabien0102 sometimes just writing down the problem helps me solve it.
Here's what I did to get it to work. I'm stilling trying to figure out the best practice (if you will) for multiple pages and multiple images per page but... I'm unstuck for now.
import { ImageSharp } from "../graphql-types";
...
const pageimages = props.data.pageimages.edges;
console.log(pageimages[0].node.responsiveResolution.src);
const cover = pageimages[0].node.responsiveResolution as ImageSharp;
...
<img src={cover.src} height="618" width="1000"></img>
from gatsby-starter.
Related Issues (20)
- Cannot read tags group. HOT 4
- Create branch for Gatsby v2 HOT 12
- There are conflicting field types in your data. GraphQL schema will omit those fields. HOT 1
- Tags-Page.tsx template and blog.tsx page - corner case HOT 1
- Next branch error eslint-config-react-app missing HOT 3
- error GraphQL Error Unknown field `responsiveResolution` on type `ImageSharp` HOT 2
- how to remove disqus comment section with facebook comments
- what goes in siteMetaData googleVerification HOT 2
- Move `next` to `master`, move current `master` to `v1` HOT 2
- Repo description still says V1 instead of V2 HOT 1
- gatsby-browser and gatsby-ssr need to be Javascript HOT 1
- Allot of vulnerabilities in this repo HOT 2
- Styles from styled components not loading in production, SSR issue?
- Not work with node v11.11.0 && npm v6.7.0 && gatsby v2.4.10 HOT 1
- npm ERR! notarget No matching version found for gatsby@next HOT 3
- License? HOT 1
- Create a new site fails HOT 2
- [Suggestion] replace storybook-readme with Docs Addon HOT 1
- [Idea][Feature Request] Netlify CMS?
- Creating or overriding semantic-ui themes HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
š Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ššš
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ā¤ļø Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gatsby-starter.