This commit is contained in:
Tobe O 2018-11-08 11:17:44 -05:00
parent 961fd5fc83
commit baad6bc628
6 changed files with 41 additions and 28 deletions

View file

@ -1,2 +1,5 @@
# 2.0.0
* Angel 2 updates.
# 1.0.0
* Initial release.

View file

@ -1,3 +1,3 @@
analyzer:
strong-mode:
implicit-cast: false
implicit-casts: false

View file

@ -1,11 +1,12 @@
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_framework/http.dart';
import 'package:angel_seo/angel_seo.dart';
import 'package:angel_static/angel_static.dart';
import 'package:dart2_constant/convert.dart';
import 'package:file/local.dart';
main() async {
var app = new Angel()..lazyParseBodies = true;
var app = new Angel();
var fs = const LocalFileSystem();
var http = new AngelHttp(app);
@ -18,7 +19,7 @@ main() async {
),
);
app.use(vDir.handleRequest);
app.fallback(vDir.handleRequest);
// OR, just add a finalizer. Note that [VirtualDirectory] *streams* its response,
// so a response finalizer does not touch its contents.
@ -26,7 +27,7 @@ main() async {
// You likely won't need to use both; it just depends on your use case.
app.responseFinalizers.add(inlineAssets(fs.directory('web')));
app.get('/using_response_buffer', (ResponseContext res) async {
app.get('/using_response_buffer', (req, res) async {
var indexHtml = fs.directory('web').childFile('index.html');
var contents = await indexHtml.readAsString();
res
@ -34,7 +35,7 @@ main() async {
..buffer.add(utf8.encode(contents));
});
app.use(() => throw new AngelHttpException.notFound());
app.fallback((req, res) => throw new AngelHttpException.notFound());
var server = await http.startServer('127.0.0.1', 3000);
print('Listening at http://${server.address.address}:${server.port}');

View file

@ -1,24 +1,23 @@
import 'dart:async';
import 'dart:convert';
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_static/angel_static.dart';
import 'package:dart2_constant/convert.dart';
import 'package:file/file.dart';
import 'package:html/dom.dart' as html;
import 'package:html/parser.dart' as html;
import 'package:path/path.dart' as p;
/// Wraps a `VirtualDirectory` to patch the way it sends
/// `.html` files.
/// 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`.
RequestMiddleware inlineAssets(Directory assetDirectory) {
RequestHandler inlineAssets(Directory assetDirectory) {
return (req, res) {
if (res.willCloseItself ||
res.streaming ||
if (!res.isOpen ||
!res.isBuffered ||
res.contentType.mimeType != 'text/html') {
return new Future<bool>.value(true);
} else {
@ -112,7 +111,7 @@ class _InlineAssets extends VirtualDirectory {
res
..headers['content-type'] = 'text/html; charset=utf8'
..buffer.add(utf8.encode(doc.outerHtml));
..add(utf8.encode(doc.outerHtml));
return false;
} else {
return await inner.serveFile(file, stat, req, res);

View file

@ -1,17 +1,17 @@
name: angel_seo
description: Helpers for building SEO-friendly Web pages in Angel.
description: Helper infrastructure for building SEO-friendly Web backends in Angel.
author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/seo
version: 1.0.0
version: 2.0.0
environment:
sdk: ">=1.8.0 <3.0.0"
sdk: ">=2.0.0-dev <3.0.0"
dependencies:
angel_framework: ^1.0.0-dev
angel_static: ^1.3.0
dart2_constant: ^1.0.0
file: ^2.0.0
angel_framework: ^2.0.0-alpha
angel_static: ^2.0.0-alpha
file: ^5.0.0
html: ^0.13.0
path: ^1.0.0
dev_dependencies:
angel_test: ^1.0.0
test: ^0.12.0
angel_test: ^2.0.0-alpha
logging:
test: ^1.0.0

View file

@ -2,22 +2,24 @@ import 'package:angel_framework/angel_framework.dart';
import 'package:angel_seo/angel_seo.dart';
import 'package:angel_static/angel_static.dart';
import 'package:angel_test/angel_test.dart';
import 'package:dart2_constant/convert.dart';
import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:html/dom.dart' as html;
import 'package:html/parser.dart' as html;
import 'package:http_parser/http_parser.dart';
import 'package:logging/logging.dart';
import 'package:test/test.dart';
void main() {
group('inlineAssets', () {
group('buffer', inlineAssetsTests((app, dir) {
app.get('/', (ResponseContext res) async {
app.get('/', (req, res) async {
var indexHtml = dir.childFile('index.html');
var contents = await indexHtml.readAsString();
var contents = await indexHtml.readAsBytes();
res
..headers['content-type'] = 'text/html; charset=utf-8'
..buffer.add(utf8.encode(contents));
..useBuffer()
..contentType = new MediaType.parse('text/html; charset=utf-8')
..buffer.add(contents);
});
app.responseFinalizers.add(inlineAssets(dir));
@ -26,7 +28,7 @@ void main() {
group('virtual_directory', inlineAssetsTests((app, dir) {
var vDir = inlineAssetsFromVirtualDirectory(
new VirtualDirectory(app, dir.fileSystem, source: dir));
app.use(vDir.handleRequest);
app.fallback(vDir.handleRequest);
}));
});
}
@ -49,6 +51,13 @@ void Function() inlineAssetsTests(InlineAssetTest f) {
var file = fs.file(path);
await file.writeAsString(contents[path].trim());
}
app.logger = new Logger('angel_seo')
..onRecord.listen((rec) {
print(rec);
if (rec.error != null) print(rec.error);
if (rec.stackTrace != null) print(rec.stackTrace);
});
});
tearDown(() => client.close());
@ -58,6 +67,7 @@ void Function() inlineAssetsTests(InlineAssetTest f) {
setUp(() async {
var res = await client.get('/', headers: {'accept': 'text/html'});
print(res.body);
doc = html.parse(res.body);
});
@ -68,7 +78,7 @@ void Function() inlineAssetsTests(InlineAssetTest f) {
test('populates a <style>', () {
var style = doc.querySelector('style');
expect(style.innerHtml.trim(), contents['site.css']);
expect(style?.innerHtml?.trim(), contents['site.css']);
});
test('heeds data-no-inline', () {