Skip to content
Snippets Groups Projects
Unverified Commit 0ee0db7e authored by Agate's avatar Agate :speech_balloon:
Browse files

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

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