• Stars
    star
    223
  • Rank 172,136 (Top 4 %)
  • Language
    PHP
  • Created over 4 years ago
  • Updated about 2 months ago

Reviews

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

Repository Details

The official Statamic Static Site Generator

Statamic Static Site Generator

Generate static sites with Statamic 3.

Statamic 3.0+

Installation

Install the package using Composer:

composer require statamic/ssg

If you want or need to customize the way the site is generated, you can do so by publishing and modifying the config file with the following command:

php artisan vendor:publish --provider="Statamic\StaticSite\ServiceProvider"

The config file will be in config/statamic/ssg.php. This is optional and you can do it anytime.

Usage

Run the following command:

php please ssg:generate

Your site will be generated into a directory which you can deploy however you like. See Deployment Examples below for inspiration.

Multiple Workers

For improved performance, you may spread the page generation across multiple workers. This requires Spatie's Fork package. Then you may specify how many workers are to be used. You can use as many workers as you have CPU cores.

composer require spatie/fork
php please ssg:generate --workers=4

Routes

Routes will not automatically be generated. You can add any additional URLs you wish to be generated by adding them to the urls array in the config file.

'urls' => [
    '/this-route',
    '/that-route',
],

You can also exclude single routes, or route groups with wildcards. This will override anything in the urls config.

'exclude' => [
    '/secret-page',
    '/cheat-codes/*',
],

Dynamically adding routes

You may add URLs dynamically by providing a closure that returns an array to the addUrls method.

use Statamic\StaticSite\SSG;

class AppServiceProvider extends Provider
{
    public function boot()
    {
        SSG::addUrls(function () {
            return ['/one', '/two'];
        });
    }
}

Post-generation callback

You may optionally define extra steps to be executed after the site has been generated.

use Statamic\StaticSite\SSG;

class AppServiceProvider extends Provider
{
    public function boot()
    {
        SSG::after(function () {
            // eg. copy directory to some server
        });
    }
}

Glide Images

The default configuration of Statamic is to have Glide use "dynamic" images, which means that the glide tag will only output URLs. The images themselves will be generated when the URLs are visited. For a static site, this no longer makes sense since it will typically be deployed somewhere where there is no dynamic Glide route available.

By default, the SSG will automatically reconfigure Glide to generate images into the img directory whenever glide tags are used. This is essentially Glide's custom static path option.

You can customize where the images will be generated:

'glide' => [
    'directory' => 'images',
],

If you are using a custom glide disk, you can tell the SSG to leave it alone:

'glide' => [
    'override' => false,
],

And then copy the images over (or create a symlink) after generating has completed:

SSG::after(function () {
    $from = public_path('img');
    $to = config('statamic.ssg.destination').'/img';

    app('files')->copyDirectory($from, $to);
    // or
    app('files')->link($from, $to);
});

Triggering Command Failures

If you are using the SSG in a CI environment, you may want to prevent the command from succeeding if any pages aren't generated (e.g. to prevent deployment of an incomplete site).

By default, the command will finish and exit with a success code even if there were un-generated pages. You can tell configure the SSG to fail early on errors, or even on warnings.

'failures' => 'errors', // or 'warnings'

Deployment Examples

These examples assume your workflow will be to author content locally and not using the control panel in production.

Deploy to Netlify

Deployments are triggered by committing to Git and pushing to GitHub.

  • Create a site in your Netlify account
  • Link the site to your desired GitHub repository
  • Add build command php please ssg:generate (if you need to compile css/js, be sure to add that command too and execute it before generating the static site folder. e.g. npm install && npm run build && php please ssg:generate).
  • Set publish directory storage/app/static

After your site has an APP_URL...

  • Set it as an environment variable. Add APP_URL https://thats-numberwang-47392.netlify.com

Finally, generate an APP_KEY to your .env file locally using php artisan key:generate and copy it's value, then...

  • Set it as an environment variable. Add APP_KEY [your app key value]

S3 Asset Containers

If you are storing your assets in an S3 bucket, the .envs used will need to be different to the defaults that come with Laravel, as they are reserved by Netlify. For example, you can amend them to the following:

# .env
AWS_S3_ACCESS_KEY_ID=
AWS_S3_SECRET_ACCESS_KEY=
AWS_S3_DEFAULT_REGION=
AWS_S3_BUCKET=
AWS_URL=

