Code Monkey home page Code Monkey logo

templatus-hotwire's Introduction

Build Status Cypress

Templatus (Hotwire edition)

Templatus is an opinionated template to build web applications with Ruby on Rails and Hotwire. It simplifies the process of setting up a new application while following best practices.

Live demo available at https://templatus-hotwire.ledermann.dev

Similar projects

There are two sister repositories:

Features / Technology stack

Backend

Frontend

  • Hotwire for building the frontend without using much JavaScript by sending HTML instead of JSON over the wire
  • TypeScript for static type checking in Stimulus controllers and other script code
  • ViewComponent for creating reusable, testable & encapsulated view components
  • Slim for writing templates instead of ERB
  • Tailwind CSS 3 to not have to write CSS at all
  • Heroicons for beautiful hand-crafted SVG icons
  • Vite for bundling JavaScript and CSS with Hot Module Replacement (HMR) in development

Development

  • Puma-dev for using .test-domain and HTTPS in development
  • Overmind for starting up the application locally (Procfile handling like Foreman)
  • dotenv to load environment variables from .env into ENV
  • Prettier for auto-formatting JavaScript and Ruby code in Visual Studio Code
  • Lookbook as development UI for ViewComponent
  • annotate for annotating models and routes
  • Live reloading

Linting and testing

  • RuboCop for Ruby static code analysis
  • ESLint for JavaScript static code analysis
  • RSpec for Ruby testing
  • Factory Bot for setting up Ruby objects as test data
  • Cypress for E2E testing

Deployment

  • Docker for production deployment, NOT for development
  • DockerRailsBase for fast building an optimized Docker image based on Alpine Linux
  • GitHub Actions for testing, linting, and building Docker image
  • Dependabot configuration for updating dependencies (with auto-merge)
  • Ready for serving assets via CDN like CloudFront
  • Honeybadger for error tracking in Ruby and JavaScript
  • RorVsWild for performance monitoring
  • Plausible for privacy friendly analytics
  • Lockup to place a staging server behind a basic codeword

Production

  • Lograge for single-line logging
  • Gzip compression of dynamic responses (HTML, JSON) using Rack::Deflater
  • Fine-tuned Content Security Policy (CSP)
  • Ready for PWA (manifest, service-worker)

Metrics

This template is developed with optimized performance and security in mind. The following benchmarks are performed against the demo installation on production. It uses an inexpensive virtual server on the Hetzner Cloud behind a Traefik setup.

Lighthouse site performance

100% in all categories.

Lighthouse

Security headers

Security headers

What's the red Permissions-Policy badge? This seems to be fixed with one of the next Rails update: rails/rails#41994

Mozilla Observatory

Mozilla Observatory

WebPageTest

WebPageTest

GTmetrix

GTmetrix

Check-your-website

Check-your-website

JavaScript size

159 KB of compiled JavaScript (minified, uncompressed). The largest parts are:

  • Turbo with ActionCable (72 KB)
  • Stimulus (32 KB)
  • Honeybadger (25 KB)
$ RAILS_ENV=production bin/rails assets:precompile
➤ YN0000: · Yarn 4.1.0
➤ YN0000: ┌ Resolution step
➤ YN0000: └ Completed
➤ YN0000: ┌ Fetch step
➤ YN0000: └ Completed
➤ YN0000: ┌ Link step
➤ YN0000: └ Completed
➤ YN0000: · Done in 0s 312ms
Building with Vite ⚡️
vite v5.1.1 building for production...
transforming...
✓ 47 modules transformed.
rendering chunks...
computing gzip size...
../../public/vite/.vite/manifest-assets.json         0.10 kB │ gzip:  0.09 kB
../../public/vite/assets/logo-DdqaqAN0.svg           0.50 kB │ gzip:  0.30 kB
../../public/vite/.vite/manifest.json                0.79 kB │ gzip:  0.29 kB
../../public/vite/assets/application-CSF8MhIg.css   23.42 kB │ gzip:  5.03 kB
../../public/vite/assets/application-BzmKZiV0.js     3.65 kB │ gzip:  1.44 kB │ map:   9.85 kB
../../public/vite/assets/index-BAb4QarR.js           9.52 kB │ gzip:  3.04 kB │ map:  31.50 kB
../../public/vite/assets/vendor-BmMgSTZz.js        198.82 kB │ gzip: 54.94 kB │ map: 680.86 kB
✓ built in 750ms
Build with Vite complete: /Users/ledermann/Projects/templatus-hotwire/public/vite

