Jump to content

[SOLVED] ProcessWire as static site generator


tb76
 Share

Recommended Posts

Hi,

I use ProcessWire to generate CMS content for a store that has no CMS functions itself. For this I create static copies of the pages by loading them via WireHTTP from the frontend and pushing them via SFTP to the store server.

So far this always took a few seconds when saving, but was still within the limits.

Now, however, I have a start page with 8 languages. So when saving in ProcessWire, 8 WireHTTP requests would have to be executed. This could possibly take too long when saving.

Is there a way in ProcessWire when saving a page in the backend to have it rendered directly there and save the result without having to load the page from the frontend via WireHTTP? The problem seems to be that you would have to render the pages as if you were a guest user. Is that possible somehow?

Link to comment
Share on other sites

Hi, it seem to me that the issue is the strategy used to generate the static page, but I can be wrong, could you elaborate a bit more ? 

It is required to build the static page on the save page event, and by an user ?

What about, for example, if in the backend you set a template which serve as a sort of configuration and when saving the page, a background process is throwed ? From which type of server the generation of static pages are done ?

 

Link to comment
Share on other sites

<?php
$wire->addHookAfter("Pages::saved", function (HookEvent $event) {
  $page = $event->arguments(0);
  
  // early exits based on your needs
  if($page->template != 'your-static-template') return;
  
  // save some variables for later
  $lang = $this->wire->user->language;
  $user = $this->wire->user;
  
  // create markup for all languages
  // render everything as guest user
  $this->wire->users->setCurrentUser($this->wire->users->getGuestUser());
  foreach ($this->wire->languages as $l) {
    // key for page meta data, eg static-german or static-english
    $key = "static-" . $l->name;
    
    // set temp language
    $this->wire->user->language = $l;
    
    // render page and save it to meta data
    $page->meta($key, $page->render());
  }
  
  // change user back to what it was
  $this->wire->users->setCurrentUser($user);
  $this->wire->user->language = $lang;
});

This would save the static markup in $page->meta("static-default") or $page->meta("static-english") etc...

You can then do whatever you want with that piece of data 🙂 

You could also create files instead of saving everything to the database - depends on your setup which would be better.

  • Like 8
Link to comment
Share on other sites

Thanks to @bernhard’s suggestion, I am now able to switch the language in which the page is rendered and can save the markup of all different languages together in one file.

Is it somehow possible to not only change the language, in which a page is rendered, but to change the user role, so that the rendered page markup would always be the one, a guest user would see?

Link to comment
Share on other sites

15 hours ago, flydev said:

It is required to build the static page on the save page event, and by an user ?

Thanks @flydev,

It would be convenient for the backend user if the static copy of a page would be created as soon as they click "save" in the page editor. But that's really just a matter of habit. One could also move the rendering and storing of the static pages to a backend process, which could be triggered by a cron job, or via an ajax request in an iframe. So that would always be a possibility, but for now I'm still looking for a simpler solution.

Link to comment
Share on other sites

29 minutes ago, bernhard said:

I have updated my code, but did not test it.

Thanks for the update @bernhard,

I tested the code and now the current user is no longer recognized as logged in, but the current user’s roles are both "guest" and "superuser". The method user()->isGuest() returns false and user()->isSuperuser() returns true.
I assume one cannot really change the user in a module / in the backend to "guest".
So the only way to get the markup as if one is a "guest" user would still be to retrieve the page(s) via WireHTTP.

But if I change the templates so that all user roles get the same markup anyway, I can use your method and saving the pages will be much faster!

Link to comment
Share on other sites

Thanks @bernhard,

it works! I would never have believed that this was really possible. If you knew for how long I have tried this ...

The generated markup is now correctly rendered as if a guest user would see it in the frontend:
User is not logged in, user has the role "guest", user()->isGuest() is true and user()->isSuperuser() is false.
Everything works perfectly 🙂

You are genius. Many, many thanks!

  • Like 2
Link to comment
Share on other sites

  • tb76 changed the title to [SOLVED] ProcessWire as static site generator

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...