From 03fe5c79fc35a079f7ad50439121c8236c802554 Mon Sep 17 00:00:00 2001 From: Damien Dart Date: Thu, 21 Nov 2024 23:16:08 +0000 Subject: [PATCH] Add initial speculation rules implementation. --- private/apache2/directory.conf | 8 ++++++++ src/ssg/ValueObjects/InputFileCollection.php | 3 ++- src/ssg/input/speculation-rules.json | 18 ++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 src/ssg/input/speculation-rules.json diff --git a/private/apache2/directory.conf b/private/apache2/directory.conf index a5b7266..38ca0aa 100644 --- a/private/apache2/directory.conf +++ b/private/apache2/directory.conf @@ -128,3 +128,11 @@ RewriteCond %1.%2 -f RewriteRule ^(.*)\.\d{14}\.(avif|css|js|png|svg|webp|woff|woff2)$ $1.${RELEASE_TIMESTAMP}.$2 [L,NE,R=permanent] + + + Header set Content-Type: "application/speculationrules+json" + + + + Header set Speculation-Rules: '"/speculation-rules.json"' + diff --git a/src/ssg/ValueObjects/InputFileCollection.php b/src/ssg/ValueObjects/InputFileCollection.php index 5f87648..59d90c6 100644 --- a/src/ssg/ValueObjects/InputFileCollection.php +++ b/src/ssg/ValueObjects/InputFileCollection.php @@ -49,7 +49,8 @@ public function getSitemapEntries(): array \assert(\is_string($slug)); if ( - 'robots.txt' === $slug + 'speculation-rules.json' === $slug + || 'robots.txt' === $slug || 'sitemap.xml' === $slug || str_starts_with($slug, 'google') || str_ends_with($slug, '.atom') diff --git a/src/ssg/input/speculation-rules.json b/src/ssg/input/speculation-rules.json new file mode 100644 index 0000000..527718d --- /dev/null +++ b/src/ssg/input/speculation-rules.json @@ -0,0 +1,18 @@ +{ + "prefetch": [ + { + "where": { + "selector_matches": "a[href^='/']" + }, + "eagerness": "immediate" + } + ], + "prerender": [ + { + "where": { + "selector_matches": "a[href^='/']" + }, + "eagerness": "moderate" + } + ] +}