I think the issue is because we are using the init function, which doesnât guarantee that the libraries from other packages have been initialized. In this case the handlers from wp-source.
In Frontity, the packages are initialized in the order they are defined in the frontity.settings.js file. In the case of the default mars-theme, the theme is before the wp-source package. This means that when we call the init function of the theme, the handlers donât exist yet.
I guess I didnât understand exactly where to use these actions. When I check âfrontity.libraries.source.handlersâ value at the console, I donât see handlers ending with â.htmlâ.
For mars-theme, can you tell me where I should place this action youâve sent?
For mars-theme, can you tell me where I should place this action youâve sent?
Sure! In the case of the default mars-theme I think you could edit the index.js file. This could be it:
import Theme from "./components";
import image from "@frontity/html2react/processors/image";
import iframe from "@frontity/html2react/processors/iframe";
import link from "@frontity/html2react/processors/link";
const marsTheme = {
name: "@frontity/mars-theme",
roots: {
/**
* In Frontity, any package can add React components to the site.
* We use roots for that, scoped to the `theme` namespace.
*/
theme: Theme,
},
state: {
/**
* State is where the packages store their default settings and other
* relevant state. It is scoped to the `theme` namespace.
*/
theme: {
autoPrefetch: "in-view",
menu: [],
isMobileMenuOpen: false,
featured: {
showOnList: false,
showOnPost: false,
},
},
},
/**
* Actions are functions that modify the state or deal with other parts of
* Frontity like libraries.
*/
actions: {
theme: {
toggleMobileMenu: ({ state }) => {
state.theme.isMobileMenuOpen = !state.theme.isMobileMenuOpen;
},
closeMobileMenu: ({ state }) => {
state.theme.isMobileMenuOpen = false;
},
beforeSSR: ({ libraries }) => {
libraries.source.handlers.map((handler) => {
console.log(handler.pattern);
handler.pattern += "(\\.html)?";
return handler;
});
},
},
},
libraries: {
html2react: {
/**
* Add a processor to `html2react` so it processes the `<img>` tags
* and internal link inside the content HTML.
* You can add your own processors too.
*/
processors: [image, iframe, link],
},
},
};
export default marsTheme;
Oh, I see. I just checked that the post was loading correctly in SSR.
With the second option I suggested, using the init function instead of beforeSSR, but moving the wp-source package before the mars-theme package in the frontity.settings.js file, it seems to appear also in the console. Did that work for you?
Youâre right. Iâve noticed that it only works for the latest post. The older ones donât work in SSR with these solutions. Iâve written what I found in the issue to keep researching the issue there.
It wouldnât work as it is because it would require that our postTypeHandler checks if there is a params.id and use that instead of the params.slug to retrieve the content from the REST API. But I think a change like that would make sense. It is also explained in the video.