Static Site Generator Plugin

:wave: With this plugin you can create a directory with assets, media and static html files generated from your pages, which you then could e.g. simply upload to a CDN.

This is from the functionality somewhat similiar to @fvsch 's StaticBuilder plugin, but for Kirby 3.
There might be still smaller quirks to iron out (e.g. sometimes unnecessary media file version creation while no change to the file was made), so I am happy for feedback.

The company I work for uses the plugin when triggering the deployment to production (CMS environment serves kirby and the panel while production just serves the plain html version).

8 Likes

Hello jonathan, in the panel of kirby 3.22, I can’t find the button that shows “Click the button to generate a static version of the website.”
Once I put the plugin into site/plugins/static-site-generator, I will see this build button, isn’t it, or I am missing some steps?

No, you have to add a field to one of your blueprint and there’s also a config setting you have to set, see the steps in the readme.

1 Like

Hello Jonathan,

great plugin so far. Thats almost what I searched for. Do you think, that there is a simple way of making all urls relative?

This is so cool! I finally added it to the plugin directory. Sorry that it took so long.

2 Likes

After submitting the build button, why do I always get the prompt “mkdir(): No such file or directory”?
Kirby v3.2.5
My config.php

'd4l' => [
        'static_site_generator' => [
            'endpoint' => 'kirby-ssr',
            'output_folder' => './static',
            'preserve' => [],
            'base_url' => '/static/',
            'skip_media' => false,
            'skip_templates' => []
        ]

I can generate an HTML file but I am missing a resource file.
I have to manually copy the assets folder to the build directory.

Thanks for adding it to the plugin directory @bastianallgeier ! :slight_smile:

@taoguangc The config you posted works fine for me (tested with starterkit + kirby 3.2.5) - do you have a setup with symlinks? Or is there maybe an issue writing (to) the output folder? (maybe one of the folders within the output folder belongs to another user?) And are you using the latest version of the plugin?

In any case it would be great (unless the issue is already solved) if you could create this as an issue in the github repo so that we have a central place where bugs are collected, thanks!

Thank you @jonathan-reisdorf ! I don’t know coding very much. so I describe my setup process in detail.

  1. I run XAMPP in Windows environment, PHP 7.2.
  2. I downloaded the latest starterkit package and placed it in the root directory.
  3. modified site\blueprints\site.yml, add
...
- width: 1/2
     Fields:
       staticSiteGenerator:
         Label: Generate static stie
         Help: Click the button to generate a static version of the website.
         Progress: Please wait...
         Success: Static site successfully generated
         Error: error
  1. modify site\config\config.php,
return [
	'd4l' => [
        'static_site_generator' => [
            'endpoint' => 'kirby-ssr',
            'output_folder' => './static',
            'preserve' => [],
            'base_url' => '/static/',
            'skip_media' => false,
            'skip_templates' => []
        ]
    ],
    'debug' => true,
];

Windows! I never tested in Windows. I’ll do that one of the next days. I assume the path resolving is not working there (/ vs. \ or sth. the like)

@bastianallgeier DS is set to '/' in https://github.com/getkirby/kirby/blob/master/config/setup.php#L6 which is probably perfect for most of us, but for Windows users wouldn’t

define('DS', DIRECTORY_SEPARATOR);

be better?
But if we don’t want to break existing kirby sites which might rely on DS always being /, I can also convert all DS to DIRECTORY_SEPARATOR in the plugin code.

@taoguangc I use DS a lot in the code for path resolving, which is why the plugin breaks for you when copying the assets.

@taoguangc a PR to solve the issue has been created: https://github.com/d4l-data4life/kirby3-static-site-generator/pull/22

Thanks for the reply, great job! It works fine with Windows.

I managed to get this plugin working locally.
However I can’t work out the line with ‘base_url’.
I want it to create the line

link href=“assets/css/extra.css”

and it creates

link href="/assets/css/extra.css"

Does somebody know how to do that?

Best,

Nedim

hello there,

i managed to install the plugin but whenever i click the “generate static html” button i get an error stating: “The JSON response from the API could not be parsed. Please check your API connection.”
i did’t customize the API or anything so i’m not sure where to look for the solution of this problem.

my config is setup like this:

        'd4l' => [
        'static_site_generator' => [
          'endpoint' => 'generate-static-site', # set to any string like 'generate-static-site' to use the built-in endpoint (necessary when using the blueprint field)
          'output_folder' => './static', # you can specify an absolute or relative path
          'preserve' => [], # preserve individual files / folders in the root level of the output folder (anything starting with "." is always preserved)
          'base_url' => '/', # if the static site is not mounted to the root folder of your domain, change accordingly here
          'skip_media' => false, # set to true to skip copying media files, e.g. when they are already on a CDN; combinable with 'preserve' => ['media']
          'skip_templates' => [] # ignore pages with given templates (home is always rendered)
        ],
      ],

if something screams “error!” to you please help out.

thanks

Is debugging turned on? If not, please enable it in your config and then check your browser console for more details.

Make sure your config.php has only a single return array.

yes debug is set to true and i don’t have multiple return arrays. The only other things in my config are some custom routes as well as the panel install.

Hi antonlinus :wave:

As this is just a generic error which says that anything else than a json has been returned, could you please have a look into your network tab and post the output of the generate-static-site call here?

Thanks :slight_smile:

do you mean this?

{“status”:“error”,“message”:“No route found for path: “generate-static-site” and request method: “GET””,“code”:404,“exception”:“Exception”,“key”:null,“file”:"/kirby/src/Http/Router.php",“line”:153,“details”:[],“route”:null}

Yes, thanks. Hm, this is weird. The plugin does not attempt to make a GET call but a POST call, which is why you get this error when making a GET call instead. I’ll see if I can duplicate the issue. The more contextual information I have the better, e.g. what OS do you use, can you post your full config.php, how did you install the plugin (manually / composer), how does the blueprint look like where you’ve added the field etc.

i’m using mac os, i installed the plugin manually by downloading the zip from github, unziping it, renaming the folder “static-site-generator” and moving it to the plugins folder of my kirby installation. I’m testing everything locally atm by running a php server via terminal.

this is my full config.php:

<?php

return [
  'debug'  => true,
  'routes' => [
  	[
  		'pattern' => 'kategorien/(:any)',
  		'action' => function ($tag) {
  			if ($page = page('kategorien/' . $tag)) {
  				return $page;
  			} else {
  				return page('inserate')->render([
  					'tag' => $tag
  				]);
  			}
  		}
  	],
  ],
  'panel' =>[
    'install' => true
  ],
  'd4l' => [
    'static_site_generator' => [
      'endpoint' => 'generate-static-site', # set to any string like 'generate-static-site' to use the built-in endpoint (necessary when using the blueprint field)
      'output_folder' => './static', # you can specify an absolute or relative path
      'preserve' => [], # preserve individual files / folders in the root level of the output folder (anything starting with "." is always preserved)
      'base_url' => '/', # if the static site is not mounted to the root folder of your domain, change accordingly here
      'skip_media' => false, # set to true to skip copying media files, e.g. when they are already on a CDN; combinable with 'preserve' => ['media']
      'skip_templates' => [] # ignore pages with given templates (home is always rendered)
    ],
  ],
];

this is my sites blueprint:

title: Home
fields:
  staticSiteGenerator:
    label: generate static html
  Bilder:
    label: Bilder
    type: files