Be sure to also update these in your s3 disk configuration:

// config/filesystems.php
's3' => [
    'driver' => 's3',
    'key' => env('AWS_S3_ACCESS_KEY_ID'),
    'secret' => env('AWS_S3_SECRET_ACCESS_KEY'),
    'region' => env('AWS_S3_DEFAULT_REGION'),
    'bucket' => env('AWS_S3_BUCKET'),
    'url' => env('AWS_URL'),
],

Deploy to Vercel

Deployments are triggered by committing to Git and pushing to GitHub.

  • Create a new file called ./build.sh and paste the code snippet below.
  • Run chmod +x build.sh on your terminal to make sure the file can be executed when deploying.
  • Import a new site in your Vercel account
  • Link the site to your desired GitHub repository
  • Add build command ./build.sh
  • Set output directory to storage/app/static
  • Add environment variable in your project settings: APP_KEY <copy & paste from dev>

Code for build.sh

Add the following snippet to build.sh file to install PHP, Composer, and run the ssg:generate command:

#!/bin/sh

# Install PHP & WGET
yum install -y amazon-linux-extras
amazon-linux-extras enable php7.4
yum clean metadata
yum install php php-{common,curl,mbstring,gd,gettext,bcmath,json,xml,fpm,intl,zip,imap}
yum install wget

# INSTALL COMPOSER
EXPECTED_CHECKSUM="$(wget -q -O - https://composer.github.io/installer.sig)"
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"

if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]
then
    >&2 echo 'ERROR: Invalid installer checksum'
    rm composer-setup.php
    exit 1
fi

php composer-setup.php --quiet
rm composer-setup.php

# INSTALL COMPOSER DEPENDENCIES
php composer.phar install

# GENERATE APP KEY
php artisan key:generate

# BUILD STATIC SITE
php please ssg:generate

Deploy to Surge

Prerequisite: Install with npm install --global surge. Your first deployment will involve creating an account via command line.

  • Build with command php please ssg:generate
  • Deploy with surge storage/app/static

Deploy to Firebase hosting

Prerequisite: Follow the instructions to get started with Firebase hosting

  • Once hosting is set up, make sure the public config in your firebase.json is set to storage/app/static
  • (Optionally) Add a predeploy config to run php please ssg:generate
  • Run firebase deploy

More Repositories

1

cms

The core Laravel CMS Composer package
PHP
3,319
star
2

statamic

Statamic: The New Site/App Package
PHP
684
star
3

awesome

A collection of awesome Statamic articles, links, resources and other rad things.
159
star
4

starter-kit-cool-writings

Statamic Starter Kit: Cool Writings
Antlers
109
star
5

docs

Statamic Documentation
PHP
100
star
6

eloquent-driver

A package that allows you to store Statamic entries in a database.
PHP
100
star
7

spock

Automatically perform git commits, pushes, and other cli actions when Statamic content changes
PHP
95
star
8

v2-hub

Statamic 2 - Feature Requests and Bug Reports
95
star
9

starter-kit-starters-creek

Statamic Starter Kit: Starter's Creek
CSS
84
star
10

cli

Install and manage your Statamic v3 projects from the command line.
PHP
69
star
11

v2-addons

Addons for Statamic v2
59
star
12

v2-docs

Statamic v2 Documentation
55
star
13

seo-pro

An all-in-one site reporting, metadata wrangling, Open Graph managing, Twitter card making, sitemap generating, turn-key addon for Statamic.
PHP
51
star
14

statamic-cheat-sheet

Cheat sheet of all tags and variables available in Statamic.
CSS
50
star
15

Theme-Kindling

Base starting point for building your custom Statamic site or theme.
CSS
44
star
16

wordpress-to-statamic-exporter

One-click WordPress plugin that converts all posts, pages, taxonomies, metadata, and settings to Statamic's JSON import format
PHP
44
star
17

migrator

Migrate from Statamic v2
PHP
43
star
18

starter-kit-doogie-browser

Statamic Starter Kit: Doogie Browser
JavaScript
39
star
19

i18n

Language translations for the Statamic v2 Control Panel
PHP
38
star
20

hosts

A list of web hosts that work well with Statamic right out of the box.
31
star
21

collaboration

Real-time, multi-user editing with Statamic via websockets
JavaScript
30
star
22