Network transfer

Small footprint: The demo application transfers only 62 KB of (compressed) data on the first visit.

Network

Docker build time

With multi-stage building and using DockerRailsBase the build of the Docker image takes very little time. Currently, the build job requires about 1,5 minutes on GitHub Actions (see https://github.com/templatus/templatus-hotwire/actions)

Docker image size

The Docker image is based on Alpine Linux and is optimized for minimal size (currently 117 MB uncompressed disk size). It includes just the bare minimum - no build tools like Node.js, no JS sources (just the compiled assets), no tests.

$ container-diff analyze ghcr.io/templatus/templatus-hotwire -n

-----Size-----

Analysis for ghcr.io/templatus/templatus-hotwire:
IMAGE                                      DIGEST       SIZE
ghcr.io/templatus/templatus-hotwire        sha256:... 116.7M

Getting started

Install for development

  1. Clone the repo locally:
git clone [email protected]:templatus/templatus-hotwire.git
cd templatus-hotwire
  1. Install PostgreSQL, Redis, and puma-dev (if not already present). On a Mac with HomeBrew, run this to install from the Brewfile:
brew bundle
  1. Install and set up puma-dev to use HTTPS for development. Do this on macOS:
sudo puma-dev -setup
puma-dev -install
puma-dev link

# Use Vite via puma-dev proxy
# Adopted from https://github.com/puma/puma-dev#webpack-dev-server
echo 3036 > ~/.puma-dev/vite.templatus-hotwire
  1. Setup the application to install gems and NPM packages and create the database:
bin/setup
  1. Start the application locally:
bin/dev

Then open https://templatus-hotwire.test in your browser.

Preview components in LookBook

bin/dev

Then open https://templatus-hotwire.test/lookbook/ in your browser.

Running linters

RuboCop:

bin/rubocop

ESLint:

bin/yarn lint

Running tests locally

Ruby tests:

bin/rspec
open coverage/index.html

JavaScript unit tests:

bin/yarn test

E2E tests with Cypress:

bin/cypress open

This opens Cypress and starts Rails in development environment, but with CYPRESS=true, so the test database is used. This allows code editing without class reloading and recompiling assets.

To run Cypress in headless mode:

bin/cypress run

Test deployment locally

docker network create public
docker compose up

templatus-hotwire's People

Contributors

dependabot[bot] avatar gaizkaeu avatar ledermann avatar olegchuev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

templatus-hotwire's Issues

Blocked host: templatus-hotwire.test

Thanks for sharing your great work as open source! 🚀

Carefully following the README setup guide on MacOS, this occurs after step 5:

Following your setup tutorial Blocked host: templatus-hotwire.test
To allow requests to templatus-hotwire.test make sure it is a valid hostname (containing only numbers, letters, dashes and dots), then add the following to your environment configuration:

config.hosts << "templatus-hotwire.test"
For more details view: the Host Authorization guide

In .env, changing APP_HOST=templatus-hotwire.example.com to APP_HOST=templatus-hotwire.test, and running bin/setup again fixes this.

But it doesn't seem this is the intended way things should work for the development environment? Perhaps there needs to be a .env.dev, or the README needs updating for clarity.

Licence

Hi.

This template is nice & neat. Would you mind adding a licence?

  • Xander

Almost all assets are served over http 1 not http2 on local

Hello Georg,

I've setup the project as described in the README both locally using puma-dev and on docker, however my assets still get served over http 1. I opened your demo app, and I notice it gets served over the h2 protocol. I am new to rails in general but I was just wondering of we need a production deployment to enable h2 protocol or am I missing something?
Screen Shot 2022-01-10 at 6 30 49 PM

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.