inlineAssets function

RequestHandler inlineAssets(
  1. Directory assetDirectory
)

Inlines assets into buffered responses, resolving paths from an assetDirectory.

In any .html file sent down, link and script elements that point to internal resources will have the contents of said file read, and inlined into the HTML page itself.

In this case, "internal resources" refers to a URI without a scheme, i.e. /site.css or foo/bar/baz.js.

Implementation

RequestHandler inlineAssets(Directory assetDirectory) {
  return (req, res) {
    if (!res.isOpen ||
        !res.isBuffered ||
        res.contentType.mimeType != 'text/html') {
      return Future<bool>.value(true);
    } else {
      var doc = html.parse(utf8.decode(res.buffer!.takeBytes()));
      return inlineAssetsIntoDocument(doc, assetDirectory).then((_) {
        res.buffer!.add(utf8.encode(doc.outerHtml));
        return false;
      });
    }
  };
}