Support server-rendered index.html for all clients. Closes #1871

This commit is contained in:
Gabe Kangas 2022-06-19 16:35:55 -07:00
parent 78c6189c02
commit ff968616ba
No known key found for this signature in database
GPG key ID: 9A56337728BC81EA
5 changed files with 69 additions and 57 deletions

View file

@ -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)

16
static/static.go vendored
View file

@ -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
}

View file

@ -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 == "" {

View file

@ -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

View file

@ -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 (
<>
<Head>
<meta name="description" content="{{.Summary}}" />
<meta property="og:title" content="{{.Name}}" />
<meta property="og:site_name" content="{{.Name}}" />
<meta property="og:url" content="{{.RequestedURL}}" />
<meta property="og:description" content="{{.Summary}}" />
<meta property="og:type" content="video.other" />
<meta property="video:tag" content="{{.TagsString}}" />
<meta property="og:image" content="{{.Thumbnail}}" />
<meta property="og:image:url" content="{{.Thumbnail}}" />
<meta property="og:image:alt" content="{{.Image}}" />
<meta property="og:video" content="{{.RequestedURL}}embed/video" />
<meta property="og:video:secure_url" content="{{.RequestedURL}}embed/video" />
<meta property="og:video:height" content="315" />
<meta property="og:video:width" content="560" />
<meta property="og:video:type" content="text/html" />
<meta property="og:video:actor" content="{{.Name}}" />
<meta property="twitter:title" content="{{.Name}}" />
<meta property="twitter:url" content="{{.RequestedURL}}" />
<meta property="twitter:description" content="{{.Summary}}" />
<meta property="twitter:image" content="{{.Image}}" />
<meta property="twitter:card" content="player" />
<meta property="twitter:player" content="{{.RequestedURL}}embed/video" />
<meta property="twitter:player:width" content="560" />
<meta property="twitter:player:height" content="315" />
<link rel="apple-touch-icon" sizes="57x57" href="/img/favicon/apple-icon-57x57.png" />
<link rel="apple-touch-icon" sizes="60x60" href="/img/favicon/apple-icon-60x60.png" />
<link rel="apple-touch-icon" sizes="72x72" href="/img/favicon/apple-icon-72x72.png" />
<link rel="apple-touch-icon" sizes="76x76" href="/img/favicon/apple-icon-76x76.png" />
<link rel="apple-touch-icon" sizes="114x114" href="/img/favicon/apple-icon-114x114.png" />
<link rel="apple-touch-icon" sizes="120x120" href="/img/favicon/apple-icon-120x120.png" />
<link rel="apple-touch-icon" sizes="144x144" href="/img/favicon/apple-icon-144x144.png" />
<link rel="apple-touch-icon" sizes="152x152" href="/img/favicon/apple-icon-152x152.png" />
<link rel="apple-touch-icon" sizes="180x180" href="/img/favicon/apple-icon-180x180.png" />
<link
rel="icon"
type="image/png"
sizes="192x192"
href="/img/favicon/android-icon-192x192.png"
/>
<link rel="icon" type="image/png" sizes="32x32" href="/img/favicon/favicon-32x32.png" />
<link rel="icon" type="image/png" sizes="96x96" href="/img/favicon/favicon-96x96.png" />
<link rel="icon" type="image/png" sizes="16x16" href="/img/favicon/favicon-16x16.png" />
<link rel="manifest" href="/manifest.json" />
<link href="/api/auth/provider/indieauth" />
<meta name="msapplication-TileColor" content="#ffffff" />
<meta name="msapplication-TileImage" content="/img/favicon/ms-icon-144x144.png" />
<meta name="theme-color" content="#ffffff" />
</Head>
<ClientConfigStore />
<Layout>
<Header name={title || name} chatAvailable={isChatAvailable} />