I need to create a custom handler to fetch info from my custom WP-REST api endpoint. I have done this before. Actually, this worked, but as soon as I changed the pattern to “/blog” (so info would be added to postsPage
), the handler isn’t being even evaluated. I have tried changing the priority and the pattern. I’m logging the data received, and it seems to me everything is on its place but handler still not working.
I created a brand new project with the necessary code for evaluation. Here is the repository url:
Here you can see snippets of the code:
/src/handlers/categories.js, where I export the handler. Console.log
is never fired.
const categories = {
name: 'categories',
priority: 10,
pattern: '/blog',
func: async ({link, libraries, state}) => {
console.log('Being handled') /*I should see this on console*/
const response = await libraries.source.api.get({
endpoint: '/jasonsegnini/v1/categories/'
})
const categories = await response.json()
Object.assign(state.source.data[link], {
categories: categories
})
}
}
export default categories
The index.js:
import Root from "./components"
import categories from "./handlers/categories"
const myFirstTheme = {
name: "test-theme",
roots: {
theme: Root,
},
state: {
theme: {},
},
actions: {
theme: {
init: ({libraries}) =>
libraries.source.handlers.push(categories),
beforeSSR: async ({state, actions}) =>
await actions.source.fetch(state.source.postsPage) /*postsPage is /blog, same as the handler's pattern*/
},
},
}
export default myFirstTheme
The theme settings:
const settings = {
"name": "test-project",
"state": {
"frontity": {
"url": "https://test.frontity.org",
"title": "Test Frontity Blog",
"description": "WordPress installation for Frontity development"
}
},
"packages": [
{
"name": "test-theme"
},
{
"name": "@frontity/wp-source",
"state": {
"source": {
"url": "https://jasonsegnini.com/",
"homepage": "/home",
"postsPage": "/blog"
}
}
},
"@frontity/tiny-router",
"@frontity/html2react"
]
};
export default settings;
The root component for testing (/src/components/index.js):
import {connect} from "frontity";
const Root = ({state}) => {
const data = state.source.get(state.source.postsPage)
console.log(state.source.postsPage) /*Output: /blog*/
const items = data.categories
console.log(items) /*undefined*/
return (
<>
You can edit your package in:
<pre>packages\test-theme\src\index.js</pre>
</>
);
};
export default connect(Root)
Take into account that issue doesn’t seem to be happening inside the handler as console.log isn’t outputting anything. In theory, this should work, right? Or am I missing something?