Skip to content
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

[Question][Plugins] Loading assets on demand? #2681

Closed
ahmedelgabri opened this issue Oct 30, 2017 · 9 comments
Closed

[Question][Plugins] Loading assets on demand? #2681

ahmedelgabri opened this issue Oct 30, 2017 · 9 comments

Comments

@ahmedelgabri
Copy link
Contributor

I wrote gatsby-plugin-klipse but I'd love to load the scripts & style files on demand, only if the page requires them, so I wonder what is the best way to do this? or if there is another plugin that is already doing this so I can have a look?

@KyleAMathews
Copy link
Contributor

Here's one way I came up with in 30 seconds so use accordingly :-)

  1. Add a new field to MarkdownRemark that returns a boolean saying if there is or isn't a code block in the markdown content
  2. Don't auto-load the scripts/styles — instead, expose a module you can import (e.g. gatsby-plugin-klipse/load-client-resources)
  1. Then a Gatsby template using klipse would query if there's a code field or not and if there is, it would run the load-client-resources function which would obviously be idempotent.

@ahmedelgabri
Copy link
Contributor Author

But how would this work alongside gatsby-remark-prism for example, which overrides the node type https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-remark-prismjs/src/index.js#L33-L40?

@KyleAMathews
Copy link
Contributor

Put this plugin before prism so it runs first.

@ahmedelgabri
Copy link
Contributor Author

I was hoping for a more robust way, but I guess I'll do that. I hope people will read the README.md then :)

@KyleAMathews
Copy link
Contributor

Well feel free to come up with your own way :-)

@ahmedelgabri
Copy link
Contributor Author

The problem is not my specific use-case, it's the fact that plugins can override nodes that other plugins can depend on. Which can be a hassle to debug & even use by users, I understand that this is not an easy issue but maybe something to consider as an improvement in the future, if possible.

@KyleAMathews
Copy link
Contributor

Yes it's a poor design by Remark. Gatsby disallows plugins from mutating nodes for example for precisely this reason.

@ahmedelgabri
Copy link
Contributor Author

I have a couple of more questions if you don't mind :)

  1. Add a new field to MarkdownRemark that returns a boolean saying if there is or isn't a code block in the markdown content

This means I need to the plugin to be part of remark plugins because I'll need to manipulate the markdown AST right? Or there is another way to check for code blocks?

  1. Don't auto-load the scripts/styles — instead, expose a module you can import (e.g. gatsby-plugin-klipse/load-client-resources)
  2. Then a Gatsby template using klipse would query if there's a code field or not and if there is, it would run the load-client-resources function which would obviously be idempotent.

Not sure how can I load the assets from a plugin outside of gatsby-ssr.js which is what I'm currently doing using onRenderBody.

As for point 3, I assume this needs to be done on the user side, or can it be done inside the plugin still?

@KyleAMathews
Copy link
Contributor

Due to the high volume of issues, we're closing out older ones without recent activity. Please open a new issue if you need help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants