How do I remove CSS from a website that loads a script tag, and how do I remove that script tag?


James Morrison

This is a weird specific problem and I can't find any info anywhere.

I have a main React app that dynamically loads other React micro-apps. It does this through the componentDidMount()functionality of a component , using pure JavaScript to add a script tag to the end of the document body whose src address proxies the request to the web server to return a single bundled file that is its sub-application. load. When the component is unmounted, the script tag is removed from the document body and the div root is emptied.

The mini-app needs to be loadable into a bundle (no need to go into it here), I can do this by npm run buildrunning a modified function like the following to build the react app ;

"scripts": {
    "build": "npm run build:sass && npm run build:react && npm run build:bundle",
    "build:react": "react-scripts build",
    "build:bundle": "webpack --config webpack.config.js",
    "build:sass": "node-sass --precision=5 --indent-type=space --output-style=nested --indent-width=2 src/styles/Site.scss src/styles/Site.css"
  }

The bundle portion of the command uses the webpack.config.js file below;

const path = require("path")
const UglifyJsPlugin = require("uglifyjs-webpack-plugin")
const glob = require("glob")

module.exports = {
  entry: {
    "bundle.js": glob.sync("build/static/?(js|css)/*.?(js|css)").map(f => path.resolve(__dirname, f)),
  },
  output: {
    filename: "build/bundle.min.js"
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"],
      },
    ],
  },
  plugins: [new UglifyJsPlugin()],
}

This in turn produces a bundle.min.js file containing all the scripts, node_modules and css in the mini-app, which is loaded by the main app via the following components.

export class MyComponent extends React.PureComponent<Props, object> {

    componentDidMount() {
        const script = document.createElement("script");
        script.src = "myScriptSrc";
        script.async = true;
        script.id = this.AppIdToScriptId();
        document.body.appendChild(script);
    }

    componentWillUnmount() {
        document.getElementById("miniAppRoot").innerHTML = "";
        if (this.props.app) {
            document.getElementById(this.AppIdToScriptId()).remove();
        }
    }

    AppIdToScriptId = () => "d" + this.props.App.Id.replace("-", "");

    public render() {
        return (<div id="miniAppRoot" ></div>);
    }
}

export default MyComponent;

This all works fine, the only issue I'm having is that the css loaded via the react micro-app bundle script persists even after I remove the script tag and empty the html root. Does anyone know why this is happening? I'm not sure if the webpack config is to blame for client side caching or some other possibility, any ideas how to fix this?

smear 202

In your webpack config you are using style loader . By default this is adding styles as tags in the dom.

If your main app loads CSS differently than sub-apps load CSS, you should be able to remove anonymous style tags after unloading script tags.

Otherwise, style loaders do allow you to inject properties into the generated style tags (or actually load styles differently). That should allow you to find the sub-app style and remove it from the DOM.

Related


How do I remove the QuickBlox user tag from the application?

Billy Chrisnawan Adhyaksa I've searched all the QBUser tutorials, but I can't find a way to remove the user tag. Anyone know how to do this from the app without deleting the user and recreating it? Rubycon Using the Edit User API http://quickblox.com/developer

How do I remove the QuickBlox user tag from the application?

Billy Chrisnawan Adhyaksa I've searched all the QBUser tutorials, but I can't find a way to remove the user tag. Anyone know how to do this from the app without deleting the user and recreating it? Rubycon Using the Edit User API http://quickblox.com/developer

How do I remove the QuickBlox user tag from the application?

Billy Chrisnawan Adhyaksa I've searched all the QBUser tutorials, but I can't find a way to remove the user tag. Anyone know how to do this from the app without deleting the user and recreating it? Rubycon Using the Edit User API http://quickblox.com/developer

How to remove a line from a tag in a shell script?

username We have an xml file (abc_lop.xml)in which I need to remove a line present in the tag: Below is an xml file, I shortened it as it is huge. <HELLO version="4.2" xmlns="http://www.bacd.org/HELLO-4_2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

How to remove a line from a tag in a shell script?

