Coder Social home page Coder Social logo

markogdev / isclientside Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 313 KB

Helps us know when Javascript/Typescript code is executing client side or server side.

Home Page: https://markogdev.github.io/IsClientSide/

License: GNU General Public License v3.0

TypeScript 50.27% JavaScript 49.73%
javascript typescript client-server

isclientside's Introduction

IsClientSide

Use to check if JavaScript Code is on the client or server.

Nice Git Page

https://markogdev.github.io/IsClientSide/

Install

https://www.npmjs.com/package/is-client-side

npm install is-client-side

We also have a Javascipt compiled version in the dist folder that we are using for our demo.

Demo

Demo Code

Live Demo

API

IsClientSide.true()
  • Return Type: Boolean
  • Action: Returns True if all our client side checks are true. Meaning code is executing on the client side (e.g. in the browser).
IsClientSide.false()
  • Return Type: Boolean
  • Action: Returns True if any client side checks are false. Meaning code is executing on the server side (e.g. in Node).
IsClientSide.windowAvailable()
  • Return Type: Boolean
  • Action: Returns True if Window Is available.
IsClientSide.documentAvailable()
  • Return Type: Boolean
  • Action: Returns True if Document is Available.

All functions return only True or False. No need to worry about 'undefined'.

You only really need the IsClientSide.false() function.

Example

Useful to stop client code executing on server side.

import { IsClientSide } from 'is-client-side';

if (IsClientSide.false()) { 
    return null;
}

You can just put this snippet of code before client side conditional imports OR at the top of client only functions etc.

Typescript Building

We are using Webpack to compile typescript files and convert to Javascript that can run standard web browsers.

We don't want Visual Studio to compile the Typescript files.

Therefore we have we have both set the 'build action' of the Typescript files to 'None' and we have modified the project file IsClientSide.csproj to include a TypeScriptCompileBlocked element as below.

<PropertyGroup>
  <TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
  ...
<PropertyGroup/>

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.