tb76 Posted April 24, 2023 Share Posted April 24, 2023 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 More sharing options...
flydev Posted April 24, 2023 Share Posted April 24, 2023 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 More sharing options...
bernhard Posted April 24, 2023 Share Posted April 24, 2023 <?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. 8 Link to comment Share on other sites More sharing options...
tb76 Posted April 25, 2023 Author Share Posted April 25, 2023 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 More sharing options...
bernhard Posted April 25, 2023 Share Posted April 25, 2023 I have updated my code, but did not test it. Link to comment Share on other sites More sharing options...
tb76 Posted April 25, 2023 Author Share Posted April 25, 2023 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 More sharing options...
tb76 Posted April 25, 2023 Author Share Posted April 25, 2023 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 More sharing options...
bernhard Posted April 25, 2023 Share Posted April 25, 2023 I think I've fixed that ? Does it work now? 2 1 Link to comment Share on other sites More sharing options...
tb76 Posted April 25, 2023 Author Share Posted April 25, 2023 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! 2 Link to comment Share on other sites More sharing options...
bernhard Posted April 25, 2023 Share Posted April 25, 2023 Great ? 2 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now