Custom home page to discourse

(bunjee) #1

Greetings Discoursers,

Congratulations for what you did so far. It looks great.

I’m in the process of adding my custom home to discourse.

I’m new to Ember so I followed this cool tutorial from @eviltrout: Adding to Discourse using Ember.js Part 1: Routing and Templates - Evil Trout’s Blog.

Added the following at the end of application_routes.js:

this.resource('home', { path: '/home' });

Created home_route.js inside app / assets / javascripts / discourse / routes.

Discourse.HomeRoute = Discourse.Route.extend({
  renderTemplate: function() {

Created home.js.handlebars inside app / assets / javascripts / discourse / templates.


For some reason I still get 404 when accessing

Does anybody know why ?

Edit: I’m using bitnami and I precompile assets with the following:

sudo ​/opt/bitnami/ruby/bin/ruby bin/rake assets:precompile RAILS_ENV=production

Thanks :heart:.

Custom home page routing
Custom page preserve scrolling
How to customize your own discourse and deploy it to production on Ubuntu for beginners
(Robin Ward) #2

The issue here is the Rails app also needs to answer to /home. Otherwise it will serve up a 404. Right now all URLs have to exist in both worlds.

I’ve considered allowing a failback where a URL that doesn’t exist server side would just serve up the JS app and do nothing, but never decided if that’s a good thing or not. It’s not typical to make URLs that don’t have a server side counterpart.

Help wanted on plugin controller
Discourse.Route.buildRoutes is not a function
(bunjee) #3

Thank you sir @eviltrout.

I skimmed a little fast on the initial Rails part of the tutorial.

I guess I’ll add a controller for home in the discourse / app / controllers.

Convention over configuration should do the rest.

Takes a little time to get used to when coming from C++ :D.

(bunjee) #4

Added pages_controller.rb in discourse / app / controllers:

class PagesController < ApplicationController

Added the following in config / routes.rb:

get "/home" => "pages#home"

Finally a blank home.html.erb in discourse / app / views / pages.

Not sure if it’s the best way but it works.

Thanks again :smiley: .

(Kane York) #5

Oh yeah, anything in the public/ directory gets served up as static files. So that should help, too.

(Robbo) #6

I disagree with this. I have been working a lot with angular and we completely separate client from server side. To the point we could completely switch the server side language without client side having to be touched at all, it is just an API. With discourse I have noticed you still do server side views however are using ember, that is fine but I would expect that a public endpoint would simply be handled by ember alone, why does the server side have to process before we get the client side code? I would completely expect that the server side is only an API and since you use server side views also have an endpoint for everything non API to just return ember which would then use the API etc.

That paragraph was all over the place… as long as I can hack it or something to not have to add an endpoint every time in rails I am fine. Sounds awfully tedious.

(Robin Ward) #7

This is basically how Discourse works too with very few exceptions. Server side rendering is mostly used for emails, noscript tags for google and of course the initial payload that contains the site.

Because we use the HTML5 History API for nice looking URLs, the server has to respond to any URL and serve up the initial payload for the app as HTML/JS before Ember can begin its work.

Addtionally, we “preload” content on some URLs if we know the client app is going to need it to prevent another round trip request. If you request /t/hello/123 as your initial request, we’ll shove the topic data into the HTML so the JS can just load it. It’s considerably faster that way.

We could probably just serve up the site regardless of URL if no handler is found, but then sometimes we do want to respond with a 404, for googlebot and other reasons. Honestly, virtually all new features end up with a server side endpoint that is the same as the client one. It is quite an exception to add an entire URL like a custom homepage that does not need some kind of server response.

So this is kind of an edge case.

(Robbo) #8

Yeah I thought about it a lot and what I do is complete separation like I said. As in I give you a static HTML page which loads in assets. First thing it does is request the csrf token and then it bootstraps by getting the visitor. This works fine for me as google isn’t so important currently. However I have been thinking about when the app I work on goes public we will want certain data to be indexed so I may do something similar to this. But it still feels wrong.

I am currently evaluating discourse for some new sites where SEO will be important so once again I can see why now that I have had a think about it.

So if I get the go ahead you will see more of me (probably not a good thing ;)). Hopefully I’ll be allowed to open source the plugins I need to make if I do.

(Martis Genus) #9

Currently I try to make a plugin that does the same (making a static page the landing page). As a test I tried to make an existing static page (e.g. /faq) the home page. But all I get is the 404 page. I’m really lost and kind of frustrated - right now it’s horrible to write plugins (no debugging, hardly any documentation,…and no experience with rb on my side).
So maybe someone can point me in the right direction. Thanks!

(Love Chopra ) #10

Hi @bunjee,
Indeed a greatly explained step by step approach for creating a new pages in discourse, but I am facing a problem with this your approach, in my development environment everything works fine but when I deploy my changes to production environment using official docker installation method (./launcher rebuild app) I am getting following error:

RuntimeError: cd /var/www/discourse && su discourse -c 'bundle exec rake assets:precompile' failed with return #<Process::Status: pid 434 exit 1>
Location of failure: /pups/lib/pups/exec_command.rb:105:in `spawn'
exec failed with the params {"cd"=>"$home", "hook"=>"bundle_exec", "cmd"=>["su discourse -c 'bundle install --deployment --verbose --without test --without development'", "su discourse -c 'bundle exec rake db:migrate'", "su discourse -c 'bundle exec rake assets:precompile'"]}

(bunjee) #11

Hi @lovechopra1,

Actually I haven’t switched to docker yet.

I’m using Bitnami images found here: Discourse Cloud Hosting, Discourse Installer, Docker Container and VM.

Maybe @eviltrout, @awesomerobot or @riking have a clue about this.


(Love Chopra ) #12

Hey @bunjee,
I have successfully made the pages as per your suggested approach, but the problem is when I look at view-source of the page then main outlet looks empty although pages are rendering fine on browser

  <div id="main-outlet" class="container">

Can you please tell me what could be the issue here?

(Kane York) #13

Check the JS console.

(bunjee) #14

Not sure what happens here.

I would advice checking the console / switching browsers.

(Mike Mayer) #15

can you elaborate on what I should be looking for in the JS console?

(Mike Mayer) #16

I have run up against the same problem, can you elaborate? did you find a solution?

(Kane York) #17

Errors and stuff! Because that’s pretty much all that should be there :stuck_out_tongue:

(Sunny Butani) #18


Where to find this folder ?


I’m having this exact problem. A blank page only showing ‘empty’. I have the endpoint in the plugin and the routes can be accessed via the link-tos when navigating in discourse but as soon as I try to reach the route via the url I get this problem. What am I missing?

(Aman Jagga) #20

I need the same feature, but thinking of doing it through a plugin so everyone can use.Can someone tell me what extra changes will be there do it through plugin? I am new to Ember and rails and I understood this logic as well.

Just few doubts like what would be the folder structure for my plugin? Does the folder structure matters? Do I have to inport some dependencies etc etc.
Tagging few members who are involved in this
@bunjee @eviltrout