76 lines
2.9 KiB
Markdown
76 lines
2.9 KiB
Markdown
# Protevus SEO
|
|
|
|
![Pub Version (including pre-releases)](https://img.shields.io/pub/v/protevus_seo?include_prereleases)
|
|
[![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety)
|
|
[![Discord](https://img.shields.io/discord/1060322353214660698)](https://discord.gg/3X6bxTUdCM)
|
|
[![License](https://img.shields.io/github/license/dart-backend/protevus)](https://github.com/dart-backend/protevus/tree/master/packages/seo/LICENSE)
|
|
|
|
Helpers for building SEO-friendly Web pages in Protevus. The goal of `package:protevus_seo` is to speed up perceived client page loads, prevent the infamous [flash of unstyled content](https://en.wikipedia.org/wiki/Flash_of_unstyled_content), and other SEO optimizations that can easily become tedious to perform by hand.
|
|
|
|
## Disabling inlining per-element
|
|
|
|
Add a `data-no-inline` attribute to a `link` or `script` to prevent inlining it:
|
|
|
|
```html
|
|
<script src="main.dart.js" data-no-inline></script>
|
|
```
|
|
|
|
## `inlineAssets`
|
|
|
|
A [response finalizer](https://protevus-docs.dukefirehawk.com/guides/request-lifecycle) that can be used in any application to patch HTML responses, including those sent with a templating engine like Jael3.
|
|
|
|
In any `text/html` response 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`.
|
|
|
|
```dart
|
|
import 'package:protevus_framework/protevus_framework.dart';
|
|
import 'package:protevus_seo/protevus_seo.dart';
|
|
import 'package:protevus_static/protevus_static.dart';
|
|
import 'package:file/local.dart';
|
|
|
|
void main() async {
|
|
var app = Protevus()..lazyParseBodies = true;
|
|
var fs = const LocalFileSystem();
|
|
var http = ProtevusHttp(app);
|
|
|
|
app.responseFinalizers.add(inlineAssets(fs.directory('web')));
|
|
|
|
app.use(() => throw ProtevusHttpException.notFound());
|
|
|
|
var server = await http.startServer('127.0.0.1', 3000);
|
|
print('Listening at http://${server.address.address}:${server.port}');
|
|
}
|
|
```
|
|
|
|
## `inlineAssetsFromVirtualDirectory`
|
|
|
|
This function is a simple one; it wraps a `VirtualDirectory` to patch the way it sends `.html` files. Produces the same functionality as `inlineAssets`.
|
|
|
|
```dart
|
|
import 'package:protevus_framework/protevus_framework.dart';
|
|
import 'package:protevus_seo/protevus_seo.dart';
|
|
import 'package:protevus_static/protevus_static.dart';
|
|
import 'package:file/local.dart';
|
|
|
|
void main() async {
|
|
var app = Protevus()..lazyParseBodies = true;
|
|
var fs = const LocalFileSystem();
|
|
var http = ProtevusHttp(app);
|
|
|
|
var vDir = inlineAssets(
|
|
VirtualDirectory(
|
|
app,
|
|
fs,
|
|
source: fs.directory('web'),
|
|
),
|
|
);
|
|
|
|
app.use(vDir.handleRequest);
|
|
|
|
app.use(() => throw ProtevusHttpException.notFound());
|
|
|
|
var server = await http.startServer('127.0.0.1', 3000);
|
|
print('Listening at http://${server.address.address}:${server.port}');
|
|
}
|
|
```
|