write-html

Write HTML in parts, <head> first, <body> later

Usage no npm install needed!

<script type="module">
  import writeHtml from 'https://cdn.skypack.dev/write-html';
</script>

README

write-html

build status dependency status

Write HTML in parts, <head> first, <body> later

Example

WriteHtml encourages you to write the <head> of your html page immediately and write the body once you have done some async things. this allows script tags, style tags & assets like link prefetching to load in parallel with your database fetching code.

var http = require("http")
var WriteHtml = require("write-html")

var server = http.createServer(function (req, res) {
    if (req.url.match(/\/user\//)) {
        var userParts = req.url.split("/")
        var userId = userParts[userParts.length - 1]

        var writer = WriteHtml(req, res)
        writer.writeHead("<head><title>User page</title></head>")

        db.get("user:" + userId, function (err, user) {
            writer.writeBody("<body>" + render(user) + "</body>")
        })
    }
})

Installation

npm install write-html

Contributors

  • Raynos

MIT Licenced