diff --git a/playground/.editorconfig b/playground/.editorconfig new file mode 100644 index 0000000..d8e085a --- /dev/null +++ b/playground/.editorconfig @@ -0,0 +1,10 @@ +root = true + +[*] +indent_style = space +indent_size = 2 +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true +end_of_line = lf +max_line_length = null diff --git a/playground/.env.example b/playground/.env.example new file mode 100644 index 0000000..45327b9 --- /dev/null +++ b/playground/.env.example @@ -0,0 +1,16 @@ +# Added by Payload +# Database connection string +DATABASE_URI=file:./playground.db +# Used to encrypt JWT tokens +PAYLOAD_SECRET=5a9a296d283d03980a3110f3 +# Used to format links and URLs +PAYLOAD_PUBLIC_SERVER_URL=http://localhost:3000 +NEXT_PUBLIC_SERVER_URL=http://localhost:3000 +# Allow robots to index the site (optional) +NEXT_PUBLIC_IS_LIVE= +# Used to preview drafts +PAYLOAD_PUBLIC_DRAFT_SECRET=demo-draft-secret +NEXT_PRIVATE_DRAFT_SECRET=demo-draft-secret +# Used to revalidate static pages +REVALIDATION_KEY=demo-revalation-key +NEXT_PRIVATE_REVALIDATION_KEY=demo-revalation-key diff --git a/playground/.eslintignore b/playground/.eslintignore new file mode 100644 index 0000000..fa111f7 --- /dev/null +++ b/playground/.eslintignore @@ -0,0 +1,12 @@ +.tmp +**/.git +**/.hg +**/.pnp.* +**/.svn +**/.yarn/** +**/build +**/dist/** +**/node_modules +**/temp +playwright.config.ts +jest.config.js diff --git a/playground/.eslintrc.cjs b/playground/.eslintrc.cjs new file mode 100644 index 0000000..d21138f --- /dev/null +++ b/playground/.eslintrc.cjs @@ -0,0 +1,8 @@ +module.exports = { + extends: 'next', + root: true, + parserOptions: { + project: ['./tsconfig.json'], + tsconfigRootDir: __dirname, + }, +} diff --git a/playground/.gitignore b/playground/.gitignore new file mode 100644 index 0000000..52bec0f --- /dev/null +++ b/playground/.gitignore @@ -0,0 +1,10 @@ +build +dist / media +node_modules +.DS_Store +.env +.next +.vercel + +# Payload default media upload directory +public/media/ diff --git a/playground/.prettierignore b/playground/.prettierignore new file mode 100644 index 0000000..996b10e --- /dev/null +++ b/playground/.prettierignore @@ -0,0 +1,14 @@ +**/payload-types.ts +.tmp +**/.git +**/.hg +**/.pnp.* +**/.svn +**/.yarn/** +**/build +**/dist/** +**/node_modules +**/temp +**/docs/** +tsconfig.json + diff --git a/playground/.prettierrc.json b/playground/.prettierrc.json new file mode 100644 index 0000000..cb8ee26 --- /dev/null +++ b/playground/.prettierrc.json @@ -0,0 +1,6 @@ +{ + "singleQuote": true, + "trailingComma": "all", + "printWidth": 100, + "semi": false +} diff --git a/playground/Dockerfile b/playground/Dockerfile new file mode 100644 index 0000000..e7a5ead --- /dev/null +++ b/playground/Dockerfile @@ -0,0 +1,24 @@ +FROM node:18.8-alpine as base + +FROM base as builder + +WORKDIR /home/node/app +COPY package*.json ./ + +COPY . . +RUN yarn install +RUN yarn build + +FROM base as runtime + +ENV NODE_ENV=production + +WORKDIR /home/node/app +COPY package*.json ./ +COPY yarn.lock ./ + +RUN yarn install --production + +EXPOSE 3000 + +CMD ["node", "dist/server.js"] diff --git a/playground/README.md b/playground/README.md new file mode 100644 index 0000000..a926690 --- /dev/null +++ b/playground/README.md @@ -0,0 +1,223 @@ +# Payload Website Template + +This is the official [Payload Website Template](https://github.com/payloadcms/payload/blob/main/templates/website). Use it to power websites, blogs, or portfolios from small to enterprise. This repo includes a fully-working backend, enterprise-grade admin panel, and a beautifully designed, production-ready website. + +This template is right for you if you are working on: + +- A personal or enterprise-grade website, blog, or portfolio +- A content publishing platform with a fully featured publication workflow +- A lead generation website with premium content gated behind authentication + +Core features: + +- [Pre-configured Payload Config](#how-it-works) +- [Authentication](#users-authentication) +- [Access Control](#access-control) +- [Layout Builder](#layout-builder) +- [Draft Preview](#draft-preview) +- [Live Preview](#live-preview) +- [Redirects](#redirects) +- [SEO](#seo) +- [Website](#website) + +## Quick Start + +To spin up this example locally, follow these steps: + +### Clone + +If you have not done so already, you need to have standalone copy of this repo on your machine. If you've already cloned this repo, skip to [Development](#development). + +#### Method 1 (recommended) + +Go to Payload Cloud and [clone this template](https://payloadcms.com/new/clone/website). This will create a new repository on your GitHub account with this template's code which you can then clone to your own machine. + +#### Method 2 + +Use the `create-payload-app` CLI to clone this template directly to your machine: + + npx create-payload-app@beta my-project -t website + +#### Method 3 + +Use the `git` CLI to clone this template directly to your machine: + + git clone -n --depth=1 --filter=tree:0 https://github.com/payloadcms/payload my-project && cd my-project && git sparse-checkout set --no-cone templates/website && git checkout && rm -rf .git && git init && git add . && git mv -f templates/website/{.,}* . && git add . && git commit -m "Initial commit" + +### Development + +1. First [clone the repo](#clone) if you have not done so already +1. `cd my-project && cp .env.example .env` to copy the example environment variables +1. `pnpm install && pnpm dev` to install dependencies and start the dev server +1. open `http://localhost:3000` to open the app in your browser + +That's it! Changes made in `./src` will be reflected in your app. Follow the on-screen instructions to login and create your first admin user. Then check out [Production](#production) once you're ready to build and serve your app, and [Deployment](#deployment) when you're ready to go live. + +## How it works + +The Payload config is tailored specifically to the needs of most websites. It is pre-configured in the following ways: + +### Collections + +See the [Collections](https://payloadcms.com/docs/beta/configuration/collections) docs for details on how to extend this functionality. + +- #### Users (Authentication) + + Users are auth-enabled collections that have access to the admin panel and unpublished content. See [Access Control](#access-control) for more details. + + For additional help, see the official [Auth Example](https://github.com/payloadcms/payload/tree/beta/examples/auth) or the [Authentication](https://payloadcms.com/docs/beta/authentication/overview#authentication-overview) docs. + +- #### Posts + + Posts are used to generated blog posts, news articles, or any other type of content that is published over time. All posts are layout builder enabled so you can generate unique layouts for each post using layout-building blocks, see [Layout Builder](#layout-builder) for more details. Posts are also draft-enabled so you can preview them before publishing them to your website, see [Draft Preview](#draft-preview) for more details. + +- #### Pages + + All pages are layout builder enabled so you can generate unique layouts for each page using layout-building blocks, see [Layout Builder](#layout-builder) for more details. Pages are also draft-enabled so you can preview them before publishing them to your website, see [Draft Preview](#draft-preview) for more details. + +- #### Media + + This is the uploads enabled collection used by pages, posts, and projects to contain media like images, videos, downloads, and other assets. + +- #### Categories + + A taxonomy used to group posts together. Categories can be nested inside of one another, for example "News > Technology". See the official [Payload Nested Docs Plugin](https://payloadcms.com/docs/beta/plugins/nested-docs) for more details. + +### Globals + +See the [Globals](https://payloadcms.com/docs/configuration/globals) docs for details on how to extend this functionality. + +- `Header` + + The data required by the header on your front-end like nav links. + +- `Footer` + + Same as above but for the footer of your site. + +## Access control + +Basic access control is setup to limit access to various content based based on publishing status. + +- `users`: Users can access the admin panel and create or edit content. +- `posts`: Everyone can access published posts, but only users can create, update, or delete them. +- `pages`: Everyone can access published pages, but only users can create, update, or delete them. + +For more details on how to extend this functionality, see the [Payload Access Control](https://payloadcms.com/docs/beta/access-control/overview#access-control) docs. + +## Layout Builder + +Create unique page layouts for any type of content using a powerful layout builder. This template comes pre-configured with the following layout building blocks: + +- Hero +- Content +- Media +- Call To Action +- Archive + +Each block is fully designed and built into the front-end website that comes with this template. See [Website](#website) for more details. + +## Lexical editor + +A deep editorial experience that allows complete freedom to focus just on writing content without breaking out of the flow with support for Payload blocks, media, links and other features provided out of the box. See [Lexical](https://payloadcms.com/docs/beta/lexical/overview) docs. + +## Draft Preview + +All posts and pages are draft-enabled so you can preview them before publishing them to your website. To do this, these collections use [Versions](https://payloadcms.com/docs/beta/configuration/collections#versions) with `drafts` set to `true`. This means that when you create a new post, project, or page, it will be saved as a draft and will not be visible on your website until you publish it. This also means that you can preview your draft before publishing it to your website. To do this, we automatically format a custom URL which redirects to your front-end to securely fetch the draft version of your content. + +Since the front-end of this template is statically generated, this also means that pages, posts, and projects will need to be regenerated as changes are made to published documents. To do this, we use an `afterChange` hook to regenerate the front-end when a document has changed and its `_status` is `published`. + +For more details on how to extend this functionality, see the official [Draft Preview Example](https://github.com/payloadcms/payload/tree/beta/examples/draft-preview). + +## Live preview + +In addition to draft previews you can also enable live preview to view your end resulting page as you're editing content with full support for SSR rendering. See [Live preview docs](https://payloadcms.com/docs/beta/live-preview/overview) for more details. + +## SEO + +This template comes pre-configured with the official [Payload SEO Plugin](https://payloadcms.com/docs/beta/plugins/seo) for complete SEO control from the admin panel. All SEO data is fully integrated into the front-end website that comes with this template. See [Website](#website) for more details. + +## Redirects + +If you are migrating an existing site or moving content to a new URL, you can use the `redirects` collection to create a proper redirect from old URLs to new ones. This will ensure that proper request status codes are returned to search engines and that your users are not left with a broken link. This template comes pre-configured with the official [Payload Redirects Plugin](https://payloadcms.com/docs/beta/plugins/redirects) for complete redirect control from the admin panel. All redirects are fully integrated into the front-end website that comes with this template. See [Website](#website) for more details. + +## Website + +This template includes a beautifully designed, production-ready front-end built with the [Next.js App Router](https://nextjs.org), served right alongside your Payload app in a instance. This makes it so that you can deploy both your backend and website where you need it. + +Core features: + +- [Next.js App Router](https://nextjs.org) +- [TypeScript](https://www.typescriptlang.org) +- [React Hook Form](https://react-hook-form.com) +- [Payload Admin Bar](https://github.com/payloadcms/payload-admin-bar) +- [TailwindCSS styling](https://tailwindcss.com/) +- [shadcn/ui components](https://ui.shadcn.com/) +- Authentication +- Fully featured blog +- Publication workflow +- User accounts +- Dark mode +- Pre-made layout building blocks +- SEO +- Redirects +- Live preview + +### Cache + +Although Next.js includes a robust set of caching strategies out of the box, Payload Cloud proxies and caches all files through Cloudflare using the [Official Cloud Plugin](https://github.com/payloadcms/plugin-cloud). This means that Next.js caching is not needed and is disabled by default. If you are hosting your app outside of Payload Cloud, you can easily reenable the Next.js caching mechanisms by removing the `no-store` directive from all fetch requests in `./src/app/_api` and then removing all instances of `export const dynamic = 'force-dynamic'` from pages files, such as `./src/app/(pages)/[slug]/page.tsx`. For more details, see the official [Next.js Caching Docs](https://nextjs.org/docs/app/building-your-application/caching). + +## Development + +To spin up this example locally, follow the [Quick Start](#quick-start). Then [Seed](#seed) the database with a few pages, posts, and projects. + +### Docker + +Alternatively, you can use [Docker](https://www.docker.com) to spin up this template locally. To do so, follow these steps: + +1. Follow [steps 1 and 2 from above](#development), the docker-compose file will automatically use the `.env` file in your project root +1. Next run `docker-compose up` +1. Follow [steps 4 and 5 from above](#development) to login and create your first admin user + +That's it! The Docker instance will help you get up and running quickly while also standardizing the development environment across your teams. + +### Seed + +To seed the database with a few pages, posts, and projects you can click the 'seed database' link from the admin panel. + +The seed script will also create a demo user for demonstration purposes only: + +- Demo Author + - Email: `demo-author@payloadcms.com` + - Password: `password` + +> 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. + +## Production + +To run Payload in production, you need to build and start the Admin panel. To do so, follow these steps: + +1. Invoke the `next build` script by running `pnpm build` or `npm run build` in your project root. This creates a `.next` directory with a production-ready admin bundle. +1. Finally run `pnpm start` or `npm run start` to run Node in production and serve Payload from the `.build` directory. +1. When you're ready to go live, see [Deployment](#deployment) for more details. + +### Deploying to Payload Cloud + +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. + +### Deploying to Vercel + +Coming soon. + +### Self-hosting + +Before deploying your app, you need to: + +1. Ensure your app builds and serves in production. See [Production](#production) for more details. +2. Serve it from a + +You can also deploy your app manually, check out the [deployment documentation](https://payloadcms.com/docs/beta/production/deployment) for full details. + +## Questions + +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). diff --git a/playground/components.json b/playground/components.json new file mode 100644 index 0000000..f7e50ae --- /dev/null +++ b/playground/components.json @@ -0,0 +1,17 @@ +{ + "$schema": "https://ui.shadcn.com/schema.json", + "style": "default", + "rsc": true, + "tsx": true, + "tailwind": { + "config": "tailwind.config.js", + "css": "src/app/(frontend)/globals.css", + "baseColor": "slate", + "cssVariables": true, + "prefix": "" + }, + "aliases": { + "components": "@/components", + "utils": "@/_utilities" + } +} diff --git a/playground/docker-compose.yml b/playground/docker-compose.yml new file mode 100644 index 0000000..4c9fc51 --- /dev/null +++ b/playground/docker-compose.yml @@ -0,0 +1,31 @@ +version: '3' + +services: + payload: + image: node:18-alpine + ports: + - '3000:3000' + volumes: + - .:/home/node/app + - node_modules:/home/node/app/node_modules + working_dir: /home/node/app/ + command: sh -c "yarn install && yarn dev" + depends_on: + - mongo + env_file: + - .env + + mongo: + image: mongo:latest + ports: + - '27017:27017' + command: + - --storageEngine=wiredTiger + volumes: + - data:/data/db + logging: + driver: none + +volumes: + data: + node_modules: diff --git a/playground/next-env.d.ts b/playground/next-env.d.ts new file mode 100644 index 0000000..4f11a03 --- /dev/null +++ b/playground/next-env.d.ts @@ -0,0 +1,5 @@ +/// +/// + +// NOTE: This file should not be edited +// see https://nextjs.org/docs/basic-features/typescript for more information. diff --git a/playground/next.config.js b/playground/next.config.js new file mode 100644 index 0000000..e8e6935 --- /dev/null +++ b/playground/next.config.js @@ -0,0 +1,25 @@ +import { withPayload } from '@payloadcms/next/withPayload' + +import redirects from './redirects.js' + +const NEXT_PUBLIC_SERVER_URL = process.env.NEXT_PUBLIC_SERVER_URL || 'http://localhost:3000' + +/** @type {import('next').NextConfig} */ +const nextConfig = { + images: { + remotePatterns: [ + ...[NEXT_PUBLIC_SERVER_URL /* 'https://example.com' */].map((item) => { + const url = new URL(item) + + return { + hostname: url.hostname, + protocol: url.protocol.replace(':', ''), + } + }), + ], + }, + reactStrictMode: true, + redirects, +} + +export default withPayload(nextConfig) diff --git a/playground/package.json b/playground/package.json new file mode 100644 index 0000000..4af065a --- /dev/null +++ b/playground/package.json @@ -0,0 +1,86 @@ +{ + "name": "playground", + "version": "1.0.0", + "description": "Website template for Payload", + "license": "MIT", + "type": "module", + "scripts": { + "build": "cross-env NODE_OPTIONS=--no-deprecation next build", + "dev": "cross-env NODE_OPTIONS=--no-deprecation next dev", + "dev:prod": "cross-env NODE_OPTIONS=--no-deprecation rm -rf .next && pnpm build && pnpm start", + "generate:importmap": "cross-env NODE_OPTIONS=--no-deprecation payload generate:importmap", + "generate:types": "cross-env NODE_OPTIONS=--no-deprecation payload generate:types", + "ii": "cross-env NODE_OPTIONS=--no-deprecation pnpm --ignore-workspace install", + "lint": "cross-env NODE_OPTIONS=--no-deprecation next lint", + "lint:fix": "cross-env NODE_OPTIONS=--no-deprecation next lint --fix", + "payload": "cross-env NODE_OPTIONS=--no-deprecation payload", + "reinstall": "cross-env NODE_OPTIONS=--no-deprecation rm -rf node_modules && rm pnpm-lock.yaml && pnpm --ignore-workspace install", + "start": "cross-env NODE_OPTIONS=--no-deprecation next start" + }, + "dependencies": { + "@payload-llm-plugins/semantic-search": "workspace:*", + "@payloadcms/live-preview-react": "beta", + "@payloadcms/next": "beta", + "@payloadcms/plugin-cloud": "beta", + "@payloadcms/plugin-form-builder": "beta", + "@payloadcms/plugin-nested-docs": "beta", + "@payloadcms/plugin-redirects": "beta", + "@payloadcms/plugin-seo": "beta", + "@payloadcms/richtext-lexical": "beta", + "@payloadcms/ui": "beta", + "@radix-ui/react-checkbox": "^1.0.4", + "@radix-ui/react-label": "^2.0.2", + "@radix-ui/react-select": "^2.0.0", + "@radix-ui/react-slot": "^1.0.2", + "class-variance-authority": "^0.7.0", + "clsx": "^2.1.1", + "cross-env": "^7.0.3", + "geist": "^1.3.0", + "graphql": "^16.8.2", + "jsonwebtoken": "9.0.2", + "lexical": "0.17.0", + "lucide-react": "^0.378.0", + "next": "15.0.0-canary.104", + "payload": "beta", + "payload-admin-bar": "^1.0.6", + "prism-react-renderer": "^2.3.1", + "react": "19.0.0-rc-06d0b89e-20240801", + "react-dom": "19.0.0-rc-06d0b89e-20240801", + "react-hook-form": "7.45.4", + "sharp": "0.32.6", + "tailwind-merge": "^2.3.0", + "tailwindcss-animate": "^1.0.7", + "@payloadcms/db-sqlite": "beta" + }, + "devDependencies": { + "@next/eslint-plugin-next": "^13.1.6", + "@payloadcms/eslint-config": "^1.1.1", + "@tailwindcss/typography": "^0.5.13", + "@types/escape-html": "^1.0.2", + "@types/jsonwebtoken": "^9.0.6", + "@types/node": "22.5.4", + "@types/react": "npm:types-react@19.0.0-rc.0", + "@types/react-dom": "npm:types-react-dom@19.0.0-rc.0", + "autoprefixer": "^10.4.19", + "copyfiles": "^2.4.1", + "eslint": "^8", + "eslint-config-next": "15.0.0-canary.104", + "postcss": "^8.4.38", + "prettier": "^3.0.3", + "tailwindcss": "^3.4.3", + "typescript": "5.6.2" + }, + "engines": { + "node": "^18.20.2 || >=20.9.0" + }, + "pnpm": { + "overrides": { + "@types/react": "npm:types-react@19.0.0-rc.0", + "@types/react-dom": "npm:types-react-dom@19.0.0-rc.0" + } + }, + "overrides": { + "@types/react": "npm:types-react@19.0.0-rc.0", + "@types/react-dom": "npm:types-react-dom@19.0.0-rc.0" + } +} diff --git a/playground/playground.db b/playground/playground.db new file mode 100644 index 0000000..a8b3d60 Binary files /dev/null and b/playground/playground.db differ diff --git a/playground/postcss.config.js b/playground/postcss.config.js new file mode 100644 index 0000000..ebf881f --- /dev/null +++ b/playground/postcss.config.js @@ -0,0 +1,6 @@ +export default { + plugins: { + autoprefixer: {}, + tailwindcss: {}, + }, +} diff --git a/playground/public/favicon.ico b/playground/public/favicon.ico new file mode 100644 index 0000000..601b777 Binary files /dev/null and b/playground/public/favicon.ico differ diff --git a/playground/public/favicon.svg b/playground/public/favicon.svg new file mode 100644 index 0000000..86dc2de --- /dev/null +++ b/playground/public/favicon.svg @@ -0,0 +1,15 @@ + + + + + diff --git a/playground/public/website-template-OG.webp b/playground/public/website-template-OG.webp new file mode 100644 index 0000000..74bc241 Binary files /dev/null and b/playground/public/website-template-OG.webp differ diff --git a/playground/redirects.js b/playground/redirects.js new file mode 100644 index 0000000..21b76ec --- /dev/null +++ b/playground/redirects.js @@ -0,0 +1,20 @@ +const redirects = async () => { + const internetExplorerRedirect = { + destination: '/ie-incompatible.html', + has: [ + { + type: 'header', + key: 'user-agent', + value: '(.*Trident.*)', // all ie browsers + }, + ], + permanent: false, + source: '/:path((?!ie-incompatible.html$).*)', // all pages except the incompatibility page + } + + const redirects = [internetExplorerRedirect] + + return redirects +} + +export default redirects diff --git a/playground/src/Footer/Component.tsx b/playground/src/Footer/Component.tsx new file mode 100644 index 0000000..00d5d5e --- /dev/null +++ b/playground/src/Footer/Component.tsx @@ -0,0 +1,39 @@ +import { getCachedGlobal } from '../utilities/getGlobals' +import Link from 'next/link' +import React from 'react' + +import type { Footer } from '../payload-types' + +import { ThemeSelector } from '../providers/Theme/ThemeSelector' +import { CMSLink } from '../components/Link' + +export async function Footer() { + const footer: Footer = await getCachedGlobal('footer')() + + const navItems = footer?.navItems || [] + + return ( +
+
+ + + Payload Logo + + + +
+ + +
+
+
+ ) +} diff --git a/playground/src/Footer/config.ts b/playground/src/Footer/config.ts new file mode 100644 index 0000000..5813a7e --- /dev/null +++ b/playground/src/Footer/config.ts @@ -0,0 +1,26 @@ +import type { GlobalConfig } from 'payload' + +import { link } from '../fields/link' +import { revalidateFooter } from './hooks/revalidateFooter' + +export const Footer: GlobalConfig = { + slug: 'footer', + access: { + read: () => true, + }, + fields: [ + { + name: 'navItems', + type: 'array', + fields: [ + link({ + appearances: false, + }), + ], + maxRows: 6, + }, + ], + hooks: { + afterChange: [revalidateFooter], + }, +} diff --git a/playground/src/Footer/hooks/revalidateFooter.ts b/playground/src/Footer/hooks/revalidateFooter.ts new file mode 100644 index 0000000..f6267c0 --- /dev/null +++ b/playground/src/Footer/hooks/revalidateFooter.ts @@ -0,0 +1,11 @@ +import type { GlobalAfterChangeHook } from 'payload' + +import { revalidateTag } from 'next/cache' + +export const revalidateFooter: GlobalAfterChangeHook = ({ doc, req: { payload } }) => { + payload.logger.info(`Revalidating footer`) + + revalidateTag('global_footer') + + return doc +} diff --git a/playground/src/Header/Component.client.tsx b/playground/src/Header/Component.client.tsx new file mode 100644 index 0000000..b4c2518 --- /dev/null +++ b/playground/src/Header/Component.client.tsx @@ -0,0 +1,43 @@ +'use client' +import { useHeaderTheme } from '../providers/HeaderTheme' +import Link from 'next/link' +import { usePathname } from 'next/navigation' +import React, { useEffect, useState } from 'react' + +import type { Header } from '../payload-types' + +import { Logo } from '../components/Logo/Logo' +import { HeaderNav } from './Nav' + +interface HeaderClientProps { + header: Header +} + +export const HeaderClient: React.FC = ({ header }) => { + /* Storing the value in a useState to avoid hydration errors */ + const [theme, setTheme] = useState(null) + const { headerTheme, setHeaderTheme } = useHeaderTheme() + const pathname = usePathname() + + useEffect(() => { + setHeaderTheme(null) + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [pathname]) + + useEffect(() => { + if (headerTheme && headerTheme !== theme) setTheme(headerTheme) + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [headerTheme]) + + return ( +
+ + + + +
+ ) +} diff --git a/playground/src/Header/Component.tsx b/playground/src/Header/Component.tsx new file mode 100644 index 0000000..e73e570 --- /dev/null +++ b/playground/src/Header/Component.tsx @@ -0,0 +1,11 @@ +import { HeaderClient } from './Component.client' +import { getCachedGlobal } from '../utilities/getGlobals' +import React from 'react' + +import type { Header } from '../payload-types' + +export async function Header() { + const header: Header = await getCachedGlobal('header', 1)() + + return +} diff --git a/playground/src/Header/Nav/index.tsx b/playground/src/Header/Nav/index.tsx new file mode 100644 index 0000000..8aa8922 --- /dev/null +++ b/playground/src/Header/Nav/index.tsx @@ -0,0 +1,19 @@ +'use client' + +import React from 'react' + +import type { Header as HeaderType } from '../../payload-types' + +import { CMSLink } from '../../components/Link' + +export const HeaderNav: React.FC<{ header: HeaderType }> = ({ header }) => { + const navItems = header?.navItems || [] + + return ( + + ) +} diff --git a/playground/src/Header/config.ts b/playground/src/Header/config.ts new file mode 100644 index 0000000..918666a --- /dev/null +++ b/playground/src/Header/config.ts @@ -0,0 +1,26 @@ +import type { GlobalConfig } from 'payload' + +import { link } from '../fields/link' +import { revalidateHeader } from './hooks/revalidateHeader' + +export const Header: GlobalConfig = { + slug: 'header', + access: { + read: () => true, + }, + fields: [ + { + name: 'navItems', + type: 'array', + fields: [ + link({ + appearances: false, + }), + ], + maxRows: 6, + }, + ], + hooks: { + afterChange: [revalidateHeader], + }, +} diff --git a/playground/src/Header/hooks/revalidateHeader.ts b/playground/src/Header/hooks/revalidateHeader.ts new file mode 100644 index 0000000..7315cc2 --- /dev/null +++ b/playground/src/Header/hooks/revalidateHeader.ts @@ -0,0 +1,11 @@ +import type { GlobalAfterChangeHook } from 'payload' + +import { revalidateTag } from 'next/cache' + +export const revalidateHeader: GlobalAfterChangeHook = ({ doc, req: { payload } }) => { + payload.logger.info(`Revalidating header`) + + revalidateTag('global_header') + + return doc +} diff --git a/playground/src/access/anyone.ts b/playground/src/access/anyone.ts new file mode 100644 index 0000000..bf37c3a --- /dev/null +++ b/playground/src/access/anyone.ts @@ -0,0 +1,3 @@ +import type { Access } from 'payload' + +export const anyone: Access = () => true diff --git a/playground/src/access/authenticated.ts b/playground/src/access/authenticated.ts new file mode 100644 index 0000000..752ac55 --- /dev/null +++ b/playground/src/access/authenticated.ts @@ -0,0 +1,9 @@ +import type { AccessArgs } from 'payload' + +import type { User } from '../payload-types' + +type isAuthenticated = (args: AccessArgs) => boolean + +export const authenticated: isAuthenticated = ({ req: { user } }) => { + return Boolean(user) +} diff --git a/playground/src/access/authenticatedOrPublished.ts b/playground/src/access/authenticatedOrPublished.ts new file mode 100644 index 0000000..e49198f --- /dev/null +++ b/playground/src/access/authenticatedOrPublished.ts @@ -0,0 +1,13 @@ +import type { Access } from 'payload' + +export const authenticatedOrPublished: Access = ({ req: { user } }) => { + if (user) { + return true + } + + return { + _status: { + equals: 'published', + }, + } +} diff --git a/playground/src/app/(frontend)/[slug]/page.tsx b/playground/src/app/(frontend)/[slug]/page.tsx new file mode 100644 index 0000000..4d02f05 --- /dev/null +++ b/playground/src/app/(frontend)/[slug]/page.tsx @@ -0,0 +1,89 @@ +import type { Metadata } from 'next' + +import { PayloadRedirects } from '../../../components/PayloadRedirects' +import configPromise from '@payload-config' +import { getPayloadHMR } from '@payloadcms/next/utilities' +import { draftMode } from 'next/headers' +import React, { cache } from 'react' +import { homeStatic } from '../../../endpoints/seed/home-static' + +import type { Page as PageType } from '../../../payload-types' + +import { RenderBlocks } from '../../../blocks/RenderBlocks' +import { RenderHero } from '../../../heros/RenderHero' +import { generateMeta } from '../../../utilities/generateMeta' + +export async function generateStaticParams() { + const payload = await getPayloadHMR({ config: configPromise }) + const pages = await payload.find({ + collection: 'pages', + draft: false, + limit: 1000, + overrideAccess: false, + }) + + return pages.docs + ?.filter((doc) => { + return doc.slug !== 'home' + }) + .map(({ slug }) => slug) +} + +export default async function Page({ params: { slug = 'home' } }) { + const url = '/' + slug + + let page: PageType | null + + page = await queryPageBySlug({ + slug, + }) + + // Remove this code once your website is seeded + if (!page) { + page = homeStatic + } + + if (!page) { + return + } + + const { hero, layout } = page + + return ( +
+ {/* Allows redirects for valid pages too */} + + + + +
+ ) +} + +export async function generateMetadata({ params: { slug = 'home' } }): Promise { + const page = await queryPageBySlug({ + slug, + }) + + return generateMeta({ doc: page }) +} + +const queryPageBySlug = cache(async ({ slug }: { slug: string }) => { + const { isEnabled: draft } = draftMode() + + const payload = await getPayloadHMR({ config: configPromise }) + + const result = await payload.find({ + collection: 'pages', + draft, + limit: 1, + overrideAccess: true, + where: { + slug: { + equals: slug, + }, + }, + }) + + return result.docs?.[0] || null +}) diff --git a/playground/src/app/(frontend)/globals.css b/playground/src/app/(frontend)/globals.css new file mode 100644 index 0000000..13be9b7 --- /dev/null +++ b/playground/src/app/(frontend)/globals.css @@ -0,0 +1,103 @@ +@tailwind base; +@tailwind components; +@tailwind utilities; + +@layer base { + h1, + h2, + h3, + h4, + h5, + h6 { + font-size: auto; + font-weight: auto; + } + + :root { + --background: 0 0% 100%; + --foreground: 222.2 84% 4.9%; + + --card: 240 5% 96%; + --card-foreground: 222.2 84% 4.9%; + + --popover: 0 0% 100%; + --popover-foreground: 222.2 84% 4.9%; + + --primary: 222.2 47.4% 11.2%; + --primary-foreground: 210 40% 98%; + + --secondary: 210 40% 96.1%; + --secondary-foreground: 222.2 47.4% 11.2%; + + --muted: 210 40% 96.1%; + --muted-foreground: 215.4 16.3% 46.9%; + + --accent: 210 40% 96.1%; + --accent-foreground: 222.2 47.4% 11.2%; + + --destructive: 0 84.2% 60.2%; + --destructive-foreground: 210 40% 98%; + + --border: 240 6% 90%; + --input: 214.3 31.8% 91.4%; + --ring: 222.2 84% 4.9%; + + --radius: 0.2rem; + + --success: 196 52% 74%; + --warning: 34 89% 85%; + --error: 10 100% 86%; + } + + [data-theme='dark'] { + --background: 0 0% 0%; + --foreground: 210 40% 98%; + + --card: 240 6% 10%; + --card-foreground: 210 40% 98%; + + --popover: 222.2 84% 4.9%; + --popover-foreground: 210 40% 98%; + + --primary: 210 40% 98%; + --primary-foreground: 222.2 47.4% 11.2%; + + --secondary: 217.2 32.6% 17.5%; + --secondary-foreground: 210 40% 98%; + + --muted: 217.2 32.6% 17.5%; + --muted-foreground: 215 20.2% 65.1%; + + --accent: 217.2 32.6% 17.5%; + --accent-foreground: 210 40% 98%; + + --destructive: 0 62.8% 30.6%; + --destructive-foreground: 210 40% 98%; + + --border: 240 4% 16%; + --input: 217.2 32.6% 17.5%; + --ring: 212.7 26.8% 83.9%; + + --success: 196 100% 14%; + --warning: 34 51% 25%; + --error: 10 39% 43%; + } +} + +@layer base { + * { + @apply border-border; + } + body { + @apply bg-background text-foreground; + } +} + +html { + opacity: 0; +} + +html[data-theme='dark'], +html[data-theme='light'] { + opacity: initial; +} diff --git a/playground/src/app/(frontend)/layout.tsx b/playground/src/app/(frontend)/layout.tsx new file mode 100644 index 0000000..cff2fbf --- /dev/null +++ b/playground/src/app/(frontend)/layout.tsx @@ -0,0 +1,53 @@ +import type { Metadata } from 'next' + +import { cn } from '../../utilities/cn' +import { GeistMono } from 'geist/font/mono' +import { GeistSans } from 'geist/font/sans' +import React from 'react' + +import { AdminBar } from '../../components/AdminBar' +import { Footer } from '../../Footer/Component' +import { Header } from '../../Header/Component' +import { LivePreviewListener } from '../../components/LivePreviewListener' +import { Providers } from '../../providers' +import { InitTheme } from '../../providers/Theme/InitTheme' +import { mergeOpenGraph } from '../../utilities/mergeOpenGraph' +import './globals.css' +import { draftMode } from 'next/headers' + +export default async function RootLayout({ children }: { children: React.ReactNode }) { + const { isEnabled } = draftMode() + + return ( + + + + + + + + + + + +
+ {children} +