Skip to content

Commit 6da46ef

Browse files
committed
Merge branch 'test-keystatic'
2 parents 4765f0e + 32ca10f commit 6da46ef

36 files changed

Lines changed: 7159 additions & 1048 deletions

‎.env.example‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Use local file storage when these values are absent.
2+
# Set them on a Node.js-capable deployment to edit the GitHub repository online.
3+
NEXT_PUBLIC_KEYSTATIC_STORAGE_KIND=github
4+
NEXT_OUTPUT_MODE=server
5+
KEYSTATIC_GITHUB_CLIENT_ID=
6+
KEYSTATIC_GITHUB_CLIENT_SECRET=
7+
KEYSTATIC_SECRET=
8+
NEXT_PUBLIC_KEYSTATIC_GITHUB_APP_SLUG=

‎.github/workflows/nextjs.yml‎

Lines changed: 21 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# Workflow for building and deploying a Next.js site to Cloudflare Pages
2-
name: Deploy Next.js site to Pages
1+
# Workflow for building and deploying Next.js full-stack site to Cloudflare Workers
2+
name: Deploy Next.js site to Cloudflare Workers
33

44
on:
55
# Runs on pushes targeting the default branch
@@ -12,10 +12,6 @@ on:
1212
- "scripts/crawl-team.ts"
1313
- ".github/workflows/update-team-photos.yml"
1414

15-
# Run on pull requests targeting main to build preview deployments
16-
pull_request:
17-
branches: ["main"]
18-
1915
# Allows you to run this workflow manually from the Actions tab
2016
workflow_dispatch:
2117

@@ -25,71 +21,42 @@ on:
2521
types:
2622
- completed
2723

28-
# Sets permissions of the GITHUB_TOKEN to allow deployment status updates
2924
permissions:
3025
contents: read
31-
deployments: write
3226

33-
# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
34-
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
3527
concurrency:
36-
group: "pages"
28+
group: "production-worker"
3729
cancel-in-progress: false
3830

3931
jobs:
4032
build-and-deploy:
4133
runs-on: ubuntu-latest
4234
steps:
4335
- name: Checkout
44-
uses: actions/checkout@v6
45-
- name: Detect package manager
46-
id: detect-package-manager
47-
run: |
48-
if [ -f "${{ github.workspace }}/yarn.lock" ]; then
49-
echo "manager=yarn" >> $GITHUB_OUTPUT
50-
echo "command=install" >> $GITHUB_OUTPUT
51-
echo "runner=yarn" >> $GITHUB_OUTPUT
52-
exit 0
53-
elif [ -f "${{ github.workspace }}/pnpm-lock.yaml" ]; then
54-
echo "manager=pnpm" >> $GITHUB_OUTPUT
55-
echo "command=install --frozen-lockfile" >> $GITHUB_OUTPUT
56-
echo "runner=pnpm" >> $GITHUB_OUTPUT
57-
exit 0
58-
elif [ -f "${{ github.workspace }}/package.json" ]; then
59-
echo "manager=npm" >> $GITHUB_OUTPUT
60-
echo "command=ci" >> $GITHUB_OUTPUT
61-
echo "runner=npx --no-install" >> $GITHUB_OUTPUT
62-
exit 0
63-
else
64-
echo "Unable to determine package manager"
65-
exit 1
66-
fi
36+
uses: actions/checkout@v7
37+
6738
- name: Setup pnpm
6839
uses: pnpm/action-setup@v6
69-
if: steps.detect-package-manager.outputs.manager == 'pnpm'
40+
7041
- name: Setup Node
71-
uses: actions/setup-node@v6
42+
uses: actions/setup-node@v7
7243
with:
7344
node-version: "22"
74-
cache: ${{ steps.detect-package-manager.outputs.manager }}
45+
cache: "pnpm"
7546

