Coder Social home page Coder Social logo

Scripts? about react-head HOT 11 OPEN

tizmagik avatar tizmagik commented on September 24, 2024 4
Scripts?

from react-head.

Comments (11)

wmertens avatar wmertens commented on September 24, 2024 5

The use case is both inline and linked scripts. The most important thing is that the scripts should only execute once when already rendered via SSR. I suppose checking the contents would be sufficient to ward against this?

Here's how I load ga:

/* global __PRODUCTION__ __CLIENT__ __CONFIG__ */
import React, {Fragment} from 'react'
import Helmet from 'react-helmet'
import {LocationListener} from 'plugins/react-router-deluxe'

const {gaId, gAdwordsId} = __CONFIG__

export const gtag = (...args) => global.gtag && global.gtag(...args)

const handlePageView = location =>
	gtag('config', gaId, {
		// eslint-disable-next-line camelcase
		page_path: location.pathname + location.search,
	})

// Don't render the scripts on the client, they
// should be loaded on initial html only
// Also https://github.com/nfl/react-helmet/issues/357
const GoogleAnalytics = () =>
	__CLIENT__ ? (
		<LocationListener onChange={handlePageView} />
	) : __PRODUCTION__ && gaId ? (
		<Fragment>
			<Helmet>
				{/* Putting in head because it also offers page speed tracking */}
				<script
					async
					src={`https://www.googletagmanager.com/gtag/js?id=${gaId}`}
				/>
				<script>
					{// Must be string to prevent React from messing with the code
					`window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments);}gtag('js',new Date());gtag('config','${gaId}');${
						gAdwordsId ? `gtag('config','${gAdwordsId}');` : ''
					}`}
				</script>
			</Helmet>
		</Fragment>
	) : (
		false
	)

export default GoogleAnalytics

and then I can just put <GoogleAnalytics /> somewhere on the page and it will take care of the rest. The globals are added by webpack.

I think this might also be useful on the client for when you need a third-party library: put a <Script src="url" onLoad={this.handleLibLoaded}/> in the component and react-head makes sure it's loaded only once, but new onLoads are called every time?

from react-head.

RichiCoder1 avatar RichiCoder1 commented on September 24, 2024 1

@tizmagik Would you still be game to take a PR on this? I might be interested.

from react-head.

wmertens avatar wmertens commented on September 24, 2024 1

Oh and for external scripts, caching will indeed prevent downloads, but it will still run the script every time. Most scripts already protect against that, so no big issue, and I can't think of workarounds that always work and don't impact preloading etc.

from react-head.

TrySound avatar TrySound commented on September 24, 2024

react-head will load it twice too. It's better to use another way of loading for scripts IMO.

@tizmagik What do you think?

from react-head.

wmertens avatar wmertens commented on September 24, 2024

from react-head.

tizmagik avatar tizmagik commented on September 24, 2024

Interesting! I think maybe we could figure out a way to support this, either by no-op’ing on the server or no-op’ing on the client perhaps?

@wmertens is your use case for inline scripts or linked scripts, or both? If you have a specific use case or some sample code snippets or something you could share that would be helpful to understand the specific use case better.

Will play around with some ideas and post back when I get a chance.

from react-head.

tizmagik avatar tizmagik commented on September 24, 2024

@RichiCoder1 — yea for sure, if you’re up for it! Thanks in advance 🙏

from react-head.

cmmartin avatar cmmartin commented on September 24, 2024

I'm also looking for this functionality and willing to contribute, but I'm not sure the solution to the double load.

For external scripts, caching should make it irrelevant, right?

Inlined scripts are problematic though. We can't not render them on the client or they won't be set when people navigate in the client. Maybe the server could pass some data to the window indicating an inline script is already loaded?

from react-head.

wmertens avatar wmertens commented on September 24, 2024

Inline scripts could be wrapped with something that makes sure they only run once, but that seems hard to do.
Alternatively, tell the user is up to them to handle that.

from react-head.

cameronbraid avatar cameronbraid commented on September 24, 2024

Another use case for script tag is to support <script type="application/ld+json">.

Is there a need to special case these, or can a Script head tag component be exposed for this purpose ?

from react-head.

cameronbraid avatar cameronbraid commented on September 24, 2024

For <script type="application/ld+json"> use case I added a Script tag in #115

from react-head.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.