Skip to content

Instantly share code, notes, and snippets.

@joehand
Created March 15, 2018 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joehand/536f34ac91e07b014056d0e4cc9bd92a to your computer and use it in GitHub Desktop.
Save joehand/536f34ac91e07b014056d0e4cc9bd92a to your computer and use it in GitHub Desktop.
live website deployment w/ now + dat

now – Realtime global deployments

Using Dat, we can turn now's realtime deployment into live deployment.

Tutorial

  1. Install dat command line or desktop
  2. Create a new dat with your website files
  3. Deploy via dat-now via now.sh
  4. Live sync your website

0. Install Dat

npm install -g dat

Want to skip installing dat for now and just see how it works? We can use a key for a dat is already being shared.

1. Create a dat

mkdir file-sharing
cd file-sharing
echo 'hello world' > hello.txt
dat share .

2. Deploy via dat-now via now.sh

We need to deploy a simple package to hook up our dat to now. We've packaged this up into a npm package called dat-now and made a demo app you can clone and deploy. dat-now acts as a bridge between the now http server and files on your computer.

Setup dat-now

Clone our demo app (this will be deployed to now):

git clone https://github.com/joehand/dat-now-demo.git
cd dat-now-demo

dat-now reads process.env.KEY to figure out what files to download & serve. To set this, change the key to your dat key in the now.json file:

{
  "env" : {
    "KEY" : "dat://28cddf6d5d21bbd9492dcdba14a68b9a35393b769b15f61e2a5258567d8bca8f"
  }
}

(If you want to skip the Dat creation for now, you can also deploy with this key. It'll re-share a dat that is already being shared elsewhere.)

Deploy via now

Once we set the key, we can deploy! Use now to deploy the site viua now's realtime global deployment:

cd dat-now-demo
now --public

Visit the site!

4. Live Sync

Bonus: Using Dat JS API directly

The dat-now package is a really simple wrapper around the dat javascript API. You can use the Dat API directly and build more on top with the same idea of live, hosted content. The dat-now library sets a few tricky options we need for Dat to work on now.sh (bugs we are working out).

This is dat-now in its entirety.

var Dat = require('dat')

var opts = {
  key: process.env.KEY
}

// dat will save files to /tmp, the only location now.sh supports
Dat('/tmp', opts, function (err, dat) {
  if (err) throw err

  dat.joinNetwork({
    utp: false, // utp doesn't work on now.sh right now
    upload: false // upload needs another port, now.sh can only expose one port
  })

  dat.serveHttp({port: 8080, footer: `Sharing via dat on now.sh.`})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment