npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@gumgum/react-script-tag

v0.0.3

Published

A React replacement for the native script tag

Downloads

1,008

Readme

React Script Tag 💉

This react component is intended to be a drop-in replacement for the <script> html native tag. After you add it in any location of your react-app, the component will take care on appending the corresponding script tag to your app's document. It supports all the native attributes as well.

Keywords: Blazing fast 🔥, Minimal 📦, and Simple 🤖

Table of Content

Installation

You can install this package thru npm or yarn:

yarn install @gumgum/react-script-tag

Usage

You can use the Script component anywhere. Once it is mounted, the component will proceed to load your script.

import React from 'react';
import Script from '@gumgum/react-script-tag';

class MyApp extends React.Component {
    
    _onMyScriptLoad = () => {/* ... */};
    _onMyScriptError = () => {/* ... */};

    render() {
        return (
            <div>
                {/* Your App's code */}
                <Script
                    src="//url-to-your-site.com/script.js"
                    type="text/javascript"
                    onLoad={ this._onMyScriptLoad }
                    onError={ this._onMyScriptError }
                    async
                />
            </div>
        );
    }
}

export default MyApp;

It is recommended that the Script tag is placed in a component that only renders once in the entire life of your app. Otherwise, a new <script> tag will be appended each time the component mounts again. There are plans down the road to prevent this.

Examples

At GumGum, we usually wrap the Script component as follow, to facilitate adding 3rd-parties. Here is an example, on how we add Qualaroo:

import Script from '../common/ScriptLoader';
import React from 'react';

class QualarooLoader extends React.Component {
    _onCreate = () => {
        window._kiq = window._kiq || [];
    };
    
    _onSuccess = () => {
        const userStr = localStorage.getItem('user');
        const user = JSON.parse(userStr);
        if (!user) return;

        const email = user.email;
        window._kiq.push(['identify', email]);
    };
    
    _onError = error => {
        throw new Error(`Could not load ${error.outerHTML}`);
    };

    render() {
        return (
            <Script
                src="//s3.amazonaws.com/ki.js/<id>/fFn.js"
                type="text/javascript"
                onCreate={this._onCreate}
                onSuccess={this._onSuccess}
                onError={this._onError}
                defer
            />
        );
    }
}

export default QualarooLoader;

We strongly suggest using the attributes async and defer (depending on your situation). Here is a good explanation.

Then we call our new wrapper in our app:

import React from 'react';
import Qualaroo from 'QualarooLoader';

class MyApp extends React.Component {
    /* ... */
    render() {
        return (
            <>
                {/* Other Components */}
                <Qualaroo delayMs={500}/>
            </>
        );
    }
}

API

src

string | required

URI that specifies the location of your script.

delayMs

number | defaults to 0

Artifically adds a delay in milliseconds after the component mounts, but before the script tag is appended to the document. Useful for scripts that are not necessary early on, and may conflict on the browser's request-limit.

onCreate

function() | defaults to Function.prototype

A callback function that is called just right after the script tag has been appended to the document.

onLoad

function(event: Event) | defaults to Function.prototype

This function is called after the script has been successfully loaded.

onError

function(error: Event) | defaults to throw new URIError(...)

If there is a problem loading your script, this function is called.

Other Props ⚠️

As stated previously, this component supports all the attributes that the html script tag supports. You simply have to pass it as props to the <Script> component. In fact, any other prop that is not listed above will be appended as-is to the native script tag.

We have tested it with the following attributes: type, chartset, async, defer, crossOrigin, and noModule. (Everything else should work nevertheless).

Roadmap

  • [x] Document the component
  • [x] Write examples
  • [ ] Write tests
  • [ ] Prevent appending scripts twice when component re-mounts

Feel free to file an issue to suggest changes!

Contributors

Thanks goes to these people (emoji key):

| Jose Santos💻 📖 ⚠️ 👀 🐛 💡 🤔 | Serge Basile 🤔 | | :---: | :---: |

This project follows the all-contributors specification. Contributions of any kind welcome!

License

MIT