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

feat(gatsby): Webpack API for dependencies #24903

Closed
wants to merge 22 commits into from
Closed

Conversation

pieh
Copy link
Contributor

@pieh pieh commented Jun 10, 2020

Description

This PR adds couple of APIs that will allow adding arbitrary "code" modules to pages via graphql data layer. Make sure to check limitations section for current state of this PR

context.pageModel.setModule

Function available in graphql resolvers (can be implemented via createSchemaCustomization / createResolvers hooks).

Signature:

context.pageModel.setModule({
  source: string,
  type?: "default" | "named" | "namespace",
  importName?: string
}): string

Arguments

  • source: Absolute path to module to be added. If user wants to add some package - require.resolve should be used to get absolute path
  • type: Defines type of import:
    • default - import A from "b"
    • named - import { A } from "b"
    • namespace - import * as A from "b"
  • importName: only used when type is set to named - specifies named import to use

Return value

context.pageModel.setModule returns string identifier that later will be used to get instance of a module in frontend code. You will want to return this identifier in resolver where you implement context.pageModel.setModule function.

getModule

This is frontend code function used to get instance of module. Function is imported from gatsby:

import { getModule } from "gatsby"

Signature

getModule(moduleId: string): any

Arguments

  • moduleId: getModule expect module identifier (returned by context.pageModule.setModule) as argument. Module identifier is purposely not easily guessable (similar to id in nodes)

Return value

Actual module - this can be React component or any kind of arbitrary code.

Example usage:

Check demo site - https://github.com/pieh/query-webpack-modules-demo-site/tree/0bba2e2158473689850fa9b6112c26dc6f6c8461 . In particular:

Limitations (currently)

  • As-is this PR supports using new APIs only in page queries. To support static queries we will need feat(gatsby): Load static queries by the Gatsby runtime instead of webpack #23837 to be finished and merged. Static queries are now supported, not yet very much testing done as there can be surprising chains of things (static query adds a module which has static query and this can repeat over and over again)
  • Hot data updates in gatsby develop are not working properly when new modules are added to a page - user does need to manually refresh browser to load new modules ( To handle this we will need feat(gatsby): Instrument partial writes to page data #24808 + some additional refactor on top ). EDIT: with state machine in place this is almost done, there are few more timing issues to solve (making long running process rewrite modules at proper time slots, we will need to put bit more things into state machine) this should be handled now. Due to various timing difficulties it might still not be 100% reliable (there are 2 levels - timing in node process, which is now nicely handled by state machine, but there is also timing in runtime - we receive webpack HMR updates and data updates without sync, so there is a code that tries to account for that (dev-loader changes that tries to wait for webpack updates)
  • This PR doesn't implement public facing registerModule action that would allow users/plugins to stabilize webpack builds (add module to bundle even if not used currently so we don't have to rewrite whole lot of html files because of webpack hash changes)

Trying it out

I published gatsby@query-webpack-modules that includes those changes - feel free to take it for a spin and let me know what you think

Documentation

❌ This draft PR lacks any documentation related to added new APIs.

Related Issues

Related to #18689

@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Jun 10, 2020
@sidharthachatterjee sidharthachatterjee removed the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Jun 10, 2020
@pieh pieh force-pushed the query-webpack-modules branch from 137f847 to a380be6 Compare June 10, 2020 11:08
@pieh pieh added the type: feature or enhancement Issue that is not a bug and requests the addition of a new feature or enhancement. label Jun 10, 2020
@pieh pieh mentioned this pull request Jun 10, 2020
@pieh pieh force-pushed the query-webpack-modules branch 2 times, most recently from 666fae2 to ab6b500 Compare June 20, 2020 11:56
@pieh pieh force-pushed the query-webpack-modules branch 6 times, most recently from b31b7b8 to 37ed9dc Compare June 25, 2020 00:51
@imarenco
Copy link

Thanks for contribution, We test that, and work :)

@gatsby-cloud-staging
Copy link

gatsby-cloud-staging bot commented Jul 14, 2020

Gatsby Cloud Build Report

gatsby-master

🎉 Your build was successful! See the Deploy preview here.

Build Details

View the build logs here.

🕐 Build time: 2m

Performance

Lighthouse report

Metric Score
Performance 💚 97
Accessibility 🔶 87
Best Practices 💚 93
SEO 🔶 73

🔗 View full report

@pieh pieh force-pushed the query-webpack-modules branch from 9f67207 to 78bb7e2 Compare July 16, 2020 12:52
@rasmuswoelk
Copy link

This is awesome 🚀

Tested and working! 💪🏼 Looking forward for this to be merged in!

Check it out on https://samhaeng.com (using wordpress-source-gatsby, advanced custom fields and flexible content)

