The team is now working on the WordPress Interactivity API. This unblocks the same UX Frontity framework enabled but directly in WordPress Core, fully compatible with the new Site Editor.
I just saw the JS for WP Conf Talk from @SantosGuillamot and was very excited about the possibility to load Gutenberg template-parts into Frontity.
So I tried to do the same⦠But it wonāt work for me. I installed WordPress 5.5-beta1-48411, Gutenberg 8.5.1 and enabled Full Site Editing. I also created a template-part, which can be found at this wp-json url: https://gatsby-wp.noesteprojecten.nl/wp-json/wp/v2/template-parts/
TypeError: Cannot read property 'map' of undefined
at beforeSSR (webpack-internal:///./packages/mars-theme/src/index.js:21:184)
at eval (webpack-internal:///./node_modules/@frontity/connect/src/create-store.js:5:1528)
at eval (webpack-internal:///./node_modules/@frontity/core/src/server/index.tsx:49:88)
at Array.map (<anonymous>)
at eval (webpack-internal:///./node_modules/@frontity/core/src/server/index.tsx:49:48)
at processTicksAndRejections (internal/process/task_queues.js:94:5)
at eval (webpack-internal:///./node_modules/koa-mount/index.js:16:290)
Am I missing something? Do I need a specific version of Frontity? Iām running version 1.9.0 at the moment.
How are you populating state.theme.templates? The map function operates on an array and returns a new array. The result of the map operation needs to be assigned to a variable.
Hope this helps to guide you to a solution. Let us know if you need any further help.
dominique3
Hi @mburridge,
Thanks for your quick response. Frankly, I really donāt even have a clue about what I am doing here. I saw @SantosGuillamot show this exact code in his Talk and got really excited about the possibilities.
So I just typed over the shown code into my index.js file. As soon as I add this, my project completely fails (āInternal Server Errorā), with the error message above.
mburridge4
Hi @dominique
I would have thought that state.theme.templates needs to be populated with an array before the map function can operate on it. If thereās nothing there then that explains why youāre getting the message that āmapā cannot be read on āundefinedā since state.theme.templates is indeed undefined.
The other thing that confuses me about your code example is that the return value of the map operation isnāt being assigned to anything.
Maybe @SantosGuillamot can step in here with some guidance.
dominique6
But this is how beforeSSR works, right? The other fetch commands were working fine.
Maybe I am missing a package for Gutenberg.
mburridge7
Yes, kind of. The other fetch commands will work fine. Itās because your call to the wp_template_part endpoint is called within map, and map is not firing because state.theme.templates is undefined. Unless youāre populating state.theme.templates in frontity.settings.js?
dominique8
Well⦠no I didnāt add anything in frontity.settings.js. I am really new to the whole concept of Frontity, so didnāt realize this was needed.
I added the following to postTypes in frontity.settings.js now:
With the additions to beforeSSR disabled I am able to view the template-parts when I specifically visit them in my browser. But when I enable the additions to beforeSSR again, the whole sting still crashes.
SantosGuillamot9
Hi @dominique! Iām glad youāre testing template parts after the talk
First of all, sorry because I think I didnāt explain this part properly. Template Parts is a Custom Post Type Gutenberg is adding so, in order to make it work you just have to defined them in Frontity as any other Custom Post Type as you did (these are the docs just in case). In you frontity.settings.js you should have something similar to this:
From there, what we did was to define all the template-parts slugs we wanted to fetch in state.theme.templates, but note that if you define a slug that doesnāt exist in your WordPress, it isnāt going to find anything so itās going to return an error. This may be the cause of your problem, so make sure you only include template-parts slugs that are defined in your WordPress.
Once you have created all the template parts you need in your WordPress, and you have included the slugs in state.theme.templates, you can include the function in beforeSSR as you did.
This should allow you to do state.source.get("/wp_template_part/:slug/") and get the content of a specific template-part.
If you have a public repo you can share, I can take a look at the code and check what could be the problem
dominique11
Hi @SantosGuillamot, I havenāt really defined any template-parts in my code (yet). Could that also be the root of the problem?
Yes, thatās problably the root of the problem. In the action you define beforeSSR youāre trying to map state.theme.templates but, as you havenāt defined it yet, it returns an error. Let us know if it works once you add some of them!
and in the footer.js of my theme I am trying something like that:
const FooterContent = ({ state, actions, libraries }) => {
const data = state.source.get("/wp_template_part/footer");
const post = state.source["wp_template_part"][data.id];
...
Now in the frontity.state.source.data I can see āwp_template_part/footer/ā but itās on isFetching == true, and getting the post in the footer. js obviously is not working.
Somebody got an idea on where to look? The WP Site is on /wordpress/ may this have some effect?
dominique17
I might very well be wrong but I think you might have to use actions.source.fetch(/wp_template_part/${slug}), with a slash before wp_template_part.
phn18
Oh, you were right I added the leading slashes (as well as in footer.js) and now the data can be queried
Thanks for your answer
dominique19
No problem, Iām trial & errorring my way throught Frontity and React as well and sometimes it can become very hard to see things like these. In those cases I turn to the Frontity Community as well, in hope for answers.
3 Likes
briyadi20
Hi @dominique, I following your step above and quite new at WP REST API.
My API are not working correctly here and showing 401. What is the problem here?
Do you use additional WP REST API plugin for that? If so, can you share WP plugin that you use to activate the API for template and template-parts?
I installed WordPress 5.6.2 and Gutenberg 10.1.1
Thanks!
dominique21
Hi Briyadi,
Lately I havenāt had the time to play around with Frontity any more. But I noticed that the template and template-parts endpoint didnāt work any more, I donāt know why and hope someone else can shed some light on thisā¦