chapter-three / next-drupal

Next.js for Drupal has everything you need to build a next-generation front-end for your Drupal site: SSG, SSR, and ISR, Multi-site, Authentication, Webforms, Search API, I18n and Preview mode (works with JSON:API and GraphQL).
https://next-drupal.org
MIT License
629 stars 175 forks source link

Idea: Create Next API pages to proxy/mimic next-drupal methods #323

Open theodorosploumis opened 1 year ago

theodorosploumis commented 1 year ago

As far as I can understand, all the next-drupal methods are async and return Promises. This disallows using them inside React components unless you use useEffect or similar hooks.

Do you believe is is a good idea to create a proxy like API route to handle the async functions? I mean create this inside the next-drupal starter kit.

E.g. for method getResource() we could have an API route on /api/next-drupal/getResource asking for the same parameters with the original function (https://next-drupal.org/docs/reference/getresource).

What are your thoughts?

shadcn commented 1 year ago

I see what you mean. We're actually doing same for most of our sites: using a proxy API route for making getResource and getResourceCollection calls.

The reason we did not ship this in the package and rather leave it to the dev for implementation is we want the package to be unopinionated and not hardcode the routes.

Maybe we could cover this in a guide instead? The implementation is quite simple.

What do you think?

(Note: this might change in the upcoming release of Next.js which will allow component-level data fetching)

theodorosploumis commented 1 year ago

...component-level data fetching

That's great. Do you have any link for this discussion?

backlineint commented 2 months ago

I believe server components in the app router will make this possible. When the docs for this are completed we can link them back here, and close this issue.