Skip to content
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

Add Electron example #1430

Merged
merged 24 commits into from
Aug 9, 2017
Merged

Add Electron example #1430

merged 24 commits into from
Aug 9, 2017

Conversation

sergiodxa
Copy link
Contributor

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

Copy link
Member

@matheuss matheuss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if it's a good idea to fire up a HTTP server inside an app like that – it can introduce huge security vulnerabilities.

I'd wait for next export 🤔

@sergiodxa
Copy link
Contributor Author

@matheuss I think it depends on how next export it's going to work, if it's going to only generate static html files without client-side getInitialProps then it's not going to be useful for this use case 🤔 also you will need to re-export your app after every change

BTW, what kind of security vulnerabilities do you thinks can this introduce?

npm run dev
```

Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these instructions relevant to this example?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point 😅

@1msoft
Copy link

1msoft commented Mar 16, 2017

good idea, share some pages with some friends.

@rauchg
Copy link
Member

rauchg commented Mar 18, 2017

@sergiodxa I think @matheuss is concerned potentially about browsers probing localhost, finding the port, then finding open routes? Maybe an advisory in the README about that vector would be good

@rauchg
Copy link
Member

rauchg commented Mar 18, 2017

next export will certainly output 100% static stuff. So, if you only limit yourself to serving static stuff via Next, there shouldn't be any security issues. Protecting POST stuff is the key, and not performing side effects upon GET

@sergiodxa
Copy link
Contributor Author

In browsers the client code breaks (because Webpack doesn't do anything to the require function), you only get the server rendered page. Anyway I added an warning in the README about the possible access from a browser.

I also added a little validation code to respond with 404 if the request does not come from an Electron app. It's not fully secure but it should hide, for unintentional users, the access to the app from a browser.

},
"dependencies": {
"electron": "1.6.2",
"next": "beta",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it is worth pinning Next.js version as it is done in other examples:

"next": "^2.0.0-beta",

"electron": "1.6.2",
"next": "beta",
"react": "15.4.2",
"react-dom": "15.4.2"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other examples pin React version like this:

"react": "^15.4.2",
"react-dom": "^15.4.2"

@rauchg
Copy link
Member

rauchg commented Mar 20, 2017

@sergiodxa can you look into whether a unix socket file would be a viable option?

https://nodejs.org/api/net.html#net_server_listen_path_backlog_callback

@leo
Copy link
Contributor

leo commented Apr 6, 2017

Now Desktop is another example now.

@sergiodxa
Copy link
Contributor Author

sergiodxa commented Apr 6, 2017

@rauchg I have tried to use a Unix Socket with the net module but Next require an request and response object, I'm not use if it's possible : thinking:

@leo yeah! I just checked the code and the server it's basically the same, awesome, I also published yesterday a Spanish tutorial about this example. Using Next inside Electron to avoid configuration it's pure magic.

@leo
Copy link
Contributor

leo commented Apr 10, 2017

Please wait with this. I'll release a blog post about this very soon (describes how to use Next.js with Electron in detail)... 😊

@ritz078
Copy link

ritz078 commented Apr 18, 2017

@sergiodxa Have you tested by creating a package ? I created a darwin build and it didn't launch.

@leo
Copy link
Contributor

leo commented Apr 18, 2017

We're using it here. Feel free to test!

@sergiodxa
Copy link
Contributor Author

@ritz078 Yes, I tested it using this example, are you having any error in console?

@ritz078
Copy link

ritz078 commented Apr 18, 2017

No there isn't any error in the console.

I cloned the same and added electron-packager

{
  "scripts": {
    "dev": "electron index.js",
    "build": "next build",
    "start": "NODE_ENV=production electron index.js",
    "pack": "electron-packager . --out=package --darwin --overwrite"
  },
  "dependencies": {
    "electron": "1.6.2",
    "next": "^2.0.0-beta",
    "react": "^15.4.2",
    "react-dom": "^15.4.2"
  },
  "devDependencies": {
    "electron-packager": "^8.6.0"
  }
}

The app is being created fine npm run pack but its not launching the window.

@ritz078
Copy link

ritz078 commented Apr 18, 2017

It seems this is the error.

// written here executes
nextApp
  .prepare()
  .then(() => {
// anything written here doesn't execute after packaging
})

@frol
Copy link
Contributor

frol commented May 25, 2017

@sergiodxa I have figured it out with a help of a custom Electron protocol: https://electron.atom.io/docs/api/protocol/#protocol

I am testing it right now and will create a PR.

@frol
Copy link
Contributor

frol commented May 25, 2017

@sergiodxa For some really weird reason, I cannot create a PR against your fork. Please, pull my commit: frol@e44c97c

@frol
Copy link
Contributor

frol commented May 25, 2017

By the way, you cannot use cookies for "current domain" when your page is open as file://. I have experimented with using the custom next:// protocol, but it doesn't play nicely as well. I may simply use other means for the storage as cookies don't make a lot of sense in a standalone app, but it just has different behavior from the dev mode, so it is better to be prepared.

P.S. I have a biggish app in Next.js, and I am just stressing this Electron example with it.

@frol
Copy link
Contributor

frol commented May 26, 2017

I have found a workaround for the cookies in Electron, just use @expo/electron-cookies. Here is a snippet which you need to put somewhere in the top-component (_document.js or common high-level component):

if (typeof document !== 'undefined') {
  const ElectronCookies = require('@exponent/electron-cookies')
  ElectronCookies.enable({
    origin: 'https://example.com',
  });
}

So far, I have managed to package my whole Next.js application into Electron and it is fully functioning! I had to only workaround the superagent/node-formidable issue, the missing cookies support and files upload implementation (I had to use fs.createReadStream() instead of plain File objects when POSTing them).

@sergiodxa Thank you for the example!

@trevordmiller
Copy link
Contributor

@leo Any updates on the blog post? I'm looking forward to it :) Thank you for the examples so far.

@leo
Copy link
Contributor

leo commented Jun 9, 2017

@trevordmiller Coming very soon - sadly can't tell anything else publicly. But it's VERY close!

'/': { page: '/' }
}
},
// set the prefix as `./` instead of `/`, this is because when you export your pages
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment does not reflect the actual setting. There is no ./ anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks I fixed it :)

{
label: 'Speech',
submenu: [{ role: 'startspeaking' }, { role: 'stopspeaking' }]
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to remove the comma since otherwise it fails with "SyntaxError: Unexpected token )".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed it, thanks a lot for your feedback :)

@frol
Copy link
Contributor

frol commented Jun 16, 2017

@sergiodxa Thank you for the updated example! It is really easy to get any Next.js project packaged into Electron now!

@timneutkens
Copy link
Member

timneutkens commented Jul 1, 2017

@leo what's the status of this one? 😄 Wearing my project manager 🎩... 😂

@ragingwind
Copy link
Contributor

I'm also waiting for this to introduce to our devs as an organizer of Electron meetup in Korea and a big fan of next.js

@timneutkens timneutkens added Status: Awaiting Response examples Issue was opened via the examples template. labels Jul 1, 2017
@leo
Copy link
Contributor

leo commented Jul 2, 2017

My helper package is already out! Blog post very close... 🚀

@timneutkens
Copy link
Member

@leo cool 👍 🤗 🚀 :shipit:

@leo
Copy link
Contributor

leo commented Jul 16, 2017

Just published my blog post about building Electron apps using Next.js and created a boilerplate. If it looks good to you, I suggest basing this PR on it! 🚀 ❤️

@trevordmiller
Copy link
Contributor

Thanks @leo 🎉

@sergiodxa
Copy link
Contributor Author

sergiodxa commented Aug 5, 2017

I updated this example to bring some ideas of electron-next-skeleton, now it's simpler, but I keep the UI so the example show how you can import Electron modules in the renderer process and use IPC between the Next.js (renderer) app and the main process.

cc @leo @timneutkens

Copy link
Member

@timneutkens timneutkens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good ❤️

@timneutkens timneutkens merged commit 4b0467e into vercel:master Aug 9, 2017
@sergiodxa sergiodxa deleted the with-electron-example branch August 9, 2017 05:57
@lock lock bot locked as resolved and limited conversation to collaborators May 12, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
examples Issue was opened via the examples template.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants