diff --git a/README.md b/README.md index 8596156..1b6422e 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,99 @@ # microcms-blog -[Vercel's Next.js Blog with microCMS](https://github.com/microcmsio/simple-blog-with-microcms) on -**Cloudflare Workers**, deployed by [Orun](https://github.com/sourceplane/orun) to stage -and prod on every merge, and then packaged as an Orun baseline. - -This commit is the repository's frame: `intent.yaml` (with the placeholder -workspace `ws_CHANGE_ME`, so CI plans nothing until it is linked), the Orun -CI workflow, the shared TypeScript and ESLint configuration, and the spec -index. The work is tracked as an epic under [`specs/epics`](specs/epics/README.md). +Vercel's [Next.js Blog with microCMS](https://github.com/microcmsio/simple-blog-with-microcms) +— microCMS's Simple Blog template: posts, tags and writers in a microCMS +service read through its SDK, code blocks highlighted on the server with +highlight.js, posts by tag, full-text search, pages of ten, and drafts +previewed by their draft key — running on **Cloudflare Workers + Static +Assets** through [`@opennextjs/cloudflare`](https://opennext.js.org/cloudflare), deployed by +[Orun](https://github.com/sourceplane/orun) to stage and prod on every merge. +Packaging it as an Orun baseline is the next milestone (MC2). It goes live before its owner has a microCMS service +at all. + +The template's code is the code and microCMS stays the SaaS it is. What the +port adds is the Workers build, the monorepo wrapper, a test package and the +deploy contract; what it changes is what Workers force: + +- **Credentials:** the microCMS service and its API key are the Worker's, + read per request and never in a build, so every page renders on request. +- **Caching:** the template's Node `proxy.ts`, which marked a draft + uncacheable, is now a `headers()` rule. On Workers that proxy is + experimental, and its header was appended to the catch-all one instead of + replacing it. What +makes it deployable with no microCMS service is the port's: an unconnected +blog renders sample posts under a visible "not connected" notice instead of +failing (the template threw at module load). See +[`specs/epics/microcms-blog`](specs/epics/microcms-blog/README.md) for the +design, the milestones and the as-built status. + +## What is here (the site) + +| Surface | Where | +|---|---| +| Home, `/p/[current]`, `/blogs/[slug]`, `/tags/[tagId]` (and its pages), `/search` (and its pages) | `apps/web/app` | +| The microCMS reads, the SDK's client created per request, with the sample fallback | `apps/web/libs/microcms.ts`, `apps/web/libs/sample.ts` | +| The "not connected" notice and the sample writer's picture | `apps/web/components/ConnectMicroCMS`, `apps/web/public/sample` | +| Rich text and code highlighting, dates | `apps/web/libs/utils.ts` | +| The template's components (header, nav, search, lists, pagination, tags, article) | `apps/web/components` | +| The three APIs, as microCMS exports them | `apps/web/microcms-template.json` | +| The blog's name (the rename's seam) | `apps/web/app/site.ts` | +| Workers build and deploy contract | `apps/web/next.config.js`, `open-next.config.ts`, `wrangler.jsonc`, `component.yaml`, `tooling/wire/worker-secrets.mjs` | +| Tests | `tests/web` (vitest) | + +## Getting started + +```bash +pnpm install +pnpm typecheck +pnpm lint +pnpm test +pnpm build # next build + opennextjs-cloudflare build → apps/web/.open-next +pnpm --filter @app/web preview # wrangler dev over the built Worker: the sample posts, unconnected +``` + +That much needs no microCMS service: the baseline's own e2e tier +(`testing/e2e.test.sh`) runs exactly this on each landing before a tag, and +a blog's developers run it before a push — a blog's pull request builds and +dry-deploys it, and does not lint or run the tests. To see your own content: + +```bash +cd apps/web +cp .env.example .env.local # MICROCMS_SERVICE_DOMAIN, MICROCMS_API_KEY +cd ../.. +pnpm --filter @app/web dev # http://localhost:3000 +``` + +For the Worker locally, the service and its key are Worker secrets: + +```bash +cd apps/web +cp .dev.vars.example .dev.vars # MICROCMS_SERVICE_DOMAIN, MICROCMS_API_KEY +pnpm build +pnpm preview # wrangler dev over the built Worker +``` + +| Variable | Where it is read | What it does | +|---|---|---| +| `MICROCMS_SERVICE_DOMAIN` | request time, in the Worker (`getCloudflareContext().env`; `process.env` under `next dev`) | The service ID. A deploy pushes its environment's `MICROCMS_SERVICE_DOMAIN` secret to the Worker, best-effort; the build never has it. Unset, the blog is unconnected: the sample posts under a notice. | +| `MICROCMS_API_KEY` | request time, likewise | The service's API key; needed with the service ID to connect. | +| `BASE_URL` | build time (`next.config.js` inlines it) | The site's public origin, for `metadataBase` (Open Graph and canonical URLs). The deploy sets it per environment: `SITE_URL` when `intent.yaml` sets one, else the Worker's workers.dev URL. | + +## How it maps to Vercel + +| Vercel | Here | +|---|---| +| Next.js hosting (pre-rendered pages, the Node `proxy.ts`, route handlers) | One Cloudflare Worker (`.open-next/worker.js`) + Static Assets; every page renders on request (the layout is `force-dynamic`: the build has no microCMS key) | +| Project env vars (`MICROCMS_API_KEY`, `MICROCMS_SERVICE_DOMAIN`, `BASE_URL`) read by the build and the server | The service and its key: optional Orun secrets, unset for the build (`env -u`) and pushed to the Worker as secrets after it (`tooling/wire/worker-secrets.mjs`), read per request. `BASE_URL`: set per environment by the deploy | +| A missing key throws at module load: every page a 500 | An unconnected blog: sample posts and a notice — deployable before a microCMS service exists | +| `proxy.ts` sets `CDN-Cache-Control: no-store` on a draft (`/blogs/*?dk=`) | A second `headers()` rule in `next.config.js`, after the template's catch-all one (OpenNext runs a Node proxy only experimentally, and appended its header to the catch-all's) | +| Vercel's CDN honours `CDN-Cache-Control` (60 s, then 300 s stale) | The header is kept for a CDN in front; Cloudflare does not cache a Worker's responses on workers.dev, so every view reads microCMS, whose own CDN fronts its API | +| A microCMS webhook rebuilding the site (the template's `repository_dispatch` placeholder workflow) | Nothing to rebuild: a published post shows on the next visit. The placeholder workflow is not carried | +| `next/image` for the logo, the clock and the placeholder image | Unchanged, unoptimised (`images.unoptimized`): the Worker carries no image optimiser; microCMS's images keep their image-API query strings | +| Deploy on push | Orun deploys `-web-{stage,prod}` on merge to `main` and smoke-tests it (`apps/web/component.yaml`) | +| "Deploy with Vercel" (clone the template) | `orun new --blueprint repo-blueprint.yaml` — a new repository, branded, deployed | + +## CI + +| Workflow | What it runs | Ships to a blog | +|---|---|---| +| `ci.yml` | the Orun pipeline, and the only workflow a blog carries: `orun plan --changed`, then one job per lane. A pull request runs the verify lanes (the blog built and dry-deployed per environment; they do not lint or run the tests), except the bootstrap's own landings (`orun/BASE-*`), where `plan` alone is the check; a merge deploys stage → prod. It plans nothing while `intent.yaml` names the placeholder workspace `ws_CHANGE_ME`, as this repository does. A manual run (Orunbase's Deploy button, or Run workflow on `main`) plans the environment and components it names under the push-to-main trigger, so they deploy as a merge deploys them | yes | diff --git a/apps/web/.dev.vars.example b/apps/web/.dev.vars.example new file mode 100644 index 0000000..a17a943 --- /dev/null +++ b/apps/web/.dev.vars.example @@ -0,0 +1,7 @@ +# Local Worker secrets for `wrangler dev` / `pnpm preview` (opennextjs-cloudflare +# preview). Copy to .dev.vars (git-ignored). On stage and prod they are the +# product's Orun secrets MICROCMS_SERVICE_DOMAIN and MICROCMS_API_KEY, which the +# deploy pushes to the Worker (tooling/wire/worker-secrets.mjs). Both optional: +# without them the blog renders its sample posts. +MICROCMS_SERVICE_DOMAIN= +MICROCMS_API_KEY= diff --git a/apps/web/.env.example b/apps/web/.env.example new file mode 100644 index 0000000..e24e744 --- /dev/null +++ b/apps/web/.env.example @@ -0,0 +1,11 @@ +# https://github.com/microcmsio/simple-blog-with-microcms +# Copy to .env.local (git-ignored) for `next dev`. Without the service and its +# key the blog is unconnected: it renders its sample posts (libs/sample.ts) +# under a notice. On stage and prod both are the product's Orun secrets, which +# the deploy pushes to the Worker as secrets; the pages read them per request, +# and no build ever sees them (README → Connect microCMS). +MICROCMS_SERVICE_DOMAIN= +MICROCMS_API_KEY= +# The site's public origin (metadataBase). The deploy sets it per environment; +# unset, it is http://localhost:3000. +BASE_URL= diff --git a/apps/web/LICENSE b/apps/web/LICENSE new file mode 100644 index 0000000..2ca3960 --- /dev/null +++ b/apps/web/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 microCMS + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/apps/web/README.md b/apps/web/README.md new file mode 100644 index 0000000..d19b711 --- /dev/null +++ b/apps/web/README.md @@ -0,0 +1,32 @@ +# Simple Blog with microCMS, on Workers + +A blog whose content lives in microCMS, from Vercel's +[Next.js Blog with microCMS](https://github.com/microcmsio/simple-blog-with-microcms) +(microCMS's Simple Blog template, MIT, demo at +https://simple-blog-with-microcms.vercel.app/), running on Cloudflare Workers + +Static Assets: + +- Posts, tags and writers in a microCMS service's three list APIs + (`microcms-template.json`), read through `microcms-js-sdk` + (`libs/microcms.ts`), the client created per request from the Worker's + `MICROCMS_SERVICE_DOMAIN` and `MICROCMS_API_KEY` +- The post body is microCMS's rich-text HTML, its code blocks highlighted on + the server with highlight.js (`libs/utils.ts`) +- `/p/` pages of ten, `/tags/`, `/search?q=` (microCMS's full-text + search), and drafts at `/blogs/?dk=` +- Every page rendered on request by the Worker; the template's + `CDN-Cache-Control` header (`next.config.js`), a draft never cached + +Without the service and its key the blog is **unconnected**: it renders the +sample posts in `libs/sample.ts` under a notice that says how to connect. The +blog's name is in `app/site.ts`. + +```bash +pnpm dev # next dev on http://localhost:3000 (.env.local, or the sample posts) +pnpm build # next build + opennextjs-cloudflare build → .open-next/ +pnpm preview # wrangler dev over the built Worker (.dev.vars, or the sample posts) +``` + +How it is connected to a microCMS service and deployed — per environment, on +merge to `main` — is the repository's README and `component.yaml` beside this +file. diff --git a/apps/web/app/blogs/[slug]/page.tsx b/apps/web/app/blogs/[slug]/page.tsx new file mode 100644 index 0000000..f1067d5 --- /dev/null +++ b/apps/web/app/blogs/[slug]/page.tsx @@ -0,0 +1,43 @@ +import { Metadata } from 'next'; +import { getDetail } from '@/libs/microcms'; +import Article from '@/components/Article'; + +type Props = { + params: Promise<{ + slug: string; + }>; + searchParams: Promise<{ + dk: string; + }>; +}; + +export async function generateMetadata(props: Props): Promise { + const searchParams = await props.searchParams; + const params = await props.params; + const data = await getDetail(params.slug, { + draftKey: searchParams.dk, + }); + + return { + title: data.title, + description: data.description, + openGraph: { + title: data.title, + description: data.description, + images: [data?.thumbnail?.url || ''], + }, + alternates: { + canonical: `/blogs/${params.slug}`, + }, + }; +} + +export default async function Page(props: Props) { + const searchParams = await props.searchParams; + const params = await props.params; + const data = await getDetail(params.slug, { + draftKey: searchParams.dk, + }); + + return
; +} diff --git a/apps/web/app/favicon.ico b/apps/web/app/favicon.ico new file mode 100644 index 0000000..718d6fe Binary files /dev/null and b/apps/web/app/favicon.ico differ diff --git a/apps/web/app/globals.css b/apps/web/app/globals.css new file mode 100644 index 0000000..51de4b7 --- /dev/null +++ b/apps/web/app/globals.css @@ -0,0 +1,48 @@ +:root { + --font-mono: ui-monospace, Menlo, Monaco, 'Cascadia Mono', 'Segoe UI Mono', 'Roboto Mono', + 'Oxygen Mono', 'Ubuntu Monospace', 'Source Code Pro', 'Fira Mono', 'Droid Sans Mono', + 'Courier New', monospace; + --color-text-main: #333; + --color-text-sub: #999; + --color-bg-main: #fff; + --color-bg-sub: #f3f3f3; + --color-bg-code: #fafafa; + --color-border-dark: #ccc; + --color-border: #ddd; + --color-border-light: #f3f3f3; + --color-current: #eee; + --border-radius: 4px; +} + +* { + box-sizing: border-box; + padding: 0; + margin: 0; +} + +html, +body { + max-width: 100vw; + overflow-x: hidden; +} + +body { + font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', YuGothic, 'ヒラギノ角ゴ ProN W3', + Hiragino Kaku Gothic ProN, Arial, 'メイリオ', Meiryo, sans-serif; + color: var(--color-text-main); + line-height: 1.8; +} + +code { + font-family: menlo, inconsolata, monospace; +} + +a { + color: inherit; + text-decoration: none; +} + +ol, +ul { + list-style: none; +} diff --git a/apps/web/app/layout.module.css b/apps/web/app/layout.module.css new file mode 100644 index 0000000..5b71383 --- /dev/null +++ b/apps/web/app/layout.module.css @@ -0,0 +1,11 @@ +.main { + width: 720px; + margin: 0 auto; +} + +@media (max-width: 640px) { + .main { + width: auto; + padding: 24px; + } +} diff --git a/apps/web/app/layout.tsx b/apps/web/app/layout.tsx new file mode 100644 index 0000000..4141c57 --- /dev/null +++ b/apps/web/app/layout.tsx @@ -0,0 +1,57 @@ +import { getTagList, isMicroCMSConnected } from '@/libs/microcms'; +import { LIMIT } from '@/constants'; +import Header from '@/components/Header'; +import Footer from '@/components/Footer'; +import Nav from '@/components/Nav'; +import ConnectMicroCMS from '@/components/ConnectMicroCMS'; +import { SITE_NAME } from './site'; +import './globals.css'; +import styles from './layout.module.css'; + +export const metadata = { + metadataBase: new URL(process.env.BASE_URL || 'http://localhost:3000'), + title: { + template: `%s | ${SITE_NAME}`, + default: SITE_NAME, + }, + description: 'A simple blog presented by microCMS', + openGraph: { + title: { + template: `%s | ${SITE_NAME}`, + default: SITE_NAME, + }, + description: 'A simple blog presented by microCMS', + images: '/ogp.png', + }, + alternates: { + canonical: '/', + }, +}; + +type Props = { + children: React.ReactNode; +}; + +// Every page is rendered when it is requested: the microCMS service and its key +// are the Worker's, read per request (libs/microcms.ts), and a build has +// neither. +export const dynamic = 'force-dynamic'; + +export default async function RootLayout({ children }: Props) { + const tags = await getTagList({ + limit: LIMIT, + }); + return ( + + +
+