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-script): On load callback, cache #35464

Merged
merged 10 commits into from
Apr 21, 2022

Conversation

tyhopp
Copy link
Contributor

@tyhopp tyhopp commented Apr 21, 2022

Description

Implements the ability to use an onLoad callback for scripts with sources. Usage looks like:

<Script
  src={scripts.three}
  strategy={ScriptStrategy.postHydrate}
  onLoad={(event: Event) => { console.log(`Hello world`) }}
/>

Limitations
The onLoad callback works only for scripts with sources using the post-hydrate or idle strategies. It does not work with inline scripts.

It also includes two other things:

  • A cache for scripts so that loading occurs only once in all the various navigation scenarios (see test cases)
  • Handling for edge case of pre-hydrate scripts loaded after client side navigation (e.g. via Gatsby link). In this case it will be treated effectively the same as post-hydrate scripts. This is a temporary fix and we should explore better solutions

Documentation

To be done in later PR(s).

Related Issues

[sc-48841]
[sc-49445]

@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Apr 21, 2022
@tyhopp tyhopp added topic: performance Related to runtime & build performance type: feature or enhancement Issue that is not a bug and requests the addition of a new feature or enhancement. topic: frontend Relates to frontend issues (e.g. reach/router, gatsby-link, page-loading, asset-loading, navigation) and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Apr 21, 2022
@tyhopp tyhopp added this to the Script Component milestone Apr 21, 2022
@tyhopp tyhopp mentioned this pull request Apr 21, 2022
12 tasks
@tyhopp
Copy link
Contributor Author

tyhopp commented Apr 21, 2022

There are a few broken e2e tests still in the gatsby script suite, will merge this first so it's not blocking and we can address later

@tyhopp tyhopp merged commit 767f56b into feat-script-component Apr 21, 2022
@tyhopp tyhopp deleted the feat-script-component-on-load-callback branch April 21, 2022 09:03
tyhopp added a commit that referenced this pull request May 9, 2022
* feat(gatsy-script): Initial on load callback implementation

* test(gatsby-script): Refactor e2e test structure

* refactor(gatsby-script): Fix eslint, ts errors

* feat(gatsby-script): Handle edge cases from Gatsby link navigation

* test(gatsby-script): Adjust e2e tests so they run successfully again

* feat(gatsby-script): On load callback e2e tests

* test(gatsby-script): Scripts with sources navigation e2e tests

* test(gatsby-script): Inline scripts navigation e2e tests

* test(gatsby-script): Adjust e2e tests, add comment on edge case

* test(gatsby-script): Fix unit tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: frontend Relates to frontend issues (e.g. reach/router, gatsby-link, page-loading, asset-loading, navigation) topic: performance Related to runtime & build performance 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.

1 participant