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

Unable to use styled-components #7

Closed
linonetwo opened this issue Jan 27, 2020 · 7 comments
Closed

Unable to use styled-components #7

linonetwo opened this issue Jan 27, 2020 · 7 comments

Comments

@linonetwo
Copy link

styled-components.browser.esm.js:133 Refused to apply inline style because it violates the following Content Security Policy directive: "style-src 'self'". Either the 'unsafe-inline' keyword, a hash ('sha256-47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU='), or a nonce ('nonce-...') is required to enable inline execution.

It is unable to add style now.

I tried to add

  session.defaultSession.webRequest.onHeadersReceived((details, callback) => {
    callback({
      responseHeaders: {
        'Content-Security-Policy': ["default-src 'self' 'unsafe-inline' 'unsafe-eval'"],
        ...details.responseHeaders,
      },
    });
  });

But still can't get around this.

@linonetwo
Copy link
Author

Use

new CspHtmlWebpackPlugin({
      'base-uri': ["'self'"],
      'object-src': ["'none'"],
      'script-src': ["'self'"],
      'style-src': ["'self' 'unsafe-inline'"],
      'frame-src': ["'none'"],
      'worker-src': ["'none'"],
    }),

linonetwo added a commit to linonetwo/solid-box that referenced this issue Jan 27, 2020
@reZach
Copy link
Owner

reZach commented Jan 28, 2020

@linonetwo You can also pull the latest and use the mini-css-extract-plugin to load css files if you are loading css as described on this page. You were just a few hours behind me beginning to work on this! The plus side about this approach is webpack already configures your css in it's own bundle (you can configure with mini css extract plugin options if you'd like) and it has it's own nonce which matches the CSP.

image

This commit added this into the template. Essentially:

package.json

"devDependencies": {
    "mini-css-extract-plugin": "^0.9.0"
}

webpack.[development|production].js

const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = merge(base, {
  // existing
  plugins: [
    new MiniCssExtractPlugin(), // Add this!
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, "app/src/index.html"),
      filename: "index.html"
    }),
    new CspHtmlWebpackPlugin({
      "base-uri": ["'self'"],
      "object-src": ["'none'"],
      "script-src": ["'self'"],
      "style-src": ["'self'"], // don't need to add 'unsafe-inline'!
      "frame-src": ["'none'"],
      "worker-src": ["'none'"]
    })
  ]
})

@linonetwo
Copy link
Author

linonetwo commented Jan 28, 2020

Thanks for your solution, that really works for .css file.

But styled-components won't generate .css file, so it won't be loaded by the css loaders. It injects <style> tag to the header:

截屏2020-01-28上午11 14 24

截屏2020-01-28上午11 14 29

Do you know the possible solution?

@reZach
Copy link
Owner

reZach commented Jan 28, 2020

Can you explain what styled-components you are speaking of?

I also omitted (by accident!) another file you must change. In order to use mini-css-extract-plugin, you have to take out style-loader (as it is the package inserting the style tags in your HTML). Here is what you need to modify in webpack.config.js

const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = {
  // other options
  module: {
    rules: [
      // other loaders
      // loads .css files
      {
        test: /\.css$/,
        include: [path.resolve(__dirname, "app/src")],
        use: [
          MiniCssExtractPlugin.loader, // Changed!
          {
            loader: "css-loader"
          }
        ],
        resolve: {
          extensions: [".css"]
        }
      }
    ]
  }
};

@linonetwo
Copy link
Author

linonetwo commented Jan 28, 2020

I followed your commit and changed that file also.

Please try this lib https://www.npmjs.com/package/styled-components

And simply do so to add style:

import styled from 'styled-components`

const Button = styled.button`
  color: grey;
`;

export default function App() {
  return <Button>aaa</Button>
}

It is a widely used way to add style to react components.

@reZach
Copy link
Owner

reZach commented Jan 28, 2020

This is a new library for me, thank you for sharing. I take it with your earlier modification adding 'unsafe-inline' is the way to go to use styled-components. Thank you for sharing your solution with us here.

@Nantris
Copy link
Contributor

Nantris commented Feb 5, 2021

Relevant to #53

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants