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

"off-main-thread" Scripts and other Head elements cannot be loaded conditionally in production #38454

Open
2 tasks done
dslovinsky opened this issue Aug 14, 2023 · 0 comments
Open
2 tasks done
Labels
status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer type: bug An issue or pull request relating to a bug in Gatsby

Comments

@dslovinsky
Copy link
Contributor

dslovinsky commented Aug 14, 2023

Preliminary Checks

Description

  1. Loading Script tags dependently should be possible using React state as documented here. Although this does indeed work for Script elements with the idle and post-hydrate strategies, it does not work for ones with the off-main-thread strategy. The script will populate the DOM, but will not fire (so any JS it contains will not run).
  2. Using state to conditionally load other elements in the HEAD such as meta tags also does not work and the elements will never populate the DOM. This is also true for changing attributes within elements, such as the value of a "content" attribute which will never take effect.

These issues do not occur in development - only when using gatsby build. They are not documented anywhere, which leads me to believe they may be unintended, but if they are intended they should be documented.
The reproduction link below contains examples of these issues in various places to demonstrate context dependence.

Reproduction Link

https://codesandbox.io/p/sandbox/compassionate-julien-hyxk5h

Steps to Reproduce

In the Codesandbox:

  1. Run yarn build && yarn serve
  2. Open the result in a new tab and inspect the DOM.

Expected Result

In the Codesandbox:

  1. window.Bizible should not be undefined, since it is defined by the Bizible script loaded in pages/index.tsx
  2. $.ajax should be defined when called from the console, since it is defined by the Jquery script loaded in the Jquery component referenced in gatsby-browser and gatsby-ssr
  3. The title, meta, and gstatic script tags should exist in Head as set in the SEO component.
  4. The content of the meta robots tag show be index as set in the SEO component
  5. The googlesyndication script should exist in head as set in the SEO component

Actual Result

  1. window.Bizible is undefined despite the Bizible script appearing in the DOM
  2. $.ajax is undefined despite the Jquery script appearing in the DOM
  3. The title, meta, and gstatic script tags do not exist anywhere in the DOM
  4. The content of the meta robots tag remains noindex indefinitely
  5. The googlesyndication script does not exist anywhere in the DOM

Environment

System:
    OS: macOS 13.5
    CPU: (8) x64 Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.13.0 - /var/folders/zf/k040lvhs137fvc2mrdx1pd400000gp/T/yarn--1691986380375-0.2794262227943085/node
    Yarn: 1.22.19 - /var/folders/zf/k040lvhs137fvc2mrdx1pd400000gp/T/yarn--1691986380375-0.2794262227943085/yarn
    npm: 8.19.3 - ~/.nvm/versions/node/v18.13.0/bin/npm
  Browsers:
    Chrome: 115.0.5790.170
    Safari: 16.6
  npmPackages:
    gatsby: ^5.11.0 => 5.11.0

Config Flags

No response

@dslovinsky dslovinsky added the type: bug An issue or pull request relating to a bug in Gatsby label Aug 14, 2023
@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Aug 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer type: bug An issue or pull request relating to a bug in Gatsby
Projects
None yet
Development

No branches or pull requests

1 participant