ideas

💡Discussions on ideas and feature requests for Statamic
27
star
23

theme-storyteller

Statamic 2 Theme: Storyteller
JavaScript
26
star
24

preset-tailwindcss

DEPRECATED: Statamic 3 starts with TailwindCSS out of the box.
PHP
22
star
25

Check

Statamic's check-file. Checks to see if a server is capable of running Statamic or not.
PHP
20
star
26

Plugin-Image

Statamic Plugin for resizing images in your templates
PHP
19
star
27

Fieldtype-EpicEditor

Statamic Fieldtype for Epic Editor, a fullscreen Markdown editor
CSS
15
star
28

Addon-CP-Theme-Override

Override the CP Theme with a simple css file.
CSS
14
star
29

Plugin-Gist

Statamic Plugin for quick embedding Gists
PHP
13
star
30

Plugin-SimplePie

A fast and easy-to-use RSS and Atom feed parsing plugin for Statamic.
PHP
13
star
31

Plugin-Disqus

Disqus plugin for Statamic
PHP
13
star
32

importers

PHP
12
star
33

custom-publish-forms-eloquent

An example of using Custom Publish forms powered by Eloquent.
HTML
11
star
34

overload

CLI commands to create test and sample content.
PHP
11
star
35

starter-kit-podcaster

Statamic Starter Kit: Podcaster
JavaScript
10
star
36

starter-kit-multisimplicity

A Multilingual focused Starter Kit for Statamic
HTML
10
star
37

github-revisions

PHP
10
star
38

Plugin-Dribbble

Statamic Dribbble plugin to fetch and display content from Dribbble's API, including players, shots, and lists.
PHP
9
star
39

exporter

Export data from Statamic V1
PHP
8
star
40

addon-section-links

Automatically creates a table-of-contents of sections on publish pages.
JavaScript
8
star
41

custom-publish-forms-api

An example of using Custom Publish forms powered by an API.
PHP
8
star
42

pdfcrowd

PHP
7
star
43

eloquent-entries-example

An example package that allows you to store Statamic entries in a database.
PHP
6
star
44

highlight

Highlight content on your page.
PHP
5
star
45

twitter

Twitter example addon for Statamic 3.
JavaScript
5
star
46

changelog-action

JavaScript
5
star
47

Campaign-Monitor-for-Raven

PHP
5
star
48

cp-edit-url

Generate link to go to the edit page in the control panel of currently viewed page.
PHP
4
star
49

definitely-not-v3

It's definitely not v3.
4
star
50

starter-kit-stumblr

Starter Kit: Stumblr – your very own Tumblr
Antlers
4
star
51

Theme-London-Wild

The Original London Wild Theme for Statamic
HTML
3
star
52

starter-kit-link-in-the-bio

Statamic Starter Kit: Link in the Bio
Blade
3
star
53

bardify

Convert Replicator fields to Bard fields
PHP
3
star
54

Theme-Denali

Denali Theme
HTML
3
star
55

Plugin-BaconIpsum

Statamic Plugin for generating delicious bacon ipsum placeholder content.
PHP
3
star
56

workshop

Create and edit entries, pages, and globals on the front-end of your site without the control panel.
PHP
3
star
57

a11y

Discuss and track opportunities to improve Statamic's accessibility
3
star
58

page-tree

JavaScript
2
star
59

Plugin-Snipplr

Statamic Plugin for quick embedding Snipplr snippets
PHP
2
star
60

rainforest

Rainforest is a simple Statamic utility theme to help you manage invoices.
PHP
1
star
61

demo

Statamic Demo Site
PHP
1
star
62

podcast-categories

Statamic Fieldtype to manage iTunes Categories
Vue
1
star
63

stripe

Statamic v2 addon to simplify Stripe Checkout
PHP
1
star
64

another_date

Display a date other than the current date on your Statamic site.
PHP
1
star
65

wikilinks

Autolinks content wrapped in [braces], just like a wiki.
PHP
1
star
66

Bloodhound

A Statamic search add-on.
PHP
1
star
67

.github

1
star
68

content-api-demo

Simple demo of the Statamic Content API
PHP
1
star
69

rucksack

PHP
1
star
70

Raven

Powerful and simple form add-on for Statamic
PHP
1
star
71

test-readme

Will be deleted soon. Pay no heed.
1
star