Skip to content

Commit 7e99f43

Browse files
Merge pull request #105 from devweekends/chore/cloudflare-workers
feat(hosting): move the site from Vercel to Cloudflare Workers
2 parents 278d356 + f7de835 commit 7e99f43

47 files changed

Lines changed: 6008 additions & 1827 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.env.example‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,17 @@ NODE_ENV=development
5757
# without it /feed still shows existing posts and accepts submissions)
5858
OLOSTEP_API_KEY=
5959

60-
# Secret Vercel sends to the daily cron job (vercel.json). Generate: openssl rand -base64 32
60+
# Secret the Worker's daily cron sends to /api/cron/linkedin-feed (worker.ts). Generate: openssl rand -base64 32
6161
CRON_SECRET=
6262

6363
# Olostep searches per run, each costs credits (default 5)
6464
# FEED_COLLECT_QUERIES=5
65+
66+
# -----------------------------------------------------------------------------
67+
# Hosting (Cloudflare Workers)
68+
# -----------------------------------------------------------------------------
69+
# Database for the fellowship impact stories and survey
70+
# IMPACT_MONGODB_URI
71+
72+
# Cloudflare account to deploy to with `npm run deploy` (local deploys only)
73+
# CLOUDFLARE_ACCOUNT_ID

‎.gitignore‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,10 @@ coverage/
166166

167167
# Claude Code personal settings (local permission allowlists)
168168
.claude/settings.local.json
169+
170+
# Cloudflare Workers (OpenNext build output, local secrets, generated bundle)
171+
.open-next
172+
.wrangler
173+
.dev.vars*
174+
!.dev.vars.example
175+
lib/generated/

‎app/agent-markdown/route.ts‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import fs from 'fs'
2-
import path from 'path'
31
import matter from 'gray-matter'
42
import { NextRequest, NextResponse } from 'next/server'
53

64
import { htmlToMarkdown, pageFacts } from '@/lib/html-to-markdown'
75
import { SITE_NAME, SITE_URL } from '@/lib/seo'
6+
import { readContent } from '@/lib/site-files'
87

