Skip to content

Instantly share code, notes, and snippets.

@lunelson
Last active May 20, 2021 18:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lunelson/9dfb9a6c4cac7d60175bb5c8ff1882e3 to your computer and use it in GitHub Desktop.
Save lunelson/9dfb9a6c4cac7d60175bb5c8ff1882e3 to your computer and use it in GitHub Desktop.
Solutions for Dynamic Modules in Gatsby

gatsby dynamic modules, solutions

the most viable solutions are here

loadable-components

strong general solution. try this one first.

https://loadable-components.com/ https://github.com/hector-del-rio/gatsby-plugin-loadable-components-ssr

gatsby-plugin-graphql-component

https://github.com/pristas-peter/gatsby-plugin-graphql-component

allows you to register components within graphQL schema, so that you can query them out in pages and use them

Gatsby Page Modules Draft

PR: gatsbyjs/gatsby#24903 demo: https://github.com/pieh/query-webpack-modules-demo-site/ published: gatsby@query-webpack-modules

  • context.pageModel.setModule is a function enabled in graphQL resolvers, so it would be accessed in gatsby-node.js inside the creatSchemaCustomization or createResolvers hooks

    context.pageModel.setModule({
      source: 'abs/path/to/module',
      type: "default" | "named" | "namespace",
      importName: 'nameOfImport' // only if type == 'name'
    }); // returns a string identifier
  • getModule is a function which can be imported from gatsby, and can retrieve the module by name

    import { getModule } from 'gatsby';
    
    const Component = getModule('uid-returned-from-setModule');
  • needs process.env.GATSBY_EXPERIMENTAL_QUERY_MODULES = 1 set, in gatsby-config.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment