PaulieScanlon/gatsby-mdx-embed

Youtube embed placed at top of window

Closed this issue Β· 25 comments

As seen on https://5e345ce46cdafd0009b0eb8d--gatsby-theme-nicky-blog.netlify.com/mdx-with-embeds

PR with relevant changes
NickyMeuleman/gatsby-theme-nicky-blog#3
Didn't notice it before, did after.

I upgraded from theme-ui v0.2 to v0.3. That resulted in me deleting my own MDXProvider and moving to shadowing the components file from gatsby-plugin-theme-ui

Thanks @NickyMeuleman could you also let me know what OS and Browser you’re using?

OS:

  • Windows 10

Browser:

  • Chrome Version 79.0.3945.130 (Official Build) (64-bit)
  • Firefox 73.0b12 (64-bit)
  • Microsoft Edge 44.19041.1.0

Developing on WSL2.
Output from gatsby info

  System:
    OS: Linux 4.19 Ubuntu 18.04.3 LTS (Bionic Beaver)
    CPU: (4) x64 Intel(R) Core(TM) i5-2500K CPU @ 3.30GHz
    Shell: 5.4.2 - /usr/bin/zsh
  Binaries:
    Node: 12.4.0 - ~/.nvm/versions/node/v12.4.0/bin/node
    Yarn: 1.17.3 - /usr/bin/yarn
    npm: 6.9.0 - ~/.nvm/versions/node/v12.4.0/bin/npm
  Languages:
    Python: 2.7.15+ - /usr/bin/python
  npmPackages:
    gatsby-cypress: ^0.2.8 => 0.2.21

Thanks @NickyMeuleman πŸ‘

@NickyMeuleman and one more thing. Can you tell me what other Gatsby plugins you’re using. I had a similar issue recently when this plugin was used with gatsby-plugin-feed

It's quite the list.
The theme is opensourced, please code-dive!
https://github.com/NickyMeuleman/gatsby-theme-nicky-blog

Tried installing @mdx-js/react again.
Since the gatsby-mdx-embed docs require it https://gatsby-mdx-embed.netlify.com/#install-peer-dependencies.
Locally, it seemed to work. Published, it doesn't.

Theme-ui migration guide https://theme-ui.com/migrating

@emotion/core and @mdx-js/react are now dependencies of theme-ui and should not be installed separately. If you'd like to use a particular version of each library, use Yarn resolutions.

Is that "should" meant as a forbidding "don't do this"
or as a "it's no longer necessary, but you can if you want to"?
//cc @jxnblk what would be the preferred option here?

edit: hit the "clear cache and rebuild" button on Netlify
Result: https://gatsby-theme-nicky-blog.netlify.com/mdx-with-embeds
So still broken after deploy but not anymore locally.
edit2: Figured out it only breaks when directly visiting the link and not when navigating to it from the site.

Might it be caused by the gatsby-browsers of gatsby-plugin-theme-ui and the one of this plugin fighting?

This is the code there
https://github.com/system-ui/theme-ui/blob/master/packages/gatsby-plugin-theme-ui/src/provider.js
If I understand correctly the components there are basically the same thing as the components prop the MDXProvider takes.

edit: Worked on this more today and it's pointing towards my hunch being correct, I think?
It happens with gatsby build and gatsby serve but NOT in gatsby dev

Just pushed a workaround that solves it, but feels like a hack since this plugin is already doing this (it should and somehow it doesn't seem like it's working).
By manually wrapping everything in my layout with the exported MdxEmbedProviderfrom this plugin.
NickyMeuleman/gatsby-theme-nicky-blog@3d3ecd8

This is why I mentioned it feeling wrong, because the plugin is already doing it:
https://github.com/PaulieScanlon/gatsby-mdx-embed/blob/master/%40pauliescanlon/gatsby-mdx-embed/src/gatsby-browser.js#L2

Just came in to mention that this was happening to me too...

@NickyMeuleman @ehowey Hey!

yeah this is a bit of tricky one.

The method i'm using in gatsby-ssr, wrapRootElement is supposed to provided .mdx files what they need in order to correctly pass and render the components defined in this plugin.

It seems that when this plugin is used with other plugins that use the same approach, they clash.

I've updated the README with a little code snippet showing how to manually import and use the MdxEmbedProvider Hopefully this will get you out of a fix while i investigate the problem.

Hey @NickyMeuleman i've created a PR for you #5 I've think i've now fixed the issue and your theme now displays YouTube embeds correctly without the need to manually wrap your content with the MdxEmbedProvider

@ehowey and @spences10 you might also like to upgrade your projects to 0.0.17

This fix should now mean once you've installed the plugin as a dependancy and added it to gatsby-config.js you won't need to do anything else. @spences10 you can now safely remove the manually imported MdxEmbedProvider from your blog post template.

Lemme know if any of you are still experiencing issues.

Cheers!

Hey @PaulieScanlon I jus got the update come in via renovate and the build is failing:

image

I'll go ahead and remove the wrapping provider and see what happens. πŸ‘

@spences10 Hmmm... that's annoying! i wonder if it's something to do with my babel config. and the use of uppercase letters in the file name... hold tight!

@spences10 ok, can you try upgrading to 0.0.17 maybe something weird is going on with that file. it's named Provider but exports as provider i've renamed it now so maybe that'll fix it?

@spences10 Oh also, it's worth pointing out that @mdx-js/mdx is now also a peer dependency, in case you don't have that installed but i suspect you do as you're using gatsby-plugin-mdx
README

Yeah, .17 built and no issues with banner video!

Thanks @PaulieScanlon πŸ™

@spences10 Whey!!! thanks for letting me know! man that was a ball ache! glad it's fixed! might wait for @NickyMeuleman and @ehowey to feedback before i close this issue!

Confirm working for my situation on Firefox and Edge (Chromium)

Thanks @PaulieScanlon! Just merged that PR and can confirm the issue is gone.

What do you think caused it? I noticed you changed export syntax for gatsby-srr and gatsby-browser from ESM to CommonJS, could that be related?

@NickyMeuleman Sweet! thanks for letting me know!

Yeah it's something to do with ES6 and CommonJS module exports in gatsby-ssr.js and perhaps the name of the component i was attempting to use for wrapRootElement.

gatsby-browser.js seems to always be ok with ES6 but gatsby-ssr.js appears to prefer CommonJS πŸ€·β€β™‚οΈ I guess because it runs server side it’s node rather than Babel that’s dealing with the Js?

I've updated my 100DaysOfGatsby blog post about what i changed here

I'll test it tonight Paul - thanks for all your work on this!

@ehowey no worries man! Thanks to all you guys for testing it out and finding the bugs! @NickyMeuleman @spences10

Fixed for me too.

One thing I noticed though - and I don't think this is in your control - is that including the embeds is expensive from a web performance perspective. Went from almost straight 100s with pure markdown to some low 90s with Twitter, and with Youtube it dropped it down to a 60-70. All scores in Lighthouse. Gatsby Cloud makes it easy to see this in real time.

@NickyMeuleman @ehowey thanks for your feedback. Yes, i suspect those provider shortcodes are the cause, and yes, it's slightly out of my control, those provide shortcodes kinda are what they are. I'll look into the lite-youtube-embed though that is at least a step in the right direction. I think that resolves this pesky bug now so i'll close this issue but please do open issues if you find anything else not working! thanks team!