Adding new custom post endpoint handler

Hi there,

Frontity looks like a really compelling solution; thank you so much. I am keen to give it a go.

I am trying to add lists and post endpoints for a new custom post type, and I found this handler for a product post type in the documentation: https://docs.frontity.org/api-reference-1/wordpress-source#example-1

I placed this code block in mars-theme/src/index.js within the mars-theme’s before constant, so it is called in the beforeSSR action. Unfortunately, when loading the new products endpoint, I see a 404 error in the browser and in the terminal this error:

TypeError: Cannot destructure property id of ‘undefined’ or ‘null’.
at attachmentHandler (webpack-internal:///./node_modules/@frontity/wp-source/src/libraries/handlers/attachment.ts:4:129)
at processTicksAndRejections (internal/process/task_queues.js:85:5)
at Object.postType [as func] (webpack-internal:///./node_modules/@frontity/wp-source/src/libraries/handlers/postType.ts:5:262)
at eval (webpack-internal:///./node_modules/@frontity/wp-source/src/actions.ts:13:100)

I have been able to get a small Create React App to list these CPTs no problem, so I know the REST API is open, and can see the JSON, but would love to better understand how Frontity works to get this working. Any help or tips hugely appreciated.

Cheers,
Toby

2 Likes

Hi @toby, and welcome! :wave:

I see the error occurs inside the attachment handler. This means that you may have tried to fetch a link that doesn’t match the pattern of the product handler (/product/:slug), or for some reason the product handler hasn’t been added to libraries.source.handlers.

What’s the content of window.frontity.libraries.source.handlers in the browser?

2 Likes

Hi @David, thanks for responding. I can see the “product” handler is there:

But there’s still nothing found in the browser, just 404.

If I get http://wp.local/wp-json/wp/v2/product in Postman I can see the products listed in a json file

1 Like

When I enter a slug for a product that exists, the website shows “Internal Server Error”

1 GET http://localhost:3000/product/pig-fiddle/ 500 (Internal Server Error)

1 Like

Ok, that means some error happenend while running the product handler (but at least it was executed).

The problem could be the endpoint used when calling libraries.source.api.get inside the handler. That endpoint should match the endpoint of your WP REST API, and in your case it seems to be product instead of products (the value we set in the example).

Try changing that value and tell me if it works!

1 Like

Hi @David, thanks yes I had already changed the endpoint value to singular product. Before then, when using the value as in the example, it was always returning 404. The 500 internal server error at least shows something is catching – but it’s not clear what is going on. At least to me it’s not :confused:

Could you please share the code of your handler? Also, how are you dealing with it in your theme? Using something like data.isProduct? Or maybe put the whole repo in GitHub so we can take a look here :slight_smile:

Oh, and handlers give you more flexibility, but support for custom post types and taxonomies with a simple wp-source setting is almost finished, I think. Right @david?

By the way, here you have an example of two handlers working fine:

1 Like