Skip to content

Add Electron example #1430

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

Merged
merged 24 commits into from
Aug 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e7f492e
Add Electron usage example
sergiodxa Mar 15, 2017
eb3afc1
Remove the deploy part
sergiodxa Mar 16, 2017
3623bef
Merge remote-tracking branch 'zeit/master' into with-electron-example
sergiodxa Mar 19, 2017
e6e1948
Only allow GET request to our HTTP server
sergiodxa Mar 19, 2017
687707a
Only allow request from an electron app (checking the user agent)
sergiodxa Mar 19, 2017
d18e2eb
Add warning about the local HTTP server
sergiodxa Mar 19, 2017
fe330a5
Merge remote-tracking branch 'origin/with-electron-example' into with…
sergiodxa Mar 19, 2017
73bb6c8
Update package.json
sergiodxa Mar 19, 2017
280878c
Merge remote-tracking branch 'zeit/master' into with-electron-example
sergiodxa Mar 25, 2017
308c0ad
Merge remote-tracking branch 'origin/with-electron-example' into with…
sergiodxa Mar 25, 2017
5631897
Merge remote-tracking branch 'zeit/master'
sergiodxa Mar 25, 2017
2d7ec1c
Merge remote-tracking branch 'zeit/master' into with-electron-example
sergiodxa Apr 18, 2017
76b19bf
Merge remote-tracking branch 'zeit/master'
sergiodxa Apr 20, 2017
f4e1f4a
Merge remote-tracking branch 'zeit/master'
sergiodxa Apr 26, 2017
49e8b28
Update example to use Next.js v3
sergiodxa May 20, 2017
695f893
Merge branch 'master' into with-electron-example
sergiodxa May 20, 2017
8c598a6
Added required package.json fields with placeholders
sergiodxa May 22, 2017
4a049f8
Use next:// file protocol to open internal built files
sergiodxa Jun 12, 2017
9024d67
Create next.config.js
sergiodxa Jun 15, 2017
027bf7d
Merge branch 'master' into with-electron-example
sergiodxa Jun 15, 2017
ec39721
Update set-menu.js
sergiodxa Jun 16, 2017
411b481
Merge branch 'master' into with-electron-example
sergiodxa Jun 16, 2017
ba4b38c
Merge branch 'master' into with-electron-example
sergiodxa Jun 16, 2017
54eaa02
Update example to merge it with electron-next-skeleton ideas
sergiodxa Aug 5, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions examples/with-electron/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Electron application example

## How to use

Download the example [or clone the repo](https://github.com/zeit/next.js):

```bash
curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/with-electron
cd with-electron
```

Install it and run:

```bash
npm install
npm start
```

## The idea behind the example

This example show how you can use Next.js inside an Electron application to avoid a lot of configuration, use Next.js router as view and use server-render to speed up the initial render of the application.

For development it's going to run a HTTP server and let Next.js handle routing. In production it use `next export` to pre-generate HTML static files and use them in your app instead of running an HTTP server.

You can create the production app using `npm run dist`.
36 changes: 36 additions & 0 deletions examples/with-electron/main/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Native
const { join } = require('path')
const { format } = require('url')

// Packages
const { BrowserWindow, app, ipcMain } = require('electron')
const isDev = require('electron-is-dev')
const prepareNext = require('electron-next')

// Prepare the renderer once the app is ready
app.on('ready', async () => {
await prepareNext('./renderer')

const mainWindow = new BrowserWindow({
width: 800,
height: 600
})

const url = isDev
? 'http://localhost:8000/start'
: format({
pathname: join(__dirname, '../renderer/start/index.html'),
protocol: 'file:',
slashes: true
})

mainWindow.loadURL(url)
})

// Quit the app once all windows are closed
app.on('window-all-closed', app.quit)

// listen the channel `message` and resend the received message to the renderer process
ipcMain.on('message', (event, message) => {
event.sender.send('message', message)
})
31 changes: 31 additions & 0 deletions examples/with-electron/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "electron-next-skeleton",
"productName": "ElectronNext",
"version": "1.0.0",
"main": "main/index.js",
"scripts": {
"start": "electron .",
"build": "next build renderer && next export renderer",
"dist": "npm run build && build --dir"
},
"build": {
"asar": false,
"extraResources": [
{
"from": "renderer/out",
"to": "app/renderer"
}
]
},
"devDependencies": {
"electron": "^1.6.11",
"electron-builder": "^19.19.1",
"next": "beta",
"react": "15.6.1",
"react-dom": "15.6.1"
},
"dependencies": {
"electron-is-dev": "0.3.0",
"electron-next": "3.0.8"
}
}
14 changes: 14 additions & 0 deletions examples/with-electron/renderer/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
webpack (config, { dev }) {
config.target = 'electron-renderer'
return config
},
exportPathMap () {
// Let Next.js know where to find the entry page
// when it's exporting the static bundle for the use
// in the production version of your app
return {
'/start': { page: '/start' }
}
}
}
57 changes: 57 additions & 0 deletions examples/with-electron/renderer/pages/start.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Component } from 'react'
import { ipcRenderer } from 'electron'

export default class extends Component {
state = {
input: '',
message: null
}

componentDidMount () {
// start listening the channel message
ipcRenderer.on('message', this.handleMessage)
}

componentWillUnmount () {
// stop listening the channel message
ipcRenderer.removeListener('message', this.handleMessage)
}

handleMessage = (event, message) => {
// receive a message from the main process and save it in the local state
this.setState({ message })
}

handleChange = event => {
this.setState({ input: event.target.value })
}

handleSubmit = event => {
event.preventDefault()
ipcRenderer.send('message', this.state.input)
this.setState({ message: null })
}

render () {
return (
<div>
<h1>Hello Electron!</h1>

{this.state.message &&
<p>{this.state.message}</p>
}

<form onSubmit={this.handleSubmit}>
<input type='text' onChange={this.handleChange} />
</form>

<style jsx>{`
h1 {
color: red;
font-size: 50px;
}
`}</style>
</div>
)
}
}