dvStructuredData function

String dvStructuredData({
  1. required String route,
  2. required String title,
  3. required String siteName,
  4. String? description,
  5. String? siteUrl,
  6. String? image,
  7. String? schemaType,
})

JSON-LD describing what this page is.

OpenGraph and Twitter tags say how a link should look when shared. They do not say what the page is: og:type is website for every page on every site. Schema.org is the vocabulary that does, and it is what produces a site name in a result, a breadcrumb trail under a link, and a sitelinks search box.

The root gets WebSite and nothing else gets it — repeating it on every page tells a crawler the site begins again at each URL. Inner pages get WebPage pointing back at the site, and a BreadcrumbList built from the path, because the path is the hierarchy in a URL-first framework.

Implementation

String dvStructuredData({
  required String route,
  required String title,
  required String siteName,
  String? description,
  String? siteUrl,
  String? image,
  String? schemaType,
}) {
  // Every identifier in this vocabulary is an absolute URL. Emitting it with
  // relative ones produces a block that validates and describes nothing.
  if (siteUrl == null || siteUrl.isEmpty) return '';

  final String root = siteUrl.endsWith('/')
      ? siteUrl.substring(0, siteUrl.length - 1)
      : siteUrl;
  final String canonical = dvStaticCanonical(root, route);
  final blocks = <Map<String, Object?>>[];

  if (route == '/') {
    blocks.add(<String, Object?>{
      '@context': 'https://schema.org',
      '@type': 'WebSite',
      'name': siteName,
      'url': root,
      if (description != null && description.isNotEmpty)
        'description': description,
      if (image != null && image.isNotEmpty) 'image': image,
    });
  } else {
    blocks.add(<String, Object?>{
      '@context': 'https://schema.org',
      // What the model said it is, when a model owns this route. WebPage is
      // true of every page ever written and is what a crawler falls back to
      // anyway; Product, Recipe and JobPosting are what a rich result is
      // keyed off, and the declaration reached the web server and stopped
      // there.
      '@type': schemaType == null || schemaType.isEmpty ? 'WebPage' : schemaType,
      'name': title,
      'url': canonical,
      if (description != null && description.isNotEmpty)
        'description': description,
      if (image != null && image.isNotEmpty) 'image': image,
      'isPartOf': <String, Object?>{
        '@type': 'WebSite',
        'name': siteName,
        'url': root,
      },
    });
    blocks.add(_breadcrumbs(root: root, route: route, title: title));
  }

  return blocks.map(_script).join('\n');
}