From 98e289744a516bf7e1996515da94c5fc4bff9b7a Mon Sep 17 00:00:00 2001 From: Daniel Nitsikopoulos Date: Sun, 16 Jun 2024 10:14:57 +1000 Subject: [PATCH] Add speculation rules for pre-fetching and rendering --- config/app.rb | 1 + slices/main/templates/layouts/app.html.slim | 3 ++ slices/main/view.rb | 1 - slices/main/views/context.rb | 33 +++++++++++++++++++++ 4 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 slices/main/views/context.rb diff --git a/config/app.rb b/config/app.rb index 61d66b9..a1ed989 100644 --- a/config/app.rb +++ b/config/app.rb @@ -6,6 +6,7 @@ module Adamantium class App < Hanami::App config.actions.content_security_policy[:script_src] += " 'unsafe-eval' https://gist.github.com" config.actions.content_security_policy[:script_src] += " *.dnitza.com" + config.actions.content_security_policy[:script_src] += " 'inline-speculation-rules' hash-sha256-GSFnYlZ2/GzNMMMe8WYjAF1Xb+IxEi2PZpSLZO+va38=" config.actions.content_security_policy[:script_src] += " https://api.mapbox.com/mapbox-gl-js/v2.9.1/mapbox-gl.js" config.actions.content_security_policy[:media_src] += " https://dnitza.com" config.actions.content_security_policy[:script_src] += " https://unpkg.com/htmx.org@1.9.2/dist/htmx.min.js " diff --git a/slices/main/templates/layouts/app.html.slim b/slices/main/templates/layouts/app.html.slim index 304e749..21a5098 100644 --- a/slices/main/templates/layouts/app.html.slim +++ b/slices/main/templates/layouts/app.html.slim @@ -38,6 +38,9 @@ html x-data="{darkMode: $persist(false)}" :class="{'dark' : darkMode === true}" link rel="stylesheet" href="https://unpkg.com/@highlightjs/cdn-assets@11.8.0/styles/github-dark.min.css" script src="https://unpkg.com/@highlightjs/cdn-assets@11.8.0/highlight.min.js" defer="" + script type="speculationrules" + == context.speculation_rules + meta property="og:title" content="#{context.content_for(:title)}" meta property="og:type" content="website" meta property="og:url" content="#{context.content_for(:url)}" diff --git a/slices/main/view.rb b/slices/main/view.rb index a81b570..3d1420b 100644 --- a/slices/main/view.rb +++ b/slices/main/view.rb @@ -5,7 +5,6 @@ require "hanami/view" module Main class View < Hanami::View - config.default_context = Adamantium::Context.new config.layouts_dir = "layouts" config.layout = "app" config.paths = "slices/main/templates" diff --git a/slices/main/views/context.rb b/slices/main/views/context.rb new file mode 100644 index 0000000..dc111f4 --- /dev/null +++ b/slices/main/views/context.rb @@ -0,0 +1,33 @@ +module Main + module Views + class Context < Adamantium::Context + include Deps["repos.page_repo"] + + def speculation_rules + {"prerender": [ + { + "where": { + "and": [ + {"href_matches": "/*"}, + ] + } + } + ], + "prefetch": [ + { + "urls": ["bookmarks", "posts", "photos", "statuses", "uses"], + "referrer_policy": "no-referrer" + } + ]}.to_json + end + + def pages + page_repo.for_main_nav + end + + def current_path + request.fullpath + end + end + end +end