• Stars
    star
    190
  • Rank 203,739 (Top 5 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 5 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Render a Rack app (Rails/Sinatra/etc) to a static build so it can be served by Netlify, Now, GitHub Pages, S3, or any other web server.

Parklife

GitHub Actions status

Parklife is a Ruby library to render a Rack app (Rails/Sinatra/etc) to a static site so it can be served by Netlify, Now, GitHub Pages, S3, or another static server.

Getting started

Add Parklife to your application's Gemfile and run bundle install.

gem 'parklife'

Now generate a Parkfile configuration file and build script. Include some Rails- or Sinatra-specific settings by passing --rails or --sinatra, create a GitHub Actions workflow to generate your Parklife build and push it to GitHub Pages by passing --github-pages.

$ bundle exec parklife init

How to use Parklife with Rails

Parklife is configured with a file called Parkfile in the root of your project, here's an example Parkfile for an imaginary Rails app:

# Load Parklife's Rails-specific integration which, among other things, allows
# you to use URL helpers within the `routes` block below.
require 'parklife/rails'

# Load the Rails application, this gives you full access to the application's
# environment from this file - using models for example.
require_relative 'config/environment'

Parkfile.application.routes do
  # Start from the homepage and crawl all links.
  root crawl: true

  # Some extra paths that aren't discovered while crawling.
  get feed_path(format: :atom)
  get sitemap_path(format: :xml)

  # A couple more hidden pages.
  get easter_egg_path, crawl: true

  # Services typically allow a custom 404 page.
  get '404.html'
end

Listing the routes included in the above Parklife application with parklife routes would output the following:

$ bundle exec parklife routes
/             crawl=true
/feed.atom
/sitemap.xml
/easter_egg   crawl=true
/404.html

Now you can run parklife build which will fetch all the routes and save them to the build directory ready to be served as a static site. Inspecting the build directory might look like this:

$ find build -type f
build/404.html
build/about/index.html
build/blog/index.html
build/blog/2019/03/07/developers-developers-developers/index.html
build/blog/2019/04/21/modern-life-is-rubbish/index.html
build/blog/2019/05/15/introducing-parklife/index.html
build/easter_egg/index.html
build/easter_egg/surprise/index.html
build/index.html
build/location/index.html
build/feed.atom
build/sitemap.xml

Parklife doesn't know about assets (images, CSS, etc) so you likely also need to generate those and copy them to the build directory, see the Rails example's full build script for how you might do this.

More examples

Take a look at the Rails, Rack and Sinatra working examples within this repository.

Configuration

Linking to full URLs

Sometimes you need to point to a link's full URL - maybe for a feed or a social tag URL. You can tell Parklife to make its mock requests with a particular protocol / host by setting its base so Rails *_url helpers will point to the correct host:

Parklife.application.config.base = 'https://foo.example.com'

The base URL can also be passed at build-time which will override the Parkfile setting:

$ bundle exec parklife build --base https://benpickles.github.io/parklife

Dealing with trailing slashes (turning off nested index.html)

By default Parklife stores files in an index.html file nested in directory with the same name as the path - so the route /my/nested/route is stored in /my/nested/route/index.html. This is to make sure links within the app work without modification making it easier for any static server to host the build.

However, it's possible to turn this off so that /my/nested/route is stored in /my/nested/route.html. This allows you to serve trailing slash-less URLs with GitHub Pages or with Netlify by using their Pretty URLs feature or with some custom nginx config.

Parklife.application.config.nested_index = false

Changing the build output directory

The build directory shouldn't exist and is destroyed and recreated before each build. Defaults to build.

Parklife.application.config.build_dir = 'my/build/dir'

Handling a 404

By default if Parklife encounters a 404 response when fetching a route it will raise an exception (the :error setting) and stop the build. Other values are:

  • :warn - output a message to stderr, save the response, and continue processing.
  • :skip - silently ignore and not save the response, and continue processing.
Parklife.application.config.on_404 = :warn

Setting the Rack app

If you're not using the Rails configuration you'll need to define this yourself, see the examples.

Parklife.application.config.app

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Parklife project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

More Repositories

1

peity

Progressive <svg> pie, donut, bar and line charts
HTML
4,219
star
2

js-model

Work with models in your JavaScript
JavaScript
403
star
3

onScreen

Custom jQuery selector to detect elements currently on-screen
HTML
87
star
4

screamshot

A synchronous HTTP screenshot service for headless Chrome.
Ruby
33
star
5

operatic

A minimal standard interface for your Ruby operations.
Ruby
22
star
6

medium

Medium API Ruby Client
Ruby
22
star
7

aesthetic

Regression-test your site and ensure that tiny CSS changes don't have unintentional side effects
Ruby
11
star
8

fewer

Fewer is a Rack endpoint to bundle and cache assets and help you make fewer HTTP requests.
Ruby
8
star
9

js-model-rails

Work with models in your JavaScript in your Rails
JavaScript
6
star
10

phlex-sinatra

A Phlex adapter for Sinatra
Ruby
6
star
11

clocks

Clock experiments
JavaScript
5
star
12

wapper

Wapper (wapər) - wrap selected text in other elements
JavaScript
3
star
13

contentz

Contentz (pronounced "content zee") is an ActiveRecord extension that uses Zlib to compress/uncompress attribute data on write/read.
Ruby
3
star
14

snipplet

A customisable bookmarklet router based on shortwaveapp.com and written in Rails.
Ruby
2
star
15

template

Template objects that are jQuery objects that are template objects that are...
JavaScript
2
star
16

dragonfly-s3-server

Isolate Dragonfly processing and serving of S3 assets from your main app.
Ruby
2
star
17

all_your

A little experiment to see how many characters can be used in a link shortener's path.
Ruby
1
star
18

v

A Vim command line wrapper
Ruby
1
star
19

benpickles.github.com

JavaScript
1
star
20

ladder-native

JavaScript
1
star