FEATURE: Setting for short title used by Android on homescreen

FEATURE: Setting for short title used by Android on homescreen

From e7b76b319adaed7b7b336248c4047f24373e98d2 Mon Sep 17 00:00:00 2001
From: Gerhard Schlager <mail@gerhard-schlager.at>
Date: Wed, 28 Nov 2018 14:55:52 +0100
Subject: [PATCH] FEATURE: Setting for short title used by Android on
 homescreen


diff --git a/app/controllers/metadata_controller.rb b/app/controllers/metadata_controller.rb
index 07a3ac7..dacba4d 100644
--- a/app/controllers/metadata_controller.rb
+++ b/app/controllers/metadata_controller.rb
@@ -27,7 +27,6 @@ class MetadataController < ApplicationController
 
     manifest = {
       name: SiteSetting.title,
-      short_name: SiteSetting.title,
       display: display,
       start_url: Discourse.base_uri.present? ? "#{Discourse.base_uri}/" : '.',
       background_color: "##{ColorScheme.hex_for_name('secondary', view_context.scheme_id)}",
@@ -41,6 +40,8 @@ class MetadataController < ApplicationController
       ]
     }
 
+    manifest[:short_name] = SiteSetting.short_title if SiteSetting.short_title.present?
+
     if SiteSetting.native_app_install_banner
       manifest = manifest.merge(
         prefer_related_applications: true,
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 8c8da6b..4c643d4 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -1856,6 +1856,8 @@ en:
     push_notifications_prompt: "Display user consent prompt."
     push_notifications_icon: "The badge icon that appears in the notification corner. Recommended size is 96px by 96px."
 
+    short_title: "The short title will be used on the user's home screen, launcher, or other places where space may be limited. A maximum of 12 characters is recommended."
+
     errors:
       invalid_email: "Invalid email address."
       invalid_username: "There's no user with that username."
diff --git a/config/site_settings.yml b/config/site_settings.yml
index 3639fde..b64d919 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -281,6 +281,8 @@ basic:
   push_notifications_icon_url:
     hidden: true
     default: ''
+  short_title:
+    default: ''
   vapid_public_key_bytes:
     default: ''
     client: true
diff --git a/spec/requests/metadata_controller_spec.rb b/spec/requests/metadata_controller_spec.rb
index 2047b13..e5b2f03 100644
--- a/spec/requests/metadata_controller_spec.rb
+++ b/spec/requests/metadata_controller_spec.rb
@@ -63,6 +63,19 @@ RSpec.describe MetadataController do
       expect(manifest["display"]).to eq("standalone")
     end
 
+    it 'uses the short_title if it is set' do
+      get "/manifest.webmanifest"
+      expect(response.status).to eq(200)
+      manifest = JSON.parse(response.body)
+      expect(manifest).to_not have_key("short_name")
+
+      SiteSetting.short_title = "foo"
+
+      get "/manifest.webmanifest"
+      expect(response.status).to eq(200)
+      manifest = JSON.parse(response.body)
+      expect(manifest["short_name"]).to eq("foo")
+    end
   end
 
   describe 'opensearch.xml' do

GitHub

1 Like

Should it fall back to SiteSetting.title if it isn’t present?

It’s not required. The user agent should fall back to name automatically and in my tests on Android it did.

On the other hand, Lighthouse complains when short_name is missing, but it complains about other things too and it’s still working. :man_shrugging:

2 Likes