包详细信息

gatsby-script

gatsbyjs861.1kMIT2.15.0

An enhanced script component for Gatsby sites with support for various loading strategies

gatsby

自述文件

gatsby-script

An enhanced script component for Gatsby sites with support for various loading strategies.

You do not need to install this package directly, it is available in the main Gatsby package since gatsby@4.15.0.

See the documentation for full details.

Usage

import * as React from "react"
import { Script, ScriptStrategy } from "gatsby" // Re-exported from core

const GTM = `G-XXXXXXXXXX` // Example Google Analytics 4 identifier

// Example script sources for illustration
const scripts = {
  three: "https://unpkg.com/three@0.139.1/build/three.js",
  marked: "https://cdn.jsdelivr.net/npm/marked/marked.min.js",
  gtag: `https://www.googletagmanager.com/gtag/js?id=${GTM}`,
}

// Strategy prop is optional, defaults to post-hydrate
function IndexPage() {
  return (
    <main>
      <h1>Script component example</h1>
      <Script src={scripts.three} strategy={ScriptStrategy.postHydrate} />
      <Script src={scripts.marked} strategy={ScriptStrategy.idle} />
      <Script
        src={scripts.gtag}
        strategy={ScriptStrategy.offMainThread}
        forward={[`gtag`]}
      />
      <Script id="gtag-config" strategy={ScriptStrategy.offMainThread}>
        {`
          // Example configuration of Google Analytics for use in Partytown
          window.dataLayer = window.dataLayer || [];
          function gtag(){dataLayer.push(arguments)};
          gtag('js', new Date());
          gtag('config', ${GTM}, { send_page_view: false })
        `}
      </Script>
      <Script
        id="my-unique-id"
        dangerouslySetInnerHTML={{ __html: `alert('Hello world')` }}
      />
      <Script id="my-unique-id-2">{`alert('Hello world')`}</Script>
    </main>
  )
}

export default IndexPage

更新日志

Changelog: gatsby-script

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

2.14.0 (2024-11-06)

🧾 Release notes

Chores

  • update dependency del-cli to ^5.1.0 for gatsby-script #38504 (ddcc30b)

2.13.0 (2023-12-18)

🧾 Release notes

Note: Version bump only for package gatsby-script

2.12.0 (2023-08-24)

🧾 Release notes

Bug Fixes

2.11.0 (2023-06-15)

🧾 Release notes

Bug Fixes

2.10.0 (2023-05-16)

🧾 Release notes

Chores

2.9.0 (2023-04-18)

🧾 Release notes

Bug Fixes

Chores

2.8.0 (2023-03-21)

🧾 Release notes

Note: Version bump only for package gatsby-script

2.7.0 (2023-02-21)

🧾 Release notes

Note: Version bump only for package gatsby-script

2.6.0 (2023-02-07)

🧾 Release notes

Chores

2.5.0 (2023-01-24)

🧾 Release notes

Note: Version bump only for package gatsby-script

2.4.0 (2023-01-10)

🧾 Release notes

Note: Version bump only for package gatsby-script

2.3.0 (2022-12-13)

🧾 Release notes

Chores

2.2.0 (2022-11-25)

🧾 Release notes

Bug Fixes

2.1.0 (2022-11-22)

🧾 Release notes

Note: Version bump only for package gatsby-script

2.0.0 (2022-11-08)

🧾 Release notes

Features

Chores

1.9.0 (2022-09-27)

🧾 Release notes

Bug Fixes

Chores

1.8.0 (2022-09-13)

🧾 Release notes

Features

Chores

  • update dependency microbundle to ^0.15.1 for gatsby-script #36513 (80f6616)

1.7.0 (2022-08-30)

🧾 Release notes

Bug Fixes

1.6.0 (2022-08-16)

🧾 Release notes

Features

Bug Fixes

Chores

  • update dependency del-cli to v5 for gatsby-script #36289 (2ff3b09)

1.5.0 (2022-08-02)

🧾 Release notes

Chores

1.4.0 (2022-07-19)

🧾 Release notes

Features

1.3.0 (2022-07-05)

🧾 Release notes

Chores

  • Add gatsby-parcel-config & update gatsby-script #35978 (5feeea6)

1.2.0 (2022-06-21)

🧾 Release notes

Note: Version bump only for package gatsby-script

1.1.0 (2022-06-07)

🧾 Release notes

Features

  • Duplicate script callbacks if no injected script callbacks #35717 (f91f477)

Bug Fixes

  • Make load callback work when both load and error callbacks defined #35760 (b491560)
  • Adjust warning control flow #35721 (5f0b2fe)

Chores