preactjs / preact-cli

😺 Your next Preact PWA starts in 30 seconds.
MIT License
4.69k stars 375 forks source link

How to deploy a production server to now.sh? #501

Closed johhansantana closed 6 years ago

johhansantana commented 6 years ago

When I try to deploy to zeit.co now it builds and runs but only the development server.

"scripts": {
    "start": "if-env NODE_ENV=production && npm run -s serve || npm run -s dev",
    "build": "preact build",
    "serve": "preact build && preact serve",
    "dev": "preact watch",
    "lint": "eslint src"
  }

Do I need to setup the NODE_ENV to production? If so, where do I do that?

side-note: This library is awesome!

reznord commented 6 years ago

You should be good just by running npn run build and then just point out the build folder to zeit-cli while hosting it.

lukeed commented 6 years ago

Personally, I would just use this:

  "scripts": {
    "start": "preact serve",
    "build": "preact build",
    "watch": "preact watch"
  }

ZEIT will automatically run a build command if it's detected. Also, this way, you don't have to worry about env-vars, which are specific to now.sh only.

johhansantana commented 6 years ago

I changed it to @lukeed example but it fails when creating the SSL on now :(

> > preact serve
> Setting up SSL certificate (may require sudo)...
> Attempting to spawn simplehttp2server to generate cert.
> Failed to generate dev SSL certificate: Error: spawn /home/nowuser/src/node_modules/simplehttp2server/vendor/simplehttp2server_linux_amd64 ENOENT
lukeed commented 6 years ago

Oh right, they provide ssl for you on their end.

Install serve and then use it for your start command. I'm not at my desk but it's something like serve build -s where the s flag signals SPA mode.

johhansantana commented 6 years ago

It worked with serve build -s thank you! Man this is so mind-blowing how performant this preact-cli library is.

Thank you.

fardarter commented 6 years ago

Hey, @johhansantana, would you mind posting your package.json config that got it working? I'm struggling with the same error.

What was the fundamental issue?

hellsan631 commented 6 years ago

@fardarter and anyone else whom happens across this, some vendors on deploying do SSL automatically (like now). Instead of using preact's build in web server, like @lukeed lukeed suggested, you can use serve

  1. Add serve as a dependency: npm install serve --save or yarn add serve

  2. Edit your package.json to use serve instead of preact serve. It should look something like this:

    {
    "scripts": {
    "start": "if-env NODE_ENV=production && yarn serve || yarn dev",
    "build": "preact build",
    "serve": "serve build -s",
    }
    }

Or, for those pesky CI's that don't run the build command:

{
  "scripts": {
    "start": "if-env NODE_ENV=production && yarn serve || yarn dev",
    "build": "preact build",
    "serve": "npm run build && serve build -s",
  }
}

If your deploying using now, just run: now -e NODE_ENV=production And it should build correctly.