Coder Social home page Coder Social logo

modernizr-server's Introduction

modernizr-server

Express middleware that exposes client modernizr data to the server.

Install

npm install --save modernizr-server

Simple Example

// setup
npm install --save modernizr-server express cookie-praser

// server.js
const express = require('express')
const cookieParser = require('cookie-parser')
const modernizrServer = require('modernizr-server')

const app = express()

app.use(cookieParser())
app.use(modernizrServer())

app.get('/', function(req, res) {
  let Modernizr = req.cookies.modernizr
  res.send(Modernizr)
}
app.listen(8000, function(err) {
  console.log('listening at http://localhost:8000/')
})

Options

modernizr-server supports an options object as a parameter. (See an example below).

storageMethod: String

Default: 'cookie'

If you want to use a session, set this option as 'session'. See 'Example (session)' below. modernizr-server by default stores the modernizr information as a cookie. This is good if you want to access the same modernizr on the client; however, modernizr builds can be pretty big. If you want to use this method, we recommend that you use a smaller build. We also support 'session' as a storage method. This is the preferred method.

cdn: String

Default: 'https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.min.js'

If no build options are given ('config' or 'build'), modernizr-server will use a CDN to load modernizr. You can choose to use your own CDN as well.

cdn: 'https://YourCDN.com/modernizr/...'

config: Object

Set the config option to create custom builds per client. NOTE: this create the same build every time someone new makes a request. This can potentially add more dynamics. If you don't need this, Use the build option below.

config: {
  "minify": true,
  "options": [
    "hasEvent",
    "html5shiv"
  ],
  "feature-detects": [
    "canvas",
    "webanimations"
  ]
}

For a full list of feature-detects checkout config-all.json

build: String

If you already have a preset modernizr build, you can use it with the fs module like this:

build: fs.readFileSync('./modernizr-custom.js', 'UTF-8')

You can make a custom modernizr build on their website. This is the most recommended option because node won't have to create a custom build every time someone different connects to your webapp, and it keeps the modernizr build small.

storageName: String

Default: 'modernizr'

This will change the name of the cookie or session variable that modernizr-server sets.

expires: Number

Default: 0 (Session)

This will change the expiration time of the modernizr cookie in ms. You should probably set this something very high.

Example (cookie)

// server.js
const express = require('express')
const cookieParser = require('cookie-parser')
const modernizrServer = require('modernizr-server')

const app = express()

app.use(cookieParser())
app.use(modernizrServer({
  config: {
    "minify": true,
    "options": [
      "hasEvent",
      "html5shiv"
    ],
    "feature-detects": [
      "canvas",
      "webanimations"
    ]
  },
}))

app.get('/', function(req, res) {
  let Modernizr = req.cookies.modernizr
  if (!Modernizr.canvas) {
    console.log('The client does not support canvas!')
    res.send('You should get a better web browser. :(')
  } else {
    console.log('The client supports canvas!')
    res.send('Yay, you  support canvas! :)')
  }
})

app.listen(8000, function(err) {
  if (err) throw err
  console.log('listening at http://localhost:8000/')
})

Example (session)

// server.js
const express = require('express')
const session = require('express-session')
const modernizrServer = require('modernizr-server')

const app = express()

app.use(session({
  secret: 'MY_SECRET',
  resave: false,
  saveUninitialized: false
}))

app.use(modernizrServer({
  storageMethod: 'session',
  config: {
    "minify": true,
    "options": [
      "hasEvent",
      "html5shiv"
    ],
    "feature-detects": [
      "canvas",
      "webanimations"
    ]
  },
}))

app.get('/', function(req, res) {
  let Modernizr = req.session.modernizr // NOT 'req.cookies'
  if (!Modernizr.canvas) {
    console.log('The client does not support canvas!')
    res.send('You should get a better web browser. :(')
  } else {
    console.log('The client supports canvas!')
    res.send('Yay, you  support canvas! :)')
  }
})

app.listen(8000, function(err) {
  if (err) throw err
  console.log('listening at http://localhost:8000/')
})

Using the same modernizr on the client

This is not currently supported with sessions. Add this to the beginning of your javascript to gain access to modernizr.

  var Modernizr = document.cookies.modernizr

Why?

Sometimes, it is necessary to have client data on the server. If your website heavily relies on your client having a certain feature, it might be better to handle this on the server instead of loading the whole app to a client who can't run it.

How it works

modernizr-server will load a modernizr build onto the client. It will detect the functionality that you desire, then set a JSON cookie of that data. Once this is complete, the page will refresh and the server will then have access to this cookie (i.e. req.cookies.modernizr). As long as the client has the modernizr cookie, the server will never load modernizr to the client ever again.

Credit

This was inspired by rack-modernizr

modernizr-server's People

Contributors

rlindskog avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

modernizr-server's Issues

Add Storage Options

TODO:
Add a Storage option to support sessions. It is generally bad practice to store a lot of data in a cookie. A complete modernizr build can be over 1KB in the JSON cookie format. I will add a session storage option to persist the modernizr info on the server.

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.