Remix, Payload in a Single Express Server Monorepo Published On December 6, 2022
unknown node
Payload and Remix in a monorepo setup unknown node unknown node Why a monorepo? unknown node unknown node unknown node Why Remix? unknown node unknown node Close 2 import payload from 'payload' ;
3 import express from 'express' ;
4 import { createRequestHandler } from '@remix-run/express' ;
10 secret : PAYLOADCMS_SECRET ,
12 payload . logger . info ( ` Payload Admin URL: ${ payload . getAdminURL ( ) } ` ) ;
17 app . all ( '*' , ( req , res , next ) =>
18 createRequestHandler ( {
20 getLoadContext ( req , res ) {
unknown node
Close 1 export const loader : LoaderFunction = async ( {
2 context : { payload , user } ,
3 } ) : Promise < LoaderData > => {
5 return { users : [ ] , authenticatedUser : undefined } ;
7 const { docs : users } = await payload . find ( {
13 return { users , authenticatedUser : user } ;
unknown node unknown node unknown node
Local API unknown node unknown node unknown node unknown node Close 1 const response = await payload . create ( {
2 collection : 'statistics' ,
unknown node Routing unknown node unknown node unknown node unknown node Close 1 import { Response } from '@remix-run/node' ;
2 import type { Page } from '@org/cms' ;
3 import type { TypedResponse , LoaderFunction } from '@remix-run/node' ;
5 export type LoaderData = {
8 export const loader : LoaderFunction = async ( {
9 context : { payload , user } ,
12 } ) : Promise < LoaderData | TypedResponse < never > > => {
13 const { pathname } = new URL ( request . url ) ;
14 if ( pathname === '/' ) {
16 return redirect ( '/home' ) ;
19 const { pageSlug } = params ;
20 const { docs : [ page ] } = await payload . find ( {
23 overrideAccess : false ,
24 where : { slug : { equals : pageSlug } } ,
28 return new Response ( 'Not found' , { status : 404 } ) ;
unknown node unknown node Create an index route which allow the user to navigate to the other pages, or; Simply re-export the default loader and component from our dynamic $page
route The second option looks like:
Close 2 export { default } from './$page' ;
unknown node unknown node
Close 1 import type { RootLoaderData } from '~/root' ;
3 export default function Page ( ) {
4 const { page : pageSlug } = useParams ( ) ;
5 const [ { data } ] = useMatches ( ) ;
6 const { pages , user } = data as RootLoaderData ;
7 const page = pages ?. find ( ( page ) => page . slug === pageSlug ) ;
Layout Route unknown node unknown node unknown node unknown node SEO unknown node unknown node unknown node Close 1 import type { MetaFunction } from '@remix-run/node' ;
3 export const meta : MetaFunction = ( { parentsData , params } ) => {
4 const { page : pageSlug } = params ;
9 const page = pages ?. find ( ( page ) => page . slug === pageSlug ) ;
11 title : page ?. meta . title ,
12 description : page ?. meta . description ,
13 keywords : page ?. meta . keywords ,
Access Control unknown node unknown node unknown node unknown node Bonuses Lastly, here are a few tips and tricks that I have found useful when working with Payload and Remix—
Typed Remix context:
Close 4 declare module '@remix-run/node' {
5 interface AppLoadContext {
unknown node
Close 2 export const action : ActionFunction = async ( {
3 context : { payload , user , res } ,
10 data : { email , password } ,