98
/**
109
* The Markdown version of a public page, for AI agents. The middleware sends
@@ -47,9 +46,9 @@ function fromContentFile(sourcePath: string): string | undefined {
4746
const match = sourcePath.match(/^\/(blog|guides)\/([a-z0-9-]+)$/)
4847
if (!match) return undefined
4948
const [, folder, slug] = match
50-
const file = path.join(process.cwd(), 'content', folder, `${slug}.md`)
51-
if (!fs.existsSync(file)) return undefined
52-
const { data, content } = matter(fs.readFileSync(file, 'utf8'))
49+
const raw = readContent(folder as 'blog' | 'guides', slug)
50+
if (raw === undefined) return undefined
51+
const { data, content } = matter(raw)
5352
if (data.draft === true) return undefined
5453
const head = frontMatter({ title: data.title, description: data.description, url: `${SITE_URL}${sourcePath}` })
5554
return `${head}# ${data.title}\n\n${content.trim()}\n`
@@ -68,7 +67,7 @@ export async function GET(request: NextRequest) {
6867
if (fromFile) return respond(fromFile)
6968

7069
// In production the page always comes from the public address, whatever host the request named
71-
const origin = process.env.VERCEL_ENV === 'production' ? SITE_URL : request.nextUrl.origin
70+
const origin = process.env.SITE_ENV === 'production' ? SITE_URL : request.nextUrl.origin
7271
let html: string
7372
try {
7473
const page = await fetch(`${origin}${sourcePath}`, { headers: { Accept: 'text/html' }, redirect: 'follow', cache: 'no-store' })

‎app/api/cron/linkedin-feed/route.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ export const dynamic = 'force-dynamic';
66
export const maxDuration = 300;
77

88
/**
9-
* Daily Vercel cron (see vercel.json): discovers new public #DevWeekends LinkedIn
10-
* posts through Olostep. Vercel sends `Authorization: Bearer $CRON_SECRET`.
9+
* Daily cron (the Worker's scheduled handler, see worker.ts and wrangler.jsonc):
10+
* discovers new public #DevWeekends LinkedIn posts through Olostep. The handler
11+
* sends `Authorization: Bearer $CRON_SECRET`.
1112
* Each search costs Olostep credits; FEED_COLLECT_QUERIES sets how many run.
1213
*/
1314
export async function GET(request: Request) {

‎app/blog/[slug]/page.tsx‎

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import fs from "fs";
2-
import path from "path";
31
import matter from "gray-matter";
42
import type { Metadata } from "next";
53
import Image from "next/image";
64
import Link from "next/link";
75
import { ArticleBody } from "@/components/content/article-body";
86
import { formatDate } from "@/components/content/format-date";
97
import { Button, Eyebrow, container } from "@/components/site/page-kit";
8+
import { contentSlugs, readContent } from "@/lib/site-files";
109
import { SITE_NAME, blogPostingJsonLd, breadcrumbJsonLd, excerptOf, jsonLdString, pageTitle, snippetOf, withSocial } from "@/lib/seo";
1110

1211
const backLink =
@@ -17,12 +16,7 @@ const backLink =
1716
export const dynamicParams = false;
1817

1918
export function generateStaticParams() {
20-
const dir = path.join(process.cwd(), "content", "blog");
21-
if (!fs.existsSync(dir)) return [];
22-
return fs
23-
.readdirSync(dir)
24-
.filter((f) => f.endsWith(".md"))
25-
.map((f) => ({ slug: f.replace(/\.md$/, "") }));
19+
return contentSlugs("blog").map((slug) => ({ slug }));
2620
}
2721

2822
export async function generateMetadata({
@@ -31,9 +25,9 @@ export async function generateMetadata({
3125
params: Promise<{ slug: string }>;
3226
}): Promise<Metadata> {
3327
const { slug } = await params;
34-
const filePath = path.join(process.cwd(), "content", "blog", `${slug}.md`);
35-
if (!fs.existsSync(filePath)) return { title: "Post not found", robots: { index: false } };
36-
const { data, content } = matter(fs.readFileSync(filePath, "utf-8"));
28+
const raw = readContent("blog", slug);
29+
if (raw === undefined) return { title: "Post not found", robots: { index: false } };
30+
const { data, content } = matter(raw);
3731
const date = data.date ? new Date(data.date) : undefined;
3832
const description = snippetOf(data.description) ?? excerptOf(content);
3933
return withSocial({
@@ -62,14 +56,9 @@ export default async function BlogPost({
6256
}) {
6357
const { slug } = await params;
6458

65-
const filePath = path.join(
66-
process.cwd(),
67-
"content",
68-
"blog",
69-
`${slug}.md`
70-
);
59+
const fileContent = readContent("blog", slug);
7160

72-
if (!fs.existsSync(filePath)) {
61+
if (fileContent === undefined) {
7362
return (
7463
<main className="bg-white">
7564
<div className={`${container} py-24 md:py-32`}>
@@ -88,7 +77,6 @@ export default async function BlogPost({
8877
);
8978
}
9079

91-
const fileContent = fs.readFileSync(filePath, "utf-8");
9280
const { data, content } = matter(fileContent);
9381
const date = formatDate(data.date instanceof Date ? data.date.toISOString() : data.date);
9482

‎app/blog/page.tsx‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import fs from "fs";
2-
import path from "path";
31
import matter from "gray-matter";
42
import BlogClient from "./blogClient";
53
import { withSocial } from "@/lib/seo";
4+
import { contentSlugs, readContent } from "@/lib/site-files";
65

76
export const metadata = withSocial({
87
title: "News, guides and blog",
@@ -31,12 +30,8 @@ export type Post = {
3130
const PORTRAIT_POSTER_SLUGS = new Set(["gsoc"]);
3231

3332
function readFolder(folder: "blog" | "guides") {
34-
const dir = path.join(process.cwd(), "content", folder);
3533
try {
36-
return fs
37-
.readdirSync(dir)
38-
.filter((f) => f.endsWith(".md"))
39-
.map((file) => ({ slug: file.replace(/\.md$/, ""), data: matter(fs.readFileSync(path.join(dir, file), "utf-8")).data }));
34+
return contentSlugs(folder).map((slug) => ({ slug, data: matter(readContent(folder, slug) ?? "").data }));
4035
} catch (error) {
4136
console.error(`Could not read ${folder}:`, error);
4237
return [];

‎app/guides/[slug]/page.tsx‎

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import fs from "fs";
2-
import path from "path";
31
import matter from "gray-matter";
42
import Link from "next/link";
53
import type { Metadata } from "next";
@@ -8,13 +6,11 @@ import { formatDate } from "@/components/content/format-date";
86
import { Button, Eyebrow, container } from "@/components/site/page-kit";
97
import { blogPostingJsonLd, breadcrumbJsonLd, jsonLdString, pageTitle, snippetOf, withSocial } from "@/lib/seo";
108
import { svgSize } from "@/lib/svg-size";
11-
12-
const guidesDir = path.join(process.cwd(), "content", "guides");
9+
import { contentSlugs, readContent } from "@/lib/site-files";
1310

1411
function readGuide(slug: string) {
15-
const filePath = path.join(guidesDir, `${slug}.md`);
16-
if (!fs.existsSync(filePath)) return null;
17-
const fileContent = fs.readFileSync(filePath, "utf-8");
12+
const fileContent = readContent("guides", slug);
13+
if (fileContent === undefined) return null;
1814
return matter(fileContent);
1915
}
2016

@@ -23,11 +19,7 @@ function readGuide(slug: string) {
2319
export const dynamicParams = false;
2420

2521
export function generateStaticParams() {
26-
if (!fs.existsSync(guidesDir)) return [];
27-
return fs
28-
.readdirSync(guidesDir)
29-
.filter((f) => f.endsWith(".md"))
30-
.map((f) => ({ slug: f.replace(/\.md$/, "") }));
22+
return contentSlugs("guides").map((slug) => ({ slug }));
3123
}
3224

3325
export async function generateMetadata({

‎app/guides/page.tsx‎

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import fs from "fs";
2-
import path from "path";
31
import matter from "gray-matter";
42
import GuidesClient from "./guidesClient";
53
import { withSocial } from "@/lib/seo";
4+
import { contentSlugs, readContent } from "@/lib/site-files";
65

76
export const metadata = withSocial({
87
alternates: { canonical: "/guides" },
@@ -34,22 +33,8 @@ export const GUIDE_CATEGORIES = [
3433
] as const;
3534

3635
export default function GuidesPage() {
37-
const guidesDir = path.join(process.cwd(), "content", "guides");
38-
39-
let files: string[] = [];
40-
try {
41-
if (fs.existsSync(guidesDir)) {
42-
files = fs.readdirSync(guidesDir).filter((f) => f.endsWith(".md"));
43-
}
44-
} catch (error) {
45-
console.error("Could not read guides:", error);
46-
}
47-
48-
const guides: Guide[] = files.map((file) => {
49-
const filePath = path.join(guidesDir, file);
50-
const fileContent = fs.readFileSync(filePath, "utf-8");
51-
const { data } = matter(fileContent);
52-
const slug = file.replace(/\.md$/, "");
36+
const guides: Guide[] = contentSlugs("guides").map((slug) => {
37+
const { data } = matter(readContent("guides", slug) ?? "");
5338

5439
return {
5540
slug,

‎app/layout.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ const gaId = process.env.NEXT_PUBLIC_GA_ID
1919

2020
// Cloudflare Web Analytics site token for devweekends.com. It is public (it ships
2121
// in the page) and only counts on the live site, so previews and local runs stay out.
22-
const cfBeaconToken = process.env.VERCEL_ENV === "production" ? "5206235d2175433195c5d83df73ba4fc" : undefined
22+
// SITE_ENV is set to "production" only by `npm run deploy` (see package.json and wrangler.jsonc).
23+
const cfBeaconToken = process.env.SITE_ENV === "production" ? "5206235d2175433195c5d83df73ba4fc" : undefined
2324

2425
export const metadata: Metadata = {
2526
metadataBase: new URL(SITE_URL),

‎app/llms-full.txt/route.ts‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
import fs from 'fs'
2-
import path from 'path'
31
import matter from 'gray-matter'
42
import { NextResponse } from 'next/server'
53

64
import { CONTENT_SIGNAL, SITE_URL } from '@/lib/seo'
5+
import { contentSlugs, readContent } from '@/lib/site-files'
76

87
/** Posts or guides as Markdown links, newest first, with their own descriptions */
98
function contentLinks(folder: 'blog' | 'guides'): string {
10-
const dir = path.join(process.cwd(), 'content', folder)
11-
if (!fs.existsSync(dir)) return ''
12-
return fs
13-
.readdirSync(dir)
14-
.filter((file) => file.endsWith('.md'))
15-
.map((file) => ({ slug: file.replace(/\.md$/, ''), data: matter(fs.readFileSync(path.join(dir, file), 'utf8')).data }))
9+
return contentSlugs(folder)
10+
.map((slug) => ({ slug, data: matter(readContent(folder, slug) ?? '').data }))
1611
.filter(({ data }) => data.draft !== true && data.title)
1712
.sort((a, b) => new Date(b.data.date ?? 0).getTime() - new Date(a.data.date ?? 0).getTime())
1813
.map(({ slug, data }) => `- [${data.title}](${SITE_URL}/${folder}/${slug})${data.description ? `: ${data.description}` : ''}`)

0 commit comments

Comments
 (0)