76-
- name: Restore cache
77-
uses: actions/cache@v5
78-
with:
79-
path: |
80-
.next/cache
81-
# Generate a new cache whenever packages or source files change.
82-
key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json', '**/yarn.lock', '**/pnpm-lock.yaml') }}-${{ hashFiles('**.[jt]s', '**.[jt]sx') }}
83-
# If source files changed but packages didn't, rebuild from a prior cache.
84-
restore-keys: |
85-
${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json', '**/yarn.lock', '**/pnpm-lock.yaml') }}-
8647
- name: Install dependencies
87-
run: ${{ steps.detect-package-manager.outputs.manager }} ${{ steps.detect-package-manager.outputs.command }}
88-
- name: Build with Next.js
89-
run: ${{ steps.detect-package-manager.outputs.manager }} run build
90-
- name: Deploy to Cloudflare Pages
91-
id: cloudflare-deploy
48+
run: pnpm install --frozen-lockfile
49+
50+
- name: Lint
51+
run: pnpm lint
52+
53+
- name: Build and deploy to Cloudflare Workers
9254
env:
93-
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
9455
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
95-
run: npx wrangler pages deploy ./out --project-name=preferredai --branch=${{ github.head_ref || github.ref_name }}
56+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_WORKERS_API_TOKEN || secrets.CLOUDFLARE_API_TOKEN }}
57+
NEXT_PUBLIC_KEYSTATIC_STORAGE_KIND: ${{ vars.NEXT_PUBLIC_KEYSTATIC_STORAGE_KIND }}
58+
KEYSTATIC_GITHUB_CLIENT_ID: ${{ secrets.KEYSTATIC_GITHUB_CLIENT_ID }}
59+
KEYSTATIC_GITHUB_CLIENT_SECRET: ${{ secrets.KEYSTATIC_GITHUB_CLIENT_SECRET }}
60+
KEYSTATIC_SECRET: ${{ secrets.KEYSTATIC_SECRET }}
61+
NEXT_PUBLIC_KEYSTATIC_GITHUB_APP_SLUG: ${{ vars.NEXT_PUBLIC_KEYSTATIC_GITHUB_APP_SLUG }}
62+
run: pnpm deploy:worker

‎.github/workflows/test-build.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ jobs:
3131
- name: Lint
3232
run: pnpm lint
3333

34-
- name: Build with Next.js
35-
run: pnpm build
34+
- name: Build for Cloudflare Workers
35+
run: pnpm build:worker

