smarter way
This commit is contained in:
@@ -2,50 +2,10 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/vendor/autoload.php';
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
header('Content-Type: text/html; charset=UTF-8');
|
||||
|
||||
if (PHP_SAPI !== 'cli') {
|
||||
$method = $_SERVER['REQUEST_METHOD'] ?? 'GET';
|
||||
if ($method !== 'GET') {
|
||||
http_response_code(405);
|
||||
header('Allow: GET');
|
||||
echo 'Method Not Allowed';
|
||||
exit;
|
||||
}
|
||||
|
||||
$secPurpose = strtolower((string) ($_SERVER['HTTP_SEC_PURPOSE'] ?? ''));
|
||||
$purpose = strtolower((string) ($_SERVER['HTTP_PURPOSE'] ?? ''));
|
||||
$xMoz = strtolower((string) ($_SERVER['HTTP_X_MOZ'] ?? ''));
|
||||
|
||||
$isSpeculative = str_contains($secPurpose, 'prefetch')
|
||||
|| str_contains($secPurpose, 'prerender')
|
||||
|| str_contains($purpose, 'prefetch')
|
||||
|| str_contains($purpose, 'prerender')
|
||||
|| str_contains($xMoz, 'prefetch');
|
||||
|
||||
if (! $isSpeculative) {
|
||||
$secFetchMode = strtolower((string) ($_SERVER['HTTP_SEC_FETCH_MODE'] ?? ''));
|
||||
$secFetchDest = strtolower((string) ($_SERVER['HTTP_SEC_FETCH_DEST'] ?? ''));
|
||||
$secFetchUser = (string) ($_SERVER['HTTP_SEC_FETCH_USER'] ?? '');
|
||||
|
||||
// Treat obvious non-navigation fetches as non-user document requests.
|
||||
if (($secFetchMode !== '' && $secFetchMode !== 'navigate')
|
||||
|| ($secFetchDest !== '' && $secFetchDest !== 'document')
|
||||
|| ($secFetchUser !== '' && $secFetchUser !== '?1')) {
|
||||
$isSpeculative = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ($isSpeculative) {
|
||||
http_response_code(204);
|
||||
header('X-Skipped-Generation: speculative-request');
|
||||
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
$startedAt = microtime(true);
|
||||
|
||||
$apiKey = getenv('LLAMA_API_KEY') ?: '';
|
||||
@@ -57,6 +17,7 @@ $topicHint = isset($_GET['topic']) ? trim((string) $_GET['topic']) : '';
|
||||
$topicHint = mb_substr($topicHint, 0, 120);
|
||||
|
||||
$seed = sprintf('seed-%08x%08x', random_int(0, 0xffffffff), random_int(0, 0xffffffff));
|
||||
header('HX-Redirect: /' . $seed);
|
||||
|
||||
$topicCategories = [
|
||||
'local restaurant landing page',
|
||||
@@ -174,9 +135,9 @@ try {
|
||||
$html .= $timingBlock;
|
||||
}
|
||||
|
||||
echo $html;
|
||||
//echo $html;
|
||||
|
||||
file_put_contents(__DIR__ . '/pages/' . sha1($seed) . '.html', $html);
|
||||
file_put_contents(__DIR__ . '/../pages/' . $seed . '.html', $html);
|
||||
|
||||
} catch (Throwable $e) {
|
||||
http_response_code(500);
|
||||
67
web/index.php
Normal file
67
web/index.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
$url = array_filter(explode('/',ltrim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH),'/')));
|
||||
|
||||
if (count($url) === 1 && str_starts_with($url[0], 'seed-')) {
|
||||
|
||||
$file = __DIR__ . '/../pages/' . $url[0] . '.html';
|
||||
if (file_exists($file)) {
|
||||
header('Content-Type: text/html; charset=UTF-8');
|
||||
echo file_get_contents($file);
|
||||
exit;
|
||||
}
|
||||
else {
|
||||
http_response_code(404);
|
||||
echo 'Page not found';
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
|
||||
<!doctype html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Generation Error</title>
|
||||
<script src="https://cdn.tailwindcss.com"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/htmx.org@2.0.10/dist/htmx.min.js"></script>
|
||||
</head>
|
||||
|
||||
<body class="min-h-screen bg-zinc-950 text-zinc-100 p-8" hx-get="/generator.php" hx-trigger="load">
|
||||
<h1 class="text-2xl font-bold mb-4">Page being generated, please wait</h1>
|
||||
<p class="text-zinc-300 mb-3">
|
||||
Generating content... this should complete in about 30 seconds.
|
||||
</p>
|
||||
<div class="w-full max-w-xl h-3 bg-zinc-800 rounded overflow-hidden mb-2" aria-label="Generation progress">
|
||||
<div id="progress-bar" class="h-full bg-emerald-500 w-0 transition-[width] duration-100"></div>
|
||||
</div>
|
||||
<p id="progress-text" class="text-zinc-400 text-sm mb-3">0%</p>
|
||||
|
||||
<script>
|
||||
(() => {
|
||||
const durationMs = 30000;
|
||||
const start = performance.now();
|
||||
const bar = document.getElementById('progress-bar');
|
||||
const text = document.getElementById('progress-text');
|
||||
|
||||
function tick(now) {
|
||||
const elapsed = now - start;
|
||||
const progress = Math.min(elapsed / durationMs, 1);
|
||||
const percent = Math.round(progress * 100);
|
||||
|
||||
bar.style.width = percent + '%';
|
||||
text.textContent = percent + '%';
|
||||
|
||||
if (progress < 1) {
|
||||
requestAnimationFrame(tick);
|
||||
}
|
||||
}
|
||||
|
||||
requestAnimationFrame(tick);
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
Reference in New Issue
Block a user