username We have an xml file (abc_lop.xml)in which I need to remove a line present in the tag: Below is an xml file, I shortened it as it is huge. <HELLO version="4.2" xmlns="http://www.bacd.org/HELLO-4_2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

git shallow clone - how do I remove the "graft tag", what is it?

code_fodder So we created a template project "template_proj.git". The updated git version is: 2.14.1 on Windows 7 prof We have empty new projects except one of them commits with a .gitignore file. Lets say one of these projects is called "projectA.git". So my

git shallow clone - how do I remove the "graft tag", what is it?

code_fodder So we created a template project "template_proj.git". The updated git version is: 2.14.1 on Windows 7 prof We have empty new projects except one of them commits with a .gitignore file. Lets say one of these projects is called "projectA.git". So my

git shallow clone - how do I remove the "graft tag", what is it?

code_fodder So we created a template project "template_proj.git". The updated git version is: 2.14.1 on Windows 7 prof We have empty new projects except one of them commits with a .gitignore file. Lets say one of these projects is called "projectA.git". So my

How do I define where the script loads files from?

Mecca I use part of a script to load data from a registration file .npz. Here is the code: Tk().withdraw() # Here starts the first loading phase, where I pick the file I want from a window filename = askopenfilename() with load(filename) as data: # file lo

How do I define where the script loads files from?

Mecca I use part of a script to load data from a registration file .npz. Here is the code: Tk().withdraw() # Here starts the first loading phase, where I pick the file I want from a window filename = askopenfilename() with load(filename) as data: # file lo

Using Meteor + React, how do I add a script to the head tag?

near-point I need to add script tags to my website but can't get it to work. The instructions say to add to the head tag of the site, but I'm using Meteor + React and don't have the html page to add the head tag too. said in meteor tutorial Meteor parses all o

Using Meteor + React, how do I add a script to the head tag?

near-point I need to add script tags to my website but can't get it to work. The instructions say to add to the head tag of the site, but I'm using Meteor + React and don't have the html page to add the head tag too. said in meteor tutorial Meteor parses all o

Using Meteor + React, how do I add a script to the head tag?

near-point I need to add script tags to my website but can't get it to work. The instructions say to add to the head tag of the site, but I'm using Meteor + React and don't have the html page to add the head tag too. said in meteor tutorial Meteor parses all o

Using Meteor + React, how do I add a script to the head tag?

near-point I need to add script tags to my website but can't get it to work. The instructions say to add to the head tag of the site, but I'm using Meteor + React and don't have the html page to add the head tag too. said in meteor tutorial Meteor parses all o

Using Meteor + React, how do I add a script to the head tag?

near-point I need to add script tags to my website but can't get it to work. The instructions say to add to the head tag of the site, but I'm using Meteor + React and don't have the html page to add the head tag too. said in meteor tutorial Meteor parses all o

Using Meteor + React, how do I add a script to the head tag?

near-point I need to add script tags to my website but can't get it to work. The instructions say to add to the head tag of the site, but I'm using Meteor + React and don't have the html page to add the head tag too. said in meteor tutorial Meteor parses all o

Using Meteor + React, how do I add a script to the head tag?

near-point I need to add script tags to my website but can't get it to work. The instructions say to add to the head tag of the site, but I'm using Meteor + React and don't have the html page to add the head tag too. said in meteor tutorial Meteor parses all o

I want to remove a script tag from a page in vuejs and laravel

Kishan I'm adding a special script file for more functionality in this component, but when I change the component I want to remove the script tag. The following method is used to add script tags in vue components. created() { this.dynamicallyLoadScript("/a

How do I remove the side blue line from my website

sissy How to remove blue line from website already created on WordPress after using smart slider plugin please help Akshaya Kumar Satapasi The only way to provide an appropriate solution to your query is for us to obtain your website URL. The blue line may be

How do I remove the side blue line from my website

sissy How to remove blue line from website already created on WordPress after using smart slider plugin please help Akshaya Kumar Satapasi The only way to provide an appropriate solution to your query is for us to obtain your website URL. The blue line may be