‎.gitignore‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
# dependencies
44
/node_modules
5+
/.pnpm-store/
56
/.pnp
67
.pnp.*
78
.yarn/*
@@ -16,6 +17,8 @@
1617
# next.js
1718
/.next/
1819
/out/
20+
/.open-next/
21+
/.wrangler/
1922

2023
# production
2124
/build
@@ -32,13 +35,16 @@ yarn-error.log*
3235

3336
# env files (can opt-in for committing if needed)
3437
.env*
38+
!.env.example
39+
.dev.vars*
3540

3641
# vercel
3742
.vercel
3843

3944
# typescript
4045
*.tsbuildinfo
4146
next-env.d.ts
47+
cloudflare-env.d.ts
4248

4349
/src/generated/prisma
4450
/public/optimized/

‎README.md‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,56 @@ Visit [http://localhost:3000](http://localhost:3000)
3333

3434
## Creating a Blog Post
3535

36+
### Admin UI (Recommended)
37+
38+
1. Open `/admin` (it redirects to Keystatic's required `/keystatic` route).
39+
2. Open **Posts** and choose **Create post**.
40+
3. Enter the title first. It becomes the post slug and its image-folder name.
41+
4. Choose or drop a featured image into **Featured image**.
42+
5. In **Content**, use the image toolbar button, paste an image, or drag and drop it into the editor.
43+
6. Add useful alt text, preview the post, and save.
44+
45+
New images are committed with the post and organized automatically:
46+
47+
```text
48+
content/posts/my-new-post.md
49+
public/uploads/my-new-post/cover.jpg
50+
public/uploads/my-new-post/diagram-one.png
51+
```
52+
53+
Existing posts keep their legacy `/uploads/YYYY/MM/...` image paths. The **Existing featured image path (legacy)** field is only for those posts; leave it empty when creating a new post.
54+
55+
For local authoring, run `pnpm dev` and visit [http://localhost:3000/admin](http://localhost:3000/admin).
56+
57+
### Online Admin Setup (No Local `pnpm dev`)
58+
59+
Keystatic writes Markdown and images back to GitHub through server-side API routes. A static Cloudflare Pages export cannot run the admin API, so the hosted editor is deployed as a full-stack Next.js Worker through OpenNext. Set:
60+
61+
```dotenv
62+
NEXT_PUBLIC_KEYSTATIC_STORAGE_KIND=github
63+
NEXT_OUTPUT_MODE=server
64+
KEYSTATIC_GITHUB_CLIENT_ID=...
65+
KEYSTATIC_GITHUB_CLIENT_SECRET=...
66+
KEYSTATIC_SECRET=...
67+
NEXT_PUBLIC_KEYSTATIC_GITHUB_APP_SLUG=...
68+
```
69+
70+
Before the GitHub App exists, start Keystatic's one-time setup mode with
71+
`NEXT_PUBLIC_KEYSTATIC_STORAGE_KIND=github pnpm dev`, then open
72+
[http://127.0.0.1:3000/keystatic/setup](http://127.0.0.1:3000/keystatic/setup).
73+
The storage-mode flag is public because both the browser UI and server route
74+
must select GitHub storage; it contains no credential or secret.
75+
76+
Copy `.env.example` as a checklist. The GitHub App values can be generated from Keystatic's setup screen. Give the app access only to `PreferredAI/PreferredAI.github.io`; authors must have repository write access. A repository administrator can normally install Keystatic's repository-scoped app unless the organization has restricted installations to owners.
77+
78+
Run `pnpm build:worker` to produce the Cloudflare-compatible server build and `pnpm preview:worker` to test it locally. Keystatic requires server-side routes; a static export cannot provide its API or GitHub OAuth callbacks.
79+
80+
The `test-keystatic` branch deploys to the separate `preferredai-keystatic` Worker through `.github/workflows/deploy-keystatic-test.yml`. This leaves the existing Pages deployment and `preferred.ai` unchanged during testing.
81+
82+
The test workflow uses a separate `CLOUDFLARE_WORKERS_API_TOKEN` Actions secret. Do not replace the existing `CLOUDFLARE_API_TOKEN`, which remains scoped to the production Pages workflow.
83+
84+
Do not merge the Worker deployment into the production workflow or move the custom domain until the temporary Worker passes a complete GitHub login, edit, image upload, save, and rebuild test.
85+
3686
### Via GitHub Codespaces (Cloud IDE - Preview Available)
3787

3888
1. Open the project in Codespaces (see above)
@@ -61,7 +111,7 @@ Visit [http://localhost:3000](http://localhost:3000)
61111
seoDescription: "Brief summary for SEO"
62112
---
63113
```
64-
6. Write your content in Markdown. Upload images to `public/uploads/YYYY/MM/` first if needed.
114+
6. Write your content in Markdown. Upload images to `public/uploads/YYYY/MM/` first if needed. (The Admin UI above handles this automatically for new posts.)
65115
7. Commit to a **new branch** and create a **Pull Request**
66116

67117
### Local Development (Requires Installation Locally - Preview Available)

‎keystatic.config.ts‎

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import { collection, config, fields } from "@keystatic/core";
2+
3+
const isGitHubStorage =
4+
process.env.NEXT_PUBLIC_KEYSTATIC_STORAGE_KIND === "github" ||
5+
Boolean(process.env.NEXT_PUBLIC_KEYSTATIC_GITHUB_APP_SLUG);
6+
7+
const categoryOptions = [
8+
"Announcement",
9+
"Defense",
10+
"Education",
11+
"Presentation",
12+
"Publication",
13+
"Social",
14+
"Travel",
15+
"Video",
16+
].map((category) => ({ label: category, value: category }));
17+
18+
function sanitizeAssetFilename(originalFilename: string): string {
19+
const extensionIndex = originalFilename.lastIndexOf(".");
20+
const hasExtension = extensionIndex > 0;
21+
const basename = hasExtension
22+
? originalFilename.slice(0, extensionIndex)
23+
: originalFilename;
24+
const extension = hasExtension
25+
? originalFilename.slice(extensionIndex).toLowerCase()
26+
: "";
27+
const safeBasename = basename
28+
.normalize("NFKD")
29+
.replace(/[\u0300-\u036f]/g, "")
30+
.toLowerCase()
31+
.replace(/[^a-z0-9]+/g, "-")
32+
.replace(/^-+|-+$/g, "");
33+
34+
return `${safeBasename || "image"}${extension}`;
35+
}
36+
37+
export default config({
38+
storage: isGitHubStorage
39+
? {
40+
kind: "github",
41+
repo: "PreferredAI/PreferredAI.github.io",
42+
}
43+
: {
44+
kind: "local",
45+
},
46+
ui: {
47+
brand: { name: "Preferred.AI Publisher" },
48+
navigation: ["posts"],
49+
},
50+
collections: {
51+
posts: collection({
52+
label: "Posts",
53+
slugField: "title",
54+
path: "content/posts/*",
55+
format: { contentField: "content" },
56+
entryLayout: "content",
57+
columns: ["title", "date", "author"],
58+
previewUrl: "/blog/{slug}",
59+
schema: {
60+
title: fields.slug({
61+
name: {
62+
label: "Title",
63+
validation: { isRequired: true },
64+
},
65+
}),
66+
date: fields.date({
67+
label: "Publish date",
68+
defaultValue: { kind: "today" },
69+
validation: { isRequired: true },
70+
}),
71+
author: fields.text({
72+
label: "Author",
73+
defaultValue: "Preferred.AI",
74+
validation: { isRequired: true },
75+
}),
76+
excerpt: fields.text({
77+
label: "Excerpt",
78+
multiline: true,
79+
description:
80+
"A one or two sentence summary used in post listings and as the default SEO description.",
81+
validation: { isRequired: true },
82+
}),
83+
cover: fields.image({
84+
label: "Featured image",
85+
description:
86+
"Choose or drop an image. Keystatic stores it in public/uploads/<post-slug>/cover.<ext>.",
87+
directory: "public/uploads",
88+
publicPath: "/uploads",
89+
}),
90+
featuredImage: fields.text({
91+
label: "Existing featured image path (legacy)",
92+
description:
93+
"Existing posts may use an /uploads/YYYY/MM/... path. Leave this empty for new posts and use the image picker above.",
94+
}),
95+
categories: fields.multiselect({
96+
label: "Categories",
97+
description: "Choose every category that applies.",
98+
options: categoryOptions,
99+
defaultValue: ["Education"],
100+
}),
101+
tags: fields.array(fields.text({ label: "Tag" }), {
102+
label: "Tags",
103+
description: "Optional keywords for this post.",
104+
itemLabel: (props) => props.value,
105+
}),
106+
seoTitle: fields.text({
107+
label: "SEO Title",
108+
description: "Optional. Defaults to the post title when left empty.",
109+
}),
110+
seoDescription: fields.text({
111+
label: "SEO Description",
112+
multiline: true,
113+
description: "Optional. Defaults to the excerpt when left empty.",
114+
}),
115+
content: fields.markdoc({
116+
label: "Content",
117+
description:
118+
"Use the image toolbar button, paste, or drag and drop. Add meaningful alt text when prompted.",
119+
extension: "md",
120+
options: {
121+
image: {
122+
directory: "public/uploads",
123+
publicPath: "/uploads",
124+
transformFilename: sanitizeAssetFilename,
125+
},
126+
},
127+
}),
128+
},
129+
}),
130+
},
131+
});

‎next.config.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
import { initOpenNextCloudflareForDev } from "@opennextjs/cloudflare";
12
import type { NextConfig } from "next";
23

4+
const isServerBuild = process.env.NEXT_OUTPUT_MODE === "server";
5+
36
const nextConfig: NextConfig = {
4-
output: "export",
7+
agentRules: false,
8+
allowedDevOrigins: ["127.0.0.1"],
9+
...(process.env.NODE_ENV === "production" && !isServerBuild
10+
? { output: "export" }
11+
: {}),
512
images: {
613
loader: "custom",
714
loaderFile: "./src/lib/image-loader.ts",
@@ -10,4 +17,6 @@ const nextConfig: NextConfig = {
1017
},
1118
};
1219

20+
initOpenNextCloudflareForDev();
21+
1322
export default nextConfig;

‎open-next.config.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { defineCloudflareConfig } from "@opennextjs/cloudflare";
2+
3+
export default defineCloudflareConfig();

0 commit comments

Comments
 (0)