App-Dapper

 view release on metacpan or  search on metacpan

lib/App/Dapper.pm  view on Meta::CPAN

sub live_reload_script {
    return <<'HTML';
<script>
(function () {
  var last = null;
  function poll() {
    fetch('/.dapper-reload?' + Date.now(), { cache: 'no-store' })
      .then(function (r) { return r.ok ? r.text() : last; })
      .then(function (t) { if (last !== null && t !== last) { location.reload(); } last = t; })
      .catch(function () {})
      .then(function () { setTimeout(poll, 1000); });
  }
  poll();
})();
</script>
HTML
}

# sub write_generated - Write the files Dapper makes for the whole site:
# redirects, the sitemap, the feed, and llms.txt and llms-full.txt. A page that writes the
# same file wins, with a warning.

t/36-output-clean.t  view on Meta::CPAN

        '_layout/index.html' => "<html><body>[% page.content %]</body></html>",
        '_layout/upper.html' => "<HTML><BODY>B</BODY></HTML>",
        '_layout/twice.html' => "<body>1</body><body>2</body>",
        '_layout/bare.html'  => "[% page.content %]",
    });
    my $d = App::Dapper->new();
    $d->{quiet} = 1;
    $d->{live_reload} = 1;
    $d->build();
    my $script = App::Dapper::live_reload_script();
    like($script, qr{^<script>\n.*fetch\('/\.dapper-reload\?' \+ Date\.now\(\).*location\.reload\(\).*setTimeout\(poll, 1000\).*</script>\n\z}s,
        'the script polls /.dapper-reload every second');
    is(slurp('_output/a.html'), "<html><body><p>x</p>\n${script}</body></html>", 'inserted right before </body>');
    is(slurp('_output/upper.html'), "<HTML><BODY>B${script}</BODY></HTML>", 'case-insensitive');
    is(slurp('_output/twice.html'), "<body>1${script}</body><body>2</body>", 'only once');
    is(slurp('_output/nobody.html'), "<p>x</p>\n${script}", 'appended without a </body>');
    is(slurp('_output/old.htm'), "<p>x</p>\n${script}", '.htm is HTML too');
    is(slurp('_output/data.json'), "{}\n", 'other files are left alone');
    like(slurp('_output/.dapper-reload'), qr/^\d+-\d+\n\z/, 'the marker');
    ok($d->{written}->{'.dapper-reload'}, 'is remembered as written');
    ok(!-e '_output/a.md' || slurp('_output/a.md') !~ /dapper-reload/, 'not in the Markdown copy');



( run in 2.396 seconds using v1.01-cache-2.11-cpan-234fa220139 )