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).

9 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