Android mobile device detection is too strict

Has there been any thought on reviewing how Discourse detects mobile devices? I was checking in my Chrome developer tools, and it doesn’t appear that LG devices are detected as mobile. I haven’t tested on a Samsung S6 or Motorola devices.

What is the current logic? Could it be improved?


We should revisit this @neil since the nexus 7 is no longer default mobile as well.

1 Like

This is the algorithm we is crazy simple:

compare to say,

which recommends the crazy :banana: :information_desk_person:

def redirect_mobile(url = "")
  redirect_to url if /(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.match(request.user_agent) || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.match(request.user_agent[0..3])

So it all just depends on how far we want to push this. Also it may make sense just to do the switch based on screen width in the client side, but it would change a big pile of things.


Maybe the simplest would be to have a “switch to mobile view” option for desktop view similar to the “switch to desktop view” that mobile view has?

IMHO it would still be good to have some detection in place, but for those that fall through the cracks I think there would be a better chance of them finding a “switch” link than it would be for them to realize they need to do a ?mobile_view=1

1 Like

There should be a switch to mobile if we detect its a mobile device via JavaScript already in place.


Wait, are you saying I shouldn’t see this button in Chrome on my laptop?

What is the criterion for mobile devices in the client-side JavaScript code?

This already exists, not sure why we’re discussing it…

Agreed but I know with 100% certainty that Nexus 7 used to be detected as mobile, and is not now.

user_agent =~ /Mobile|webOS|Nexus 7/ && !(user_agent =~ /iPad/)

To be clear, my goals are

  1. We should auto-detect mobile in Android Chrome and Android Firefox on Android (because Android tablets, LOL) and we can test this on our team Nexus 6p devices + the Nexus 7 devices…
  2. We should auto-detect mobile in Mobile Safari on 2012+ iOS 8,9 handheld devices

So I think only #1 is failing us at the moment…

Not sure it is related but I have some problems displaying our forum on an IPhone 6 plus.

Using safari on my iPhone 6 plus


and using Chrome on my iPhone 6 plus


my user agent using safari is

Mozilla/5.0 (iPhone; CPU iPhone OS 9_2 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13C75 Safari/601

and my iOS version is 9.2 (13C75)

An other problem: When displaying the site using my phone in landscape mode The top navigation bar disappears.

You must have some unusual setup in Safari, or unusual site configuration – I have never seen any Discourse site have trouble auto-selecting the mobile layout on iPhone… ever.

What is the user agent on your Nexus 7? Maybe it changed recently

Would discourse ever consider leveraging a service like wurfl? or making this an option for admins in settings?

“You can use the service free of charge, as long as your website is publicly available and does not require fees or paid subscription to access. Read the complete license here.”

Unlikely, why take on a giant external dependency for something small like this?

Also — anyone reading this who wants better android mobile device detection, please visit this site with the target device and paste in your results here, along with the specific make / model of your device as well as OS / browser version.

Remember, we want to favor very popular Android devices running Android/Chrome, not necessarily all of them :wink:

1 Like

OK this Nexus 7 2013 with latest everything and beta Chrome is

Mozilla/5.0 (Linux; Android 6.0.1; Nexus 7 Build/MMB29Q) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.91 Safari/537.36

Very weird, the query string matches there … does this happen when you browse incognito?

Nexus 10 2013-ish

Mozilla/5.0 (Linux; Android 5.1.1; Nexus 10 Build/LMY49G) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.91 Safari/537.36

Note that on a device of this size, mobile version is the wrong choice for most users.

It seems to me that maybe viewport dimensions would be the way to go?

Trouble is, it would be a pretty complex change to shift the logic to the client.

1 Like

Then mobile version is structurally different, not just a set of CSS changes? That I could see being challenging…

If you ever click to view in desktop mode, it remembers forever. Do you get this issue when you browse incognito? Try going to hamburger and selecting mobile view.