From f865315a67dabe685eb5aee290c677b7f3c2e005 Mon Sep 17 00:00:00 2001 From: Chris Date: Sun, 17 May 2026 20:36:18 +0200 Subject: [PATCH] smarter way --- index.php => web/generator.php | 47 ++---------------------- web/index.php | 67 ++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 43 deletions(-) rename index.php => web/generator.php (77%) create mode 100644 web/index.php diff --git a/index.php b/web/generator.php similarity index 77% rename from index.php rename to web/generator.php index c69ba0d..a77fbc7 100644 --- a/index.php +++ b/web/generator.php @@ -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); diff --git a/web/index.php b/web/index.php new file mode 100644 index 0000000..185989f --- /dev/null +++ b/web/index.php @@ -0,0 +1,67 @@ + + + + + + + + + Generation Error + + + + + +

Page being generated, please wait

+

+ Generating content... this should complete in about 30 seconds. +

+
+
+
+

0%

+ + + + + \ No newline at end of file