|
| 1 | +# Payload Custom Server Example |
| 2 | + |
| 3 | +This example demonstrates how to serve your front-end alongside [Payload](https://github.com/payloadcms/payload) in a single Express server. This pattern will cut down on hosting costs and can simplify your deployment process. |
| 4 | + |
| 5 | +## Quick Start |
| 6 | + |
| 7 | +To spin up this example locally, follow these steps: |
| 8 | + |
| 9 | +1. First clone the repo |
| 10 | +1. Then `cd YOUR_PROJECT_REPO && cp .env.example .env` |
| 11 | +1. Next `yarn && yarn dev` |
| 12 | +1. Now `open http://localhost:3000/admin` to access the admin panel |
| 13 | +1. Login with email `[email protected]` and password `test` |
| 14 | + |
| 15 | +That's it! Changes made in `./src` will be reflected in your app. See the [Development](#development) section for more details. |
| 16 | + |
| 17 | +## How it works |
| 18 | + |
| 19 | +When you use Payload, you plug it into _**your**_ Express server. That's a fundamental difference between Payload and other application frameworks. It means that when you use Payload, you're technically _adding_ Payload to _your_ app, and not building a "Payload app". |
| 20 | + |
| 21 | +One of the strengths of this pattern is that it lets you do powerful things like integrate your Payload instance directly with your front-end. This will allow you to host Payload alongside a fully dynamic, CMS-integrated website or app on a single, combined server—while still getting all of the benefits of a headless CMS. |
| 22 | + |
| 23 | +### Express |
| 24 | + |
| 25 | +In every Payload app is a `server.ts` file in which you instantiate your own Express server and attach Payload to it. This is where you can can add any custom Express middleware or routes you need to serve your front-end. To combine Payload with your framework on the same server, we need to do three main things: |
| 26 | + |
| 27 | +1. Modify your `server.ts` file to build and serve your front-end using the APIs provided by your framework |
| 28 | +2. Modify your `package.json` scripts include your framework's build commands |
| 29 | +3. Use a separate `tsconfig.server.json` file to build the server, because your front-end may require incompatible TypeScript settings |
| 30 | + |
| 31 | +This example demonstrates how to do this with [Next.js](https://nextjs.org), although the same principles apply to any front-end framework like [Vue](https://vuejs.org), [Nuxt](https://nuxt.com), or [SvelteKit](https://kit.svelte.dev). If your framework does not yet have instructions listed here, please consider contributing them to this example for others to use. To quickly eject Next.js from this example, see the [Eject](#eject) section. |
| 32 | + |
| 33 | +#### Next.js |
| 34 | + |
| 35 | +For Next.js apps, your `server.ts` file looks something like this: |
| 36 | + |
| 37 | +```ts |
| 38 | +import next from 'next' |
| 39 | +import nextBuild from 'next/dist/build' |
| 40 | + |
| 41 | +// Instantiate Express and Payload |
| 42 | +// ... |
| 43 | + |
| 44 | +// If building, start the server to build the Next.js app then exit |
| 45 | +if (process.env.NEXT_BUILD) { |
| 46 | + app.listen(3000, async () => { |
| 47 | + await nextBuild(path.join(__dirname, '../')) |
| 48 | + process.exit() |
| 49 | + }) |
| 50 | + |
| 51 | + return |
| 52 | +} |
| 53 | + |
| 54 | +// Attach Next.js routes and start the server |
| 55 | +const nextApp = next({ |
| 56 | + dev: process.env.NODE_ENV !== 'production', |
| 57 | +}) |
| 58 | + |
| 59 | +const nextHandler = nextApp.getRequestHandler() |
| 60 | + |
| 61 | +app.get('*', (req, res) => nextHandler(req, res)) |
| 62 | + |
| 63 | +nextApp.prepare().then(() => { |
| 64 | + app.listen(3000) |
| 65 | +}) |
| 66 | +``` |
| 67 | + |
| 68 | +Check out the [server.ts](./src/server.ts) in this repository for a complete working example. You can also see the [Next.js docs](https://nextjs.org/docs/advanced-features/custom-server) for more details. |
| 69 | + |
| 70 | +Then your `package.json` might look something like this: |
| 71 | + |
| 72 | +```json |
| 73 | +// ... |
| 74 | +"scripts": { |
| 75 | + // ... |
| 76 | + "build:payload": "payload build", |
| 77 | + "build:server": "tsc --project tsconfig.server.json", |
| 78 | + "build:next": "next build", |
| 79 | + "build": "yarn build:payload && yarn build:server && yarn build:next", |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +Check out the [package.json](./src/package.json) in this repository for a complete working example. You can also see the [Next.js docs](https://nextjs.org/docs/api-reference/cli#build) for more details. |
| 84 | + |
| 85 | +## Eject |
| 86 | + |
| 87 | +To eject Next.js from this template and replace it with another front-end framework, follow these steps: |
| 88 | + |
| 89 | +1. Remove the `next`, `react`, and `react-dom` dependencies from your `package.json` file |
| 90 | +1. Remove the `next.config.js` and `next-env.d.ts` files from your project root |
| 91 | +1. Remove the `./src/app` directory and all of its contents |
| 92 | + |
| 93 | +Now you can install and setup any other framework you'd like. Follow the [Express](#express) instructions above to make the necessary changes to build and serve your front-end. |
| 94 | + |
| 95 | +## Development |
| 96 | + |
| 97 | +To spin up this example locally, follow the [Quick Start](#quick-start). |
| 98 | + |
| 99 | +### Seed |
| 100 | + |
| 101 | +On boot, a seed script is included to scaffold a basic database for you to use as an example. This is done by setting the `PAYLOAD_DROP_DATABASE` and `PAYLOAD_SEED` environment variables which are included in the `.env.example` by default. You can remove these from your `.env` to prevent this behavior. You can also freshly seed your project at any time by running `yarn seed`. This seed creates an admin user with email `[email protected]`, password `test`, and a `home` page. |
| 102 | + |
| 103 | +> NOTICE: seeding the database is destructive because it drops your current database to populate a fresh one from the seed template. Only run this command if you are starting a new project or can afford to lose your current data. |
| 104 | +
|
| 105 | +## Production |
| 106 | + |
| 107 | +To run Payload in production, you need to build and serve the Admin panel. To do so, follow these steps: |
| 108 | + |
| 109 | +1. First, invoke the `payload build` script by running `yarn build` or `npm run build` in your project root. This creates a `./build` directory with a production-ready admin bundle. |
| 110 | +1. Then, run `yarn serve` or `npm run serve` to run Node in production and serve Payload from the `./build` directory. |
| 111 | + |
| 112 | +### Deployment |
| 113 | + |
| 114 | +The easiest way to deploy your project is to use [Payload Cloud](https://payloadcms.com/new/import), a one-click hosting solution to deploy production-ready instances of your Payload apps directly from your GitHub repo. You can also choose to self-host your app, check out the [Deployment](https://payloadcms.com/docs/production/deployment) docs for more details. |
| 115 | + |
| 116 | +## Questions |
| 117 | + |
| 118 | +If you have any issues or questions, reach out to us on [Discord](https://discord.com/invite/payload) or start a [GitHub discussion](https://github.com/payloadcms/payload/discussions). |
0 commit comments