gatsby: [gatsby-plugin-mdx]: Double images / blurry / wrong style
I am using gatsby-remark-images in my Gatsby MDX powered website. Not sure whether this was introduced during the last months, because it worked before, but after upgrading everything I have to set the following style manually to override the responsive Gatsby style to make it work in my markdown.
.gatsby-resp-image-background-image {
display: none !important;
}
Otherwise I will have a large white space before my image gets rendered.
My images are used with the normal format in my markdown: 
Dependencies:
"dependencies": {
"@mdx-js/mdx": "^1.0.21",
"@mdx-js/react": "^1.0.21",
"axios": "^0.19.0",
"disqus-react": "^1.0.6",
"dotenv": "^8.0.0",
"gatsby": "^2.13.6",
"gatsby-image": "^2.2.4",
"gatsby-link": "^2.2.0",
"gatsby-mdx": "^0.6.3",
"gatsby-plugin-catch-links": "^2.1.0",
"gatsby-plugin-feed": "^2.3.2",
"gatsby-plugin-google-analytics": "^2.1.1",
"gatsby-plugin-manifest": "^2.2.1",
"gatsby-plugin-offline": "^2.2.1",
"gatsby-plugin-react-helmet": "^3.1.0",
"gatsby-plugin-sentry": "^1.0.1",
"gatsby-plugin-sharp": "^2.2.3",
"gatsby-plugin-sitemap": "^2.2.1",
"gatsby-plugin-styled-components": "^3.1.0",
"gatsby-remark-autolink-headers": "^2.1.0",
"gatsby-remark-copy-linked-files": "^2.1.0",
"gatsby-remark-images": "^3.1.3",
"gatsby-source-filesystem": "^2.1.2",
"gatsby-transformer-remark": "^2.6.1",
"gatsby-transformer-sharp": "^2.2.1",
"prism-react-renderer": "^0.1.7",
"prismjs": "^1.16.0",
"react": "next",
"react-dom": "next",
"react-facebook-pixel": "^0.1.3",
"react-helmet": "~5.2.1",
"react-quora-pixel": "0.0.5",
"react-twitter-conversion-tracker": "^1.0.0",
"react-youtube": "^7.9.0",
"styled-components": "^4.3.2"
},
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 31
- Comments: 33 (23 by maintainers)
Links to this issue
Commits related to this issue
- Fixes some issues with responsive images Partially related to https://github.com/gatsbyjs/gatsby/issues/15486 — committed to pedrolamas/pedrolamas.com by pedrolamas 5 years ago
- Fix image bug Ref: - https://github.com/gatsbyjs/gatsby/issues/15486 - https://github.com/cwgw/gatsby-plugin-mdx-bug — committed to glennreyes/glennreyes.com by glennreyes 5 years ago
- fix bug # https://github.com/gatsbyjs/gatsby/issues/15486 — committed to adblock-thai/site by pakonda 5 years ago
- Add css fix for https://github.com/gatsbyjs/gatsby/issues/15486 — committed to sagarnasit/blog by sagarnasit 5 years ago
- Add gatsby-remark-images to list of plugins for gatsby-plugin-mdx Taken from this thread - https://github.com/gatsbyjs/gatsby/issues/15486 — committed to zgordon/gatsby by zgordon 5 years ago
- docs(gatsby-plugin-mdx): Add gatsby-remark-images to list of plugins for gatsby-plugin-mdx (#16251) * Add gatsby-remark-images to list of plugins for gatsby-plugin-mdx Taken from this thread - https... — committed to gatsbyjs/gatsby by zgordon 5 years ago
- Inlude gatsby-remark-images as a plugin to gatsby-plugin-mdx, per fix https://github.com/gatsbyjs/gatsby/issues/15486\#issuecomment-510153237 — committed to filoxo/blog by filoxo 5 years ago
- apply image fix https://github.com/gatsbyjs/gatsby/issues/15486 — committed to cryptowerk/docs.cryptowerk.com by i001962 5 years ago
- Fix weird MDX/remark images issue https://github.com/gatsbyjs/gatsby/issues/15486 — committed to pauloelias/pauloelias.com by deleted user 5 years ago
- Remove: workaround for now fixed gatsbyjs/gatsby#15486 — committed to rbeer/developer.centrifuge.io by rbeer 4 years ago
- Remove: workaround for now fixed gatsbyjs/gatsby#15486 — committed to rbeer/developer.centrifuge.io by rbeer 4 years ago
- add gatsby-transformer-remark with gatsby-remark-images workaround for https://github.com/gatsbyjs/gatsby/issues/15486 and also this might be useful in general? — committed to jannikbuschke/gatsby-antd-docs by jannikbuschke 5 years ago
So digging a little deeper, it looks like Gatsby only looks for “subplugins” at one specific path,
options.plugins.gatsby-plugin-mdxusesoptions.gatsbyRemarkPlugins. This is fine for transforming markdown as the plugin handles that itself, but Gatsby-specific api files likegatsby-browser.jsdon’t get loaded because Gatsby doesn’t know they exist.If you try this…
…everything works as it should.
Okay, I’ve tracked it down. Here’s a repo.
I think this is a problem with
gatsby-plugin-mdx, notgatsby-remark-images, and c4a7c40d63538704f8964cdeb1df8a04285e9b21 just made it visible.My guess is that
gatsby-plugin-mdxisn’t properly loading/running the gatsby api calls made bygatsby-remark-images.If you configure both
gatsby-plugin-mdxandgatsby-transformer-remarkto usegatsby-remark-imagesas a plugin, then everything works as expected.If you don’t use
gatsby-remark-imagesas agatsby-transformer-remarkplugin, or don’t usegatsby-transformer-remarkat all, then we see our issue.@citypaul Your config is not perfect if you use with round images
After finding some references, this is the config that works for me, by declaring
gatsby-remark-imagesconfig one more time outside of MDX plugin’s options.I can confirm that this happens to my site when updating
gatsby-remark-imagesfrom 3.1.2 to 3.1.3.@rwieruch if you add the styles back, I think things will look they way they did before.
The problem isn’t with
.gatsby-resp-image-background-image, it’s that theimgafter it should be absolutely positioned and it no longer is.Hm, this seems to still be an active bug, at least when using with
gatsby-remark-images-medium-zoom.I still needed to duplicate the plugin also to the
pluginsoption key - didn’t work without it being both inpluginsandgatsbyRemarkPlugins:https://github.com/JaeYeopHan/gatsby-remark-images-medium-zoom/pull/13
@rwieruch maybe consider reopening?
This issue has gotten some visibility as a single point of reference for this bug, and is even referred to in some starters:
https://github.com/hagnerd/gatsby-starter-blog-mdx/blob/fb1966a296bbfd9f5f06e2d065705b97f1cb123b/gatsby-config.js#L30-L31
@ChristopherBiscardi thank you for the detailed explanation. And thank you for all of the work on MDX in Gatsby!
You’ve made the point a few times that using MDX obviates the need for a bunch of the gatsby-remark plugins.
I’m kind of in over my head here, but I think a fourth option would be to just drop support for gatsby-remark-* plugins.
I’ve looked at your autolink-header and prism replacements at gatsby-mdx. It looks like they just inject components. As you envision them, do gatsby-mdx-* plugins ever touch the AST?
What if gatsby-mdx-* plugins optionally exported
transformRemarkandtransformRehypefunctions which could be added to the transformation pipeline? You could do a transformation and inject a component in the same plugin.All I really want out of gatsby-remark-images is to turn something like this
into something like this
<GatsbyImage fluid={/* image data generated from my-image.jpg */} />If we could use variables in static queries then I wouldn’t need a plugin. I could just write my own component around gatsby-image and import it directly in my mdx files.
In lieu of that, I could do what gatsby-remark-images does, but instead of inserting hardcoded html I could insert jsx and map it to
GatsbyImage.I wrote a simple plugin that does this and loaded it as a
vanillagatsbyRemarkplugin, then includedGatsbyImageas a component inMDXProvider. It’s naive but it gets the job done.If gatsby-mdx-* plugins could touch the AST then something like this could be bundled into a nice replacement for gatsby-remark-images.
I think the solution is to ensure that
gatsby-plugin-mdxis loading plugins properly. However, there’s a lot going on in the plugin and much of it is over my head.@ChristopherBiscardi, am I understanding the problem correctly? Is it reasonable to just rename
gatsbyRemarkPluginsasplugins?Yesterday I updated to the recent Gatsby versions and I was able to remove the workaround code. I think this is fixed.
@cwgw Thanks for narrowing this down (and to everyone else who helped). Gatsby’s handling of sub-plugins has always been special but I suppose it matters more now than it used to with the recent gatsby-remark-plugin release.
The
flattenedPluginslist that gets plugged into the redux store won’t have thegatsbyRemarkPluginsfrom thegatsby-plugin-mdxoptions because support for sub-plugins using thepluginsAPI key is hard-coded into theload-pluginsfunction (https://github.com/gatsbyjs/gatsby/blob/a364c3d8fb4a8bfe39bad6f203f2ea5218adaa31/packages/gatsby/src/bootstrap/load-plugins/index.js#L20-L38), which is why when the api-runner-ssr goes to execute thegatsby-ssrfunctions from that earlier commit (onRenderBody), it doesn’t find it unless the plugin is also specified inplugins.gatsby-plugin-mdxtechnically uses thepluginsarray for a prototype shortcodes and components sub-plugin supply implementation in it’swrapRootElement, but gatsby-remark-images is a no-op for the files gatsby-plugin-mdx uses for that so the double-inclusion happens to work in thegatsby-config.SO, I’m not sure what the best option is here. Adding support to
load-pluginsto load alternative keys likegatsbyRemarkPluginsseems like unnecessary complexity sincegatsby-plugin-mdxis the only plugin that I’m aware of that loads additional plugins like this. So additional complexity in core for one use case is a bit meh.The other option is to move
gatsbyRemarkPluginsto operate out ofplugins. This has the downside of being a technically breaking change (even though it can be done backward compatibly) and means we can’t use thepluginsarray for gatsby-mdx specific plugins (because we do some API smoothing over to run gatsbyRemark plugins as remark plugins and that turns out to be a bit complicated where we need to do things like pass in pieces of the node system and such).I think something that could have a large impact on this might be to work the gatsby-remark-* plugin API surface back into compatibility with the regular remark plugins, which would also have the benefit of unifying the ecosystem again. That would make our gatsbyRemarkPlugin compat support layer unnecessary and we could entertain overlaying the remark plugin… but I haven’t had a chance to work deeply on this yet, it is a big chunk of work, and will also possibly not totally solve the problem here.
I wonder what @gatsbyjs/core thinks about enabling arbitrarily keyed sub-plugins (see below for pseudo-code where
a,b, andcwould contain gatsby-ssr, etc files) to participate in the gatsby-* runners.I am experiencing this same issue. Is the only solution to us cwgw’s snippet?
Thank you for your help. I’ll try to isolate the issue with a repo.
Hi @karlhorky, thanks for the question. My comment was cryptic I suppose.
So: FIRST, note that my gatsby site inherits gatsby-remark-images from gatsby-theme-blog-core. So a couple of the ‘workarounds’ above (in gatsby-config.js) I’ve avoided.
I used styles to workaround the problem.
That seemed to do the trick.
On January 17th I noticed that all the images in my blog had disappeared. (https://scrum.works/articles) In fact, they had height of zero pixels (and my padding-bottom override was preventing span.gatsby-resp-image-wrapper from expanding to the height of the image. (gatsby-image-remark uses
padding-bottom: x%(where x is a ratio of the image’s width) to control the height of the container.Yesterday, I removed my workaround and, voila!, images appear normally now. So, I wonder if others in this thread have observed a major change lately in the output of this plugin.
I had an issue just now where Images pulled in with mdx files were still showing the blur. Here’s a screenshot:
The above screenshot was generated using this config:
Simply adding the
pluginskey fixes it:Final config:
However, I couldn’t find any reference to this in the docs (and I’m not sure if this is a good solution as it seems like a bit of a hack?)
We can’t deprecate support because that means migration for people using
gatsby-transformer-remarkbecomes harder. We still want switching costs from remark to mdx to be very low and people to replace theirgatsby-remark-*plugins at a pace and time that works for them.Also gatsby-remark-images is one of the few plugins that gatsby-mdx can not replicate without StaticQuery variables, which will come eventually but don’t currently exist.
We could likely allow gatsby-mdx-* plugins to add plugins to the
remarkPluginsandrehypePluginslists. This is a bit farther out since we have to solve the current gatsbyRemark backward compat support first. (the gatsby-mdx plugins were, and still are a prototype that is dark-shipped, so there’s more work to do).@KyleAMathews
cc/ @sidharthachatterjee with whom I was discussing this the other day. Should I create a new issue to track this? Seems like we have a couple options for how to do it. If we enable this, then we would also be able to enable remark-* plugins to use gatsby apis, getting us one step closer to having a solution where gatsby-remark-* plugins can be transparently used in the same place as remark plugins.
Yep, it’s supposed to. The commit message mentions the rationale. I’m sorry to hear this isn’t working for you, I will try to help find a resolution.
Possibly, I haven’t used
gatsby-plugin-mdxmyself. I had a quick look and it doesn’t seem to use thereplaceRendererAPI, so I’m not sure why the head component I added wouldn’t be rendered.Is there a starter or public repository that reproduces the problem?