dvHostSiteFileFor function

DVHostSiteFile? dvHostSiteFileFor(
  1. DVHosts hosts, {
  2. required String? host,
  3. required String path,
  4. required Iterable<String> patterns,
  5. required String? robots,
  6. required String? sitemap,
})

/robots.txt or /sitemap.xml on host, or null when path is neither or the build wrote no such file.

robots and sitemap are what the build wrote, or null. The sitemap lists every page's public URL; each host is answered with the entries on its own origin and on the wildcard hosts it lists (DVHosts.sitemapHostOf), and a robots.txt naming its own sitemap and, on a wildcard host, the one that lists it too. A host that serves none of the route patterns -- a wildcard label nothing is published under -- is answered 404 for both. Decided from hosts, which the caller parsed from the manifest, never from the files' text.

Implementation

DVHostSiteFile? dvHostSiteFileFor(
  DVHosts hosts, {
  required String? host,
  required String path,
  required Iterable<String> patterns,
  required String? robots,
  required String? sitemap,
}) {
  if (hosts.isEmpty) return null;
  if (path != '/robots.txt' && path != '/sitemap.xml') return null;
  if ((path == '/robots.txt' ? robots : sitemap) == null) return null;
  final DVHostMatch match = hosts.matchOrPrimary(host);
  if (!match.servesAnyOf(patterns)) {
    return (status: 404, contentType: 'text/plain; charset=utf-8', body: 'Not found');
  }
  if (path == '/robots.txt') {
    final DVHostMatch listedBy = hosts.sitemapHostOf(match);
    return (
      status: 200,
      contentType: 'text/plain; charset=utf-8',
      body: dvRobotsTxt(
        siteUrl: match.origin,
        sitemap: sitemap != null,
        alsoListedBy: <String>[
          if (sitemap != null && listedBy.origin != match.origin) listedBy.origin,
        ],
      ),
    );
  }
  return (
    status: 200,
    contentType: 'application/xml; charset=utf-8',
    body: dvSitemapForHost(sitemap!, hosts, match),
  );
}