From ff968616baeeafe68f0ff6864f3986d1c0dd8468 Mon Sep 17 00:00:00 2001 From: Gabe Kangas Date: Sun, 19 Jun 2022 16:35:55 -0700 Subject: [PATCH] Support server-rendered index.html for all clients. Closes #1871 --- controllers/index.go | 8 +---- static/static.go | 16 +++++---- utils/utils.go | 29 ----------------- utils/utils_test.go | 15 --------- web/components/layouts/Main.tsx | 58 +++++++++++++++++++++++++++++++++ 5 files changed, 69 insertions(+), 57 deletions(-) diff --git a/controllers/index.go b/controllers/index.go index 0920fb98a..20753d2c1 100644 --- a/controllers/index.go +++ b/controllers/index.go @@ -36,12 +36,6 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) { isIndexRequest := r.URL.Path == "/" || filepath.Base(r.URL.Path) == "index.html" || filepath.Base(r.URL.Path) == "" if isIndexRequest { - serveWeb(w, r) - return - } - // For search engine bots and social scrapers return a special - // server-rendered page. - if utils.IsUserAgentABot(r.UserAgent()) && isIndexRequest { handleScraperMetadataPage(w, r) return } @@ -78,7 +72,7 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) { // Return a basic HTML page with server-rendered metadata from the config // to give to Opengraph clients and web scrapers (bots, web crawlers, etc). func handleScraperMetadataPage(w http.ResponseWriter, r *http.Request) { - tmpl, err := static.GetBotMetadataTemplate() + tmpl, err := static.GetWebIndexTemplate() if err != nil { log.Errorln(err) w.WriteHeader(http.StatusInternalServerError) diff --git a/static/static.go b/static/static.go index d8a546853..5aa5945bd 100644 --- a/static/static.go +++ b/static/static.go @@ -5,6 +5,8 @@ import ( "html/template" "os" "path/filepath" + + "github.com/pkg/errors" ) //go:embed web/* @@ -18,13 +20,15 @@ func GetWeb() embed.FS { return webFiles } -//go:embed metadata.html.tmpl -var botMetadataTemplate embed.FS +// GetWebIndexTemplate will return the bot/scraper metadata template. +func GetWebIndexTemplate() (*template.Template, error) { + webFiles := GetWeb() + name := "web/index.html" + t, err := template.ParseFS(webFiles, name) + if err != nil { + return nil, errors.Wrap(err, "unable to open index.html template") + } -// GetBotMetadataTemplate will return the bot/scraper metadata template. -func GetBotMetadataTemplate() (*template.Template, error) { - name := "metadata.html.tmpl" - t, err := template.ParseFS(botMetadataTemplate, name) tmpl := template.Must(t, err) return tmpl, err } diff --git a/utils/utils.go b/utils/utils.go index f6a32762a..6cd604940 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -15,7 +15,6 @@ import ( "strings" "time" - "github.com/mssola/user_agent" log "github.com/sirupsen/logrus" "github.com/yuin/goldmark" "github.com/yuin/goldmark/extension" @@ -98,34 +97,6 @@ func moveFallback(source, destination string) error { return nil } -// IsUserAgentABot returns if a web client user-agent is seen as a bot. -func IsUserAgentABot(userAgent string) bool { - if userAgent == "" { - return false - } - - botStrings := []string{ - "mastodon", - "pleroma", - "applebot", - "whatsapp", - "matrix", - "synapse", - "element", - "rocket.chat", - "duckduckbot", - } - - for _, botString := range botStrings { - if strings.Contains(strings.ToLower(userAgent), botString) { - return true - } - } - - ua := user_agent.New(userAgent) - return ua.Bot() -} - // IsUserAgentAPlayer returns if a web client user-agent is seen as a media player. func IsUserAgentAPlayer(userAgent string) bool { if userAgent == "" { diff --git a/utils/utils_test.go b/utils/utils_test.go index 9b8bfcdbc..cdabe55ea 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -4,21 +4,6 @@ import ( "testing" ) -func TestUserAgent(t *testing.T) { - testAgents := []string{ - "Pleroma 1.0.0-1168-ge18c7866-pleroma-dot-site; https://pleroma.site info@pleroma.site", - "Mastodon 1.2.3 Bot", - "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Safari/605.1.15 (Applebot/0.1; +http://www.apple.com/go/applebot)", - "WhatsApp", - } - - for _, agent := range testAgents { - if !IsUserAgentABot(agent) { - t.Error("Incorrect parsing of useragent", agent) - } - } -} - func TestGetHashtagsFromText(t *testing.T) { text := `Some text with a #hashtag goes here.\n\n Another #secondhashtag, goes here.\n\n diff --git a/web/components/layouts/Main.tsx b/web/components/layouts/Main.tsx index 900e2a41f..7bc4e7e39 100644 --- a/web/components/layouts/Main.tsx +++ b/web/components/layouts/Main.tsx @@ -1,5 +1,6 @@ import { Layout } from 'antd'; import { useRecoilValue } from 'recoil'; +import Head from 'next/head'; import { ClientConfigStore, isChatAvailableSelector, @@ -19,6 +20,63 @@ function Main() { return ( <> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +