https://github.com/mrsteele/next-isserver
A plugin for nextjs
https://github.com/mrsteele/next-isserver
Last synced: 3 months ago
JSON representation
A plugin for nextjs
- Host: GitHub
- URL: https://github.com/mrsteele/next-isserver
- Owner: mrsteele
- Created: 2017-11-13T20:25:02.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-11-16T19:53:57.000Z (over 7 years ago)
- Last Synced: 2025-02-21T13:40:26.191Z (3 months ago)
- Language: JavaScript
- Size: 5.86 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# next-isServer
A simple [nextjs](https://github.com/zeit/next.js/) plugin to check if `getInitialProps` was loaded on the server or the client.
### How to use
##### Install
First you need to install the package locally to your project:
```
npm i next-isserver --save
```Create your server file if you have not yet already:
```js
// server.js
const next = require('next')
const isServer = require('next-isserver')
const app = next({dev: process.env.NODE_ENV !== 'production'})
const handler = isServer(app)// With express
const express = require('express')
app.prepare().then(() => {
express().use(handler).listen(3000)
})// Without express
const {createServer} = require('http')
app.prepare().then(() => {
createServer(handler).listen(3000)
})```
Update your `package.json` file scripts if needed:
```json
"scripts": {
"dev": "node server.js",
"build": "next build",
"start": "NODE_ENV=production node server.js"
}
```##### Client
In any of your files that use the `getInitialProps` function, you can access `isServer` from the `query` property like so:
```js
import React from 'react'export default class extends React.Component {
static async getInitialProps({ query }) {
const { isServer } = query
return { isServer }
}render() {
return (
This page was {this.props.isServer ? '': 'NOT'} rendered on the server.
)
}
}
```### License
MIT