Replies: 4 comments 1 reply
-
Sure can! Payload just has to be initialized first. As long as you can initialize Payload, you can use its local API. |
Beta Was this translation helpful? Give feedback.
-
Hi James, const start = async () => {
await payload.init({
secret: 'somestring',
mongoURL: 'mongodb://localhost:27017/payloadcms',
local: true
})
}
start() But what I get is: I don't get how to do it. Am I supposed to move the Local API script (together with my Static Site Generator) into Payload's project folder so it has access to the config, etc? |
Beta Was this translation helpful? Give feedback.
-
Thank you for your support. In the end I couldn't get it to work because my the Static Site Generator Eleventy (that should use Payload's Local API) is build upon CommonJS modules while I'm using ESModules for Payload. |
Beta Was this translation helpful? Give feedback.
-
@jmikrut @DanRibbens Just wondering if there's a bigger feature to be implemented here? The optimal Payload DX is to deploy your CMS anywhere and then reliably expose the Payload Local API & types across package/service boundaries. Sure, you can cram everything into a Next.js project by using Payload Local API via a custom Next.js server example, or next-payload, but each of those come with tradeoffs. On one hand you can't deploy properly to Vercel and on the other you get gnarly cold starts on your CMS. Could a It could build a I anticipate that for most use cases, the method of just pointing There's also the issue @unonweb detailed above, whereby CommonJS and ESModules don't play nicely together and may be incompatible between Payload & the consuming app. The generated package could also handle local API connection management, which is presently a problem in the latest versions of Next.js and export all generated types, therein providing a better solution than having to set TS aliases for consuming them via This would be delicious: import {
getPayloadClient
/*, type Page */
} from '@my-scope/payload-client'
const getPage = cache(async (path: string) => {
const payload = await getPayloadClient()
const { docs } = await payload.find({
collection: 'pages',
where: {
path: {
equals: path
}
}
})
const page = docs[0] // Type is automatically inferred as Page
try {
if (!page) throw new Error('Page not found')
} catch (err) {
return notFound()
}
return page
})
export default async function FooPage () {
const page = await getPage('/foo')
return (
<header>
<h1>{page.title}</h1>
</header>
)
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
just to confirm that this is not possible:
Can I use the Local API from another process (Static Site Generator) running on the same machine?
Beta Was this translation helpful? Give feedback.
All reactions