Advice for Hosting a Front-End on Heroku
...don't. Use surge instead. Seriously, surge is the absolutely easiest front-end publishing method I've ever seen*. Here's how to use it:
1. Download surge by opening up your terminal, typing "npm install --global surge" and pressing enter.
2. Navigate to the directory with all your front-end code and run "npm build". This will create a build sub-folder within your directory.
3. Navigate into that directory ("cd build") and....
Type "surge" and enter.
That's it. If you just keep pressing enter your site will go to the web.
Several cool things about this:
A. There's a nice, neat division between your production code and your code that you filled full of bugs trying to get one simple, easy new button into. You can work all you want on your laptop with your buggy code and not mess up your running-just-fine website. Then when you do figure out your bugs, you can simply run npm build again and publish again to surge to the same domain. No git headaches trying to manage your various versions and making sure you push your code to production without blowing something up - just get it running on your home machine and put the new version up.
B. You can run a couple different versions of your website. Want to test out your new code - on the web! - without blowing up your old production code? Just push your new version up on one of surge's random name urls and view it. Then when it looks like what you want it to look like, push it to your real url.
C. Time to publishing is literally seconds. So whatever you want to do, whatever versions, bugs, layouts, etc. you want to put up can be done with no muss and no fuss. Extreme dexterity here, folks.
D. It's free. Nice.
Advanced users:
4. Surge automatically picks a url for you seemingly based off of this password generation scheme popularized by xkcd. If you happen to want to pick your website's name, you simply enter in a different one when the "domain:" prompt comes up in.
All of the domains will still contain the "surge.sh" top-level domain, however. If you want to publish to a completely-your-name domain, it's still fairly easy.
1. Buy a domain (this can be easy, but it can also be an endless rabbit hole of weighing the pros, cons, and prices of various domain hosting services. Good luck, can't help you with that - but I use dreamhost, which is a bit pricier than most but comes in-built with a fair amount of security features. You don't want your silly homegrown project hijacked by nefarious dark web hackers, do you?!?!)
2. Set your CNAME record to na-west1.surge.sh. This maps your hosted site to surge's hosting and makes sure that your domain gets your program from surge. How to do this can vary based on what your domain provider is, so check out surge's instructions for your particular service.
3. Type "surge your-website-name.com" instead of just "surge" from your project's build directory.
4. That's it. Woo!
After all that, if you don't just have a static site, you may still want a back-end though. So if you do end up using Heroku for that, and your backend is Ruby on Rails, I got some advice for you:
1. In your /config/applications.rb file, check out your middleware configuration. It should look something like this:
Importantly, in Ruby on Rails 5 or 6 (i.e., the latest versions), putting quotes around Rack::Cors will wreck everything. Most of your online tutorials you might haphazardly google into do not mention this, because before these versions you needed quotes - "Rack::Cors". If you're having difficulties, you can figure out which version of Rails you have by entering "rails -v" in your terminal.
2. Use a Postgres database. That's what they want you to use, so use it. Ugh.
3. Don't use Heroku. Figure out AWS, or Azure, or google one of the thousand other solutions. aALKHFSDGLSHGHEROKUARGGGHHH
*Note: this advice applies to front ends using JavaScript and React. I believe surge works for other stacks as well, but just want to make sure I'm not caught with my pants down.
Comments
Post a Comment