GithubHelp home page GithubHelp logo

isabella232 / eslint-plugin-ssr-friendly Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cultureamp/eslint-plugin-ssr-friendly

0.0 0.0 0.0 260 KB

ESLint plugin to detect inappropriate use of DOM globals properties

License: MIT License

JavaScript 100.00%

eslint-plugin-ssr-friendly's Introduction

eslint-plugin-ssr-friendly

ESLint plugin that detects incorrect use of DOM globals in order to properly do SSR and in general share code between client-side JS and Node.js modules.

npm version

Installation

npm i --dev eslint-plugin-ssr-friendly

Then add these to your eslintrc configuration:

{
  "plugins": ["ssr-friendly"],
  "extends": ["plugin:ssr-friendly/recommended"]
}

Rules

no-dom-globals-in-module-scope

Disallow use of DOM globals in module and global scope, as this will break any import/require in a NodeJS environment.

To fix it, wrap it in a function that will call when on client-side.

Please note that we can't detect if you're still calling this function without properly checking upfront if typeof window !== "undefined".

Not allowed

const retina = devicePixelRatio > 2;

Allowed

const isRetina = () => devicePixelRatio >= 2;

no-dom-globals-in-constructor

Disallow use of DOM globals in class constructors, as this will break SSR if you're instantiating this class as singleton or you're rendering this component.

To fix it, move this statement in a initOnBrowser() like-method or componentDidMount() if you're using React.

Please note that we can't detect if you're still calling this function in your constructor without properly checking upfront if typeof window !== "undefined".

Not allowed

class myClass {
  constructor() {
    document.title = "Otto";
  }
}

Allowed

class myClass {
  componentDidMount() {
    document.title = "Otto";
  }
}

no-dom-globals-in-react-cc-render

Disallow use of DOM globals in render() method of a React class-component, as this will break SSR if you're rendering this component.

To fix it, move this statement to componentDidMount()

Please note that we can't detect if you're still calling this function in your constructor without properly checking upfront if typeof window !== "undefined".

Not allowed

class Header extends React.Component {
  render() {
    const width = window.innerWidth;
    return <div style={{ width }} />;
  }
}

Allowed

class Header extends React.Component {
  componentDidMount() {
    this.setState({ width: window.innerWidth });
  }
  render() {
    return <div style={{ width }} />;
  }
}

no-dom-globals-in-react-fc

Disallow use of DOM globals in the render-cycle of a React FC, as this will break SSR if you're rendering this component.

To fix it, move this statement into a useEffect())

Not allowed

const Header = () => {
  window.addEventListener("resize", () => {});
  return <div />;
};

Allowed

const Header = () => {
  useEffect(() => {
    window.addEventListener("resize", () => {});
  }, []);
  return <div />;
};

eslint-plugin-ssr-friendly's People

Contributors

dependabot[bot] avatar kopiro avatar vietanhtran16 avatar

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.