@pieh pieh force-pushed the query-webpack-modules branch from 78bb7e2 to 7d13f07 Compare July 27, 2020 12:42
@gatsby-cloud-staging
Copy link

gatsby-cloud-staging bot commented Jul 27, 2020

Gatsby Cloud Build Report

gatsby
🎉 Your build was successful! See the Deploy preview here.

Build Details

View the build logs here.

🕐 Build time: 17m

Performance

Lighthouse report

Metric Score
Performance 💚 91
Accessibility 💚 100
Best Practices 💚 100
SEO 🔶 76

🔗 View full report

@pieh pieh force-pushed the query-webpack-modules branch 3 times, most recently from 9cad1a9 to 62ab61f Compare July 29, 2020 09:50
@gatsby-cloud
Copy link

gatsby-cloud bot commented Jul 29, 2020

Gatsby Cloud Build Report

client-only-paths

🎉 Your build was successful! See the Deploy preview here.

Build Details

View the build logs here.

🕐 Build time: 19s

Performance

Lighthouse report

Metric Score
Performance 💚 100
Accessibility 🔶 85
Best Practices 💚 100
SEO 🔶 70

🔗 View full report

@gatsby-cloud

This comment has been minimized.

@gatsby-cloud
Copy link

gatsby-cloud bot commented Jul 29, 2020

Gatsby Cloud Build Report

using-reach-skip-nav

🎉 Your build was successful! See the Deploy preview here.

Build Details

View the build logs here.

🕐 Build time: 20s

Performance

Lighthouse report

Metric Score
Performance 💚 100
Accessibility 💚 100
Best Practices 💚 100
SEO 🔶 82

🔗 View full report

@pieh pieh force-pushed the query-webpack-modules branch from cdfcfcc to 75ebf21 Compare October 9, 2020 08:21
@rasmuswoelk
Copy link

Any news? This feature is all I want for Christmas 😆🤞🏼🎅🏼

@pieh
Copy link
Contributor Author

pieh commented Nov 27, 2020

@rasmuswoelk Sorry, no news on this :(

This is unfortunately postponed for now due to refactors that aim to speed up time time it takes to start up development server and/or improve development experience taking priority. Some of those would be much harder to do if we got this feature in first (this feature does make a lot of things more complicated which would make it much harder for us to do those refactors).

@rasmuswoelk
Copy link

Hey @pieh 👋

No worries – Thanks for the update! 🙌🏼

I'm just eager to be able to use this feature 👍 (I'm building a large page builder based site that could benefit a lot from this feature)

Do you think it would be possible to merge the changes from the latest Gatsby version into this draft?

Thank you so much for your work. It's greatly appreciated!

@lunelson
Copy link

I am also excited for this feature to be in Gatsby core, but @rasmuswoelk if you need a page-builder capability now, this package—based on my experiments so far—works very well right now (it is a part of the WP GraphQL Gutenberg project):

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

@rasmuswoelk
Copy link

@lunelson This looks awesome! I will try it out soon 👌🏼

Thank you for sharing this! 🙌🏼

@Grsmto
Copy link

Grsmto commented Dec 28, 2020

development server and/or improve development experience taking priority

dev experience taking priority over end user experience? 😕 This should be merged!

@manuelJung
Copy link

@pieh any updates on this?

@wardpeet
Copy link
Contributor

This has been on hold. We've been focusing on the development experience with lazy images, query on-demand and ssr in develop. Currently, our focus is to speed up builds.

This feature has been moved to the backlog.

@lnikell
Copy link

lnikell commented Feb 11, 2021

Just out of curiosity, is there are any ETA when that PR could reach the stable version of Gatsby?

@rshackleton
Copy link

This has been on hold. We've been focusing on the development experience with lazy images, query on-demand and ssr in develop. Currently, our focus is to speed up builds.

This feature has been moved to the backlog.

Faster builds are nice, but shipping too much code to the end-user is a serious issue for client-side performance and PSI scores.

How likely is it that this PR or others related to lazy loading components, better code splitting, etc, will be brought in scope in the near future?

@SimonKlausLudwig
Copy link

Is there any update on this issue ?

@coreyward
Copy link
Contributor

Hey @wardpeet + @pieh, any chance that this might get some time again now that SSR has shipped? Maintaining performance with localization without having to remove static queries is tough, and this would help a lot.

@LekoArts LekoArts closed this Jul 20, 2022
@LekoArts LekoArts deleted the query-webpack-modules branch July 20, 2022 05:57
@phil-lgr
Copy link

Now that this is closed, is there another issue/discussion we can follow for this topic? @LekoArts Thanks

@LekoArts
Copy link
Contributor

We won't continue work on this feature, but you can expect an RFC about Partial Hydration support in the future which will help y'all ship less JS to the client.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature or enhancement Issue that is not a bug and requests the addition of a new feature or enhancement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.