Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Is there any alternative to next-payload for same server access to Local API?

default discord avatar
johnrisbylast year
8

Hi all, I setup PayloadCMS on Saturday and on Sunday I was trying to get next-payload working - really just for the Local API access, in many other ways I prefer the separation of next.js frontend and the CMS hosted separately.



I had various problems which I finally resolved but then - when all seemed to be working - I started having the import fs problem I've seen a lot of people mention.



I'm against the clock so I'm going to revert to a standalone payload installation and separate next.js frontend - but they are currently both on the same server.



Is there any simple, but not hacky, way to access the data from Payload via Local API without running the whole CMS inside the next.js app?



Or - as I am planning to do if I don't get an answer 🙂 - should I just use the normal REST API and accept the same-server speed boast I'll get anyway?



Thanks (ps. using npx install of payload with blog demo template, installed on Saturday)

  • default discord avatar
    plumdufferlast year

    If you scroll down the page on

    https://payloadcms.com/docs/getting-started/installation

    there is a

    local

    property you can pass to init payload without having to pass an express server. If you can provide a path to your

    payload.config.ts

    in your serve command via the enivronment variable

    PAYLOAD_CONFIG_PATH

    you should be good to go.

  • default discord avatar
    johnrisbylast year

    Thanks

    @778800940723077130

    , so this is based on "Adding to an existing app"?

  • default discord avatar
    plumdufferlast year

    Not necessarily. Should be fine either way.



    Here's an example of how I'm doing it inside a Qwik app:

    https://github.com/plumduffer/plumduffer-site/blob/qwik/src/qwik/routes/index.tsx#L8-L16
  • default discord avatar
    johnrisbylast year

    Sorry, bit confused. Let's say I've got my payload running on a subdomain cms.domain.com. and my main (existing) next.js app running an www.domain.com which already does things - including accessing mongodb btw, which seems to be another gotcha I found when adding payload to an existing app....



    if I add local to cms, it will disable the api routes, which is fine - in fact great from a security point of view - if I can access the local api from my next.js app. it also says "when set to true, express is not required" - but then I wouldn't be able to access the cms?



    but how then do I access the payload via local api in my next.js app? sorry if this is very basic and I'm missing obvious documented info here, but I went down the next-payload route from the start so it's possible i've missed something obvious.

  • default discord avatar
    plumdufferlast year

    You would be initiating your cms as normal, without the

    local

    property. And then in your next js app, you initiate payload

    again

    with the

    local

    property, letting you interact directly with your database. Of course, you need to do this on your server, whether that's a server component or getStaticProps or getServerSideProps.

  • default discord avatar
    johnrisbylast year

    got you (I think). just trying it now. thanks, will let you know!

  • default discord avatar
    plumdufferlast year

    got a repo to share?

  • default discord avatar
    johnrisbylast year

    no, sorry



    @778800940723077130

    should I be pointing PAYLOAD_CONFIG_PATH to the config of my standalone payloadCMS app? I'm doing that in my next.js app env file but I'm getting "Cannot use import statement outside a module"



    oh, think I've done it. I'm running payload in dev mode, but I just built it, and changed the PAYLOAD_CONFIG_PATH to the dist/payload.config.js instead of the .ts and now I've got a new set of errors but appears to be returning data. investigating!



    yes, working now

    @778800940723077130

    ! the "new set of errors" was just me being dumb and changing a getstaticprops to getserversideprops and leaving revalidate in. it's now returning the post data!



    thanks so much for your help!

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.