Unverified Commit 0ee0db7e authored by Agate's avatar Agate 💬

Fix #1107: Fix HTML <title> not including instance name in some situations

parent 90427331
......@@ -39,6 +39,9 @@ def serve_spa(request):
settings.FUNKWHALE_SPA_REWRITE_MANIFEST_URL
or federation_utils.full_url(urls.reverse("api:v1:instance:spa-manifest"))
)
title = preferences.get("instance__name")
if title:
head = replace_title(head, title)
head = replace_manifest_url(head, new_url)
if not preferences.get("common__api_authentication_required"):
......@@ -82,6 +85,7 @@ def serve_spa(request):
MANIFEST_LINK_REGEX = re.compile(r"<link [^>]*rel=(?:'|\")?manifest(?:'|\")?[^>]*>")
TITLE_REGEX = re.compile(r"<title>.*</title>")
def replace_manifest_url(head, new_url):
......@@ -90,6 +94,12 @@ def replace_manifest_url(head, new_url):
return head
def replace_title(head, new_title):
replacement = "<title>{}</title>".format(html.escape(new_title))
head = TITLE_REGEX.sub(replacement, head)
return head
def get_spa_html(spa_url):
return get_spa_file(spa_url, "index.html")
......
import html
import time
import pytest
from django.http import HttpResponse
from django.urls import reverse
......@@ -55,10 +55,12 @@ def test_should_fallback(path, expected, mocker):
def test_serve_spa_from_cache(mocker, settings, preferences, no_api_auth):
preferences["instance__name"] = 'Best Funkwhale "pod"'
request = mocker.Mock(path="/")
get_spa_html = mocker.patch.object(
middleware, "get_spa_html", return_value="<html><head></head></html>"
middleware,
"get_spa_html",
return_value="<html><head><title>Funkwhale</title></head></html>",
)
mocker.patch.object(
middleware,
......@@ -84,7 +86,8 @@ def test_serve_spa_from_cache(mocker, settings, preferences, no_api_auth):
assert response.status_code == 200
expected = [
"<html><head>",
"<html><head>"
"<title>{}</title>".format(html.escape(preferences["instance__name"])),
'<meta content="custom title" property="og:title" />',
'<meta content="custom description" property="og:description" />',
'<meta content="default site name" property="og:site_name" />',
......
Fix HTML <title> not including instance name in some situations (#1107)
......@@ -66,6 +66,7 @@ export default {
instanceUrl: null,
showShortcutsModal: false,
showSetInstanceModal: false,
initialTitle: document.title,
}
},
async created () {
......@@ -147,7 +148,6 @@ export default {
},
mounted () {
let self = this
// slight hack to allow use to have internal links in <translate> tags
// while preserving router behaviour
document.documentElement.addEventListener('click', function (event) {
......@@ -281,7 +281,7 @@ export default {
if (this.$store.state.ui.pageTitle) {
parts.push(this.$store.state.ui.pageTitle)
}
parts.push(this.$store.state.instance.settings.instance.name.value || 'Funkwhale')
parts.push(this.initialTitle || 'Funkwhale')
document.title = parts.join('')
},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment