Static Site Generator Plugin

Thanks for your reply.
Yes I tried with MAMP and an online php server and I still have the same JSON error…

I tried running that plugin on a site im bulding, using the most recent version of kirby. It does nothing when i hit the button. Not sure if the plugin is compatable with the most recent Kirby although it does say kirby 3./4.

You might be better off using Kirby headless though. You will end up with a static site if you feed the data into 11ty or Nuxt or Jigsaw or whatever else.

That’s what I thought, that the plugin wasn’t compatible with the latest versions of Kirby, thanks a lot for the test!
So I’ll try to use Kirby headless and 11ty then. Thanks for the suggestion!

I actually wrote a couple of cookbook guides on this…

You dont have to go all the way the second one… but it does involve 11ty

1 Like

@srhgrcn @jimbobrjames Claiming that something isn’t compatible seems only logical given that the code wasn’t maintained for a few months and you tried out one of the three ways to generate a site… What would have been more helpful though: either

  • posting the actual output of the generate-static-site endpoint from the network tab (it is not SyntaxError : JSON.parse because that is JavaScript and the endpoint is PHP)
  • trying out one of the other methods to generate the static site
  • Running the static site generation only for specific pages rather than for your whole site first to see if there are any errors (chances are high that one of your own pages cannot be rendered because there is an error there in one of them)

Anyways, I’m always testing the plugin against the latest kirby starterkit, which I’m going to do after writing this comment, to make sure there is no new incompatibility

As I already thought, everything works just fine with the static site generator plugin when testing with the starterkit.

Kirby-headless is an entirely different approach than rendering static pages. I’m also running a few sites with KQL, there’s nothing wrong about it, but it’s different things for different use cases. Either you need static pages for the performance and security benefits or hosting requirements or you don’t. Going through 11ty or nuxt to achieve that is an option of course.

Really sorry indeed I claim too quickly, and went too fast in the tests…
I’ll be careful about that next time…
Thank you very much for your answer and your help
I finally found the problem, a php function that was bugging the plugin.
and now everything works!

@srhgrcn Can you describe the problem here, or would that be too extensive? Every bug report is not only helpful for @jonathan-reisdorf as a developer, but also for other users who have similar problems and are looking for a solution here in the forum :+1:

Yes of course, this is the php function here’s the php function that was causing the static site generation to crash. I’ve just removed it for now, but I don’t know exactly what was wrong. There must be an error in my code…

<?php function generateAnchorMenu($blocks) {
    $menu = '<ul class="anchor-menu">';

    foreach ($blocks as $block) {
        if ($block->level() == 'h2') {
            $headingText = $block->text();
            $anchorId = Str::slug($headingText);
            $menu .= '<li><a href="#' . $anchorId . '">' . $headingText . '</a></li>';
        }
    }
    $menu .= '</ul>';
    return $menu;
}?>

Thank you :+1:
The level of headings (like h2, h3, etc.) is usually represented as a number, not as a string. So you should adjust the comparison to == 2 instead of == 'h2'.

<?php 
function generateAnchorMenu($blocks) {
    $menu = '<ul class="anchor-menu">';

    foreach ($blocks as $block) {
        if ($block->level() == 2) { // Correction: Compare level as a number
            $headingText = $block->text();
            $anchorId = Str::slug($headingText);
            $menu .= '<li><a href="#' . $anchorId . '">' . $headingText . '</a></li>';
        }
    }
    $menu .= '</ul>';
    return $menu;
}
?>
1 Like

Great that you found the source of the issue, thanks for sharing it! I guess what I should take away is that the plugin’s error debugging needs a bit of improvement (there is a try-catch in place, but not for the homepage nor for custom routes, and not all kinds of errors are caught). The error shared above would typically mean that an uncaught error has occured, otherwise one would see the exact error message and the file + line in which the error occurred.

@srhgrcn As for the source of your issue (aside from the level() thing), after trying to reproduce I am thinking it’s rather:
Cannot redeclare generateAnchorMenu()

The static site generator is different from the normal way of running Kirby, because multiple pages (or even the same page in multiple languages) get rendered in a row. That can cause issues you normally don’t have because when using Kirby without the static site generator, PHP starts, renders one page, and then exits.

You can wrap

if (!function_exists('generateAnchorMenu')) {
}

around your function declaration to avoid this, or use require_once and put that code into a separate file.

But I’m now also working on a better way to catch these errors which will hopefully result in a new release of the plugin soon.

New static site generator release: 2.0.2

This would now display more meaningful errors when using the field, including showing fatal errors that happen during page rendering. Feedback welcome :slight_smile:

1 Like

Many thanks it works !!

1 Like