AI-native design editor. Open-source Figma alternative.
fix(cloud): serve the site from Vite's dev server in cloud:dev (#1549)
Danila Poyarkov1ebe288b4323
diff --git a/packages/cloud/src/runtime/node/main.ts b/packages/cloud/src/runtime/node/main.tsindex 3ca6cd6fb..a0783b87b 100644--- a/packages/cloud/src/runtime/node/main.ts+++ b/packages/cloud/src/runtime/node/main.ts@@ -10,6 +10,8 @@ if (process.argv.includes('--help') || process.argv.includes('-h')) {Runs OpenPencil Cloud. Settings come from the environment:OPENPENCIL_CLOUD_CONFIG the deployment TOML file (required)OPENPENCIL_CLOUD_SITE_DIR the editor and portal build to serve+ OPENPENCIL_CLOUD_DEV_SITE_URL+ a Vite dev server to serve them from instead (cloud:dev)PORT, HOST where to listen (default 8787 on 0.0.0.0)plus the secrets the TOML names. See https://openpencil.dev/reference/self-hosting-cloud`)diff --git a/packages/cloud/src/runtime/node/server.ts b/packages/cloud/src/runtime/node/server.tsindex 55d087f52..8ed77209e 100644--- a/packages/cloud/src/runtime/node/server.ts+++ b/packages/cloud/src/runtime/node/server.ts@@ -1,6 +1,6 @@import { defaultRelayURL } from '#cloud/contract'import { createS3ObjectStore } from '#cloud/runtime/s3/objects'-import { createCloudServer } from '#cloud/runtime/site/server'+import { createCloudServer, type SiteFiles } from '#cloud/runtime/site/server'import {createCloudApp,loadAISigningKey,@@ -19,13 +19,27 @@ import { createMigratedNodeCloudDatabase } from './bootstrap'import { createNodeTransactionalEmailRuntime } from './email/runtime'import { listen } from './listen'import { attachCollaborationRelay } from './relay'-import { nodeSiteFiles } from './site'+import { developmentSiteFiles, nodeSiteFiles } from './site'export type NodeCloudServerOptions = {environment?: Readonly<Record<string, string | undefined>>port?: number}+/** The site the server serves: a build, or in `cloud:dev` a development server's source. */+function siteFiles(+ environment: Readonly<Record<string, string | undefined>>+): SiteFiles | undefined {+ const developmentSite = environment.OPENPENCIL_CLOUD_DEV_SITE_URL+ if (developmentSite) return developmentSiteFiles(developmentSite)+ const siteDirectory = environment.OPENPENCIL_CLOUD_SITE_DIR+ if (siteDirectory) return nodeSiteFiles(siteDirectory)+ console.warn(+ '[Cloud] OPENPENCIL_CLOUD_SITE_DIR is not set; the editor and sign-in pages are off.'+ )+ return undefined+}+export async function startNodeCloudServer(options: NodeCloudServerOptions = {}) {const environment = options.environment ?? process.envconst { config, database } = await createMigratedNodeCloudDatabase(environment)@@ -78,13 +92,8 @@ export async function startNodeCloudServer(options: NodeCloudServerOptions = {})onError: (error) => console.error('[Cloud] Email worker failed:', error)}): undefined- const siteDirectory = environment.OPENPENCIL_CLOUD_SITE_DIR- if (!siteDirectory)- console.warn(- '[Cloud] OPENPENCIL_CLOUD_SITE_DIR is not set; the editor and sign-in pages are off.'- )const cloudServer = createCloudServer(app, {- site: siteDirectory ? nodeSiteFiles(siteDirectory) : undefined,+ site: siteFiles(environment),indexingPolicy: config.indexingPolicy})const server = await listen(diff --git a/packages/cloud/src/runtime/node/site.ts b/packages/cloud/src/runtime/node/site.tsindex 9a54ed80a..09c4d9cfe 100644--- a/packages/cloud/src/runtime/node/site.ts+++ b/packages/cloud/src/runtime/node/site.ts@@ -4,6 +4,12 @@ import { resolve, sep } from 'node:path'import type { SiteFiles } from '#cloud/runtime/site/server'import { getMimeType } from 'hono/utils/mime'+/**+ * The only request headers Vite reads to pick a reply: which module, style, or page form to send.+ * Others stay behind, such as credentials, ranges, and validators that would get a partial or 304.+ */+const FORWARDED_HEADERS = ['accept', 'sec-fetch-dest']+/** The built site's files in `directory`, never anything outside it. */export function nodeSiteFiles(directory: string): SiteFiles {const root = resolve(directory)@@ -27,3 +33,32 @@ export function nodeSiteFiles(directory: string): SiteFiles {}}}++/**+ * The site as the Vite development server at `origin` serves it, for `bun run cloud:dev`: pages,+ * modules, and styles come from source as they are now, so an edit shows without a build.+ */+export function developmentSiteFiles(origin: string): SiteFiles {+ return async (path, request) => {+ const url = new URL(path, origin)+ const requested = new URL(request.url)+ // Vite tells module requests apart by their query, such as `?import` or `?t=`.+ if (requested.pathname === path) url.search = requested.search+ const headers = new Headers()+ for (const name of FORWARDED_HEADERS) {+ const value = request.headers.get(name)+ if (value !== null) headers.set(name, value)+ }+ try {+ const response = await fetch(url, { headers })+ if (!response.ok || !response.body) return null+ return {+ body: response.body,+ type: response.headers.get('Content-Type') ?? 'application/octet-stream',+ cacheControl: 'no-cache'+ }+ } catch {+ return null+ }+ }+}diff --git a/packages/cloud/src/runtime/site/server.ts b/packages/cloud/src/runtime/site/server.tsindex 8509976a9..cc53c0eb7 100644--- a/packages/cloud/src/runtime/site/server.ts+++ b/packages/cloud/src/runtime/site/server.ts@@ -2,8 +2,13 @@ import { CLOUD_PORTAL_PAGE_ROOTS } from '#cloud/contract'import { withIndexingPolicy } from '#cloud/server'import { Hono } from 'hono'-/** A file of the built Cloud site (`bun run build:cloud`): the editor and the portal. */-export type SiteFile = { body: ReadableStream | Uint8Array<ArrayBuffer>; type: string }+/** A file of the Cloud site (`bun run build:cloud`): the editor and the portal. */+export type SiteFile = {+ body: ReadableStream | Uint8Array<ArrayBuffer>+ type: string+ /** Overrides the cache policy built files get, for a site whose files change in place. */+ cacheControl?: string+}/** Reads the built site's file at a URL path, or null when there is none. */export type SiteFiles = (path: string, request: Request) => Promise<SiteFile | null>@@ -61,7 +66,7 @@ export function createCloudServer(: new Response(file.body, {headers: {'Content-Type': file.type,- 'Cache-Control': cacheControl(c.req.path),+ 'Cache-Control': file.cacheControl ?? cacheControl(c.req.path),'X-Content-Type-Options': 'nosniff'}})diff --git a/packages/cloud/tests/runtime/node/site.test.ts b/packages/cloud/tests/runtime/node/site.test.tsindex c9c7385c2..a3c2f20a7 100644--- a/packages/cloud/tests/runtime/node/site.test.ts+++ b/packages/cloud/tests/runtime/node/site.test.ts@@ -3,7 +3,7 @@ import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'import { tmpdir } from 'node:os'import { join } from 'node:path'-import { nodeSiteFiles } from '#cloud/runtime/node/site'+import { developmentSiteFiles, nodeSiteFiles } from '#cloud/runtime/node/site'let directory = ''@@ -42,3 +42,56 @@ describe('Node site files', () => {expect(file?.type).toBe('application/octet-stream')})})++describe('development site files', () => {+ const asked: { url: string; headers: Headers }[] = []+ const development = Bun.serve({+ port: 0,+ fetch(request) {+ const url = new URL(request.url)+ asked.push({+ url: url.pathname + url.search,+ headers: request.headers+ })+ if (url.pathname === '/missing.js') return new Response('', { status: 404 })+ return new Response('export {}', { headers: { 'Content-Type': 'text/javascript' } })+ }+ })+ afterAll(() => development.stop(true))+ const files = developmentSiteFiles(development.url.origin)++ test('asks the development server for the requested file, query and all, never cached', async () => {+ asked.length = 0+ const module = new Request('https://cloud.example/src/main.ts?t=1', {+ headers: {+ Accept: '*/*',+ 'Sec-Fetch-Dest': 'script',+ Cookie: 'session=secret',+ Authorization: 'Bearer secret',+ Range: 'bytes=0-9',+ 'If-None-Match': 'W/"stale"'+ }+ })+ const file = await files('/src/main.ts', module)+ expect(file?.type).toBe('text/javascript')+ expect(file?.cacheControl).toBe('no-cache')+ expect(asked.map(({ url }) => url)).toEqual(['/src/main.ts?t=1'])+ const forwarded = asked[0]?.headers+ expect(forwarded?.get('Accept')).toBe('*/*')+ expect(forwarded?.get('Sec-Fetch-Dest')).toBe('script')+ for (const name of ['Cookie', 'Authorization', 'Range', 'If-None-Match']) {+ expect(forwarded?.has(name)).toBe(false)+ }+ })++ test('asks for a page the server routes there without the address query', async () => {+ asked.length = 0+ await files('/cloud.html', new Request('https://cloud.example/account?tab=security'))+ expect(asked.map(({ url }) => url)).toEqual(['/cloud.html'])+ })++ test('answers nothing for a missing file or an unreachable server', async () => {+ expect(await files('/missing.js', request)).toBeNull()+ expect(await developmentSiteFiles('http://127.0.0.1:9')('/src/main.ts', request)).toBeNull()+ })+})diff --git a/packages/docs/development/cloud.md b/packages/docs/development/cloud.mdindex eebec1b89..e0b98feb4 100644--- a/packages/docs/development/cloud.md+++ b/packages/docs/development/cloud.md@@ -252,7 +252,11 @@ framed. Both runtimes put the API behind the same Hono router, `createCloudServe`src/runtime/site`, which answers portal pages before the API, and only where the API returns 404the site's files, then the editor's `index.html` for any other page, so the editor's router keepsits own addresses. Node reads the build named by `OPENPENCIL_CLOUD_SITE_DIR`, and the Worker reads-its static assets. `bun run cloud:dev` keeps a watched site build next to the server.+its static assets. `bun run cloud:dev` builds nothing: it runs Vite's development server for the+site under its own portless name and points the Node server at it with+`OPENPENCIL_CLOUD_DEV_SITE_URL`, so the server takes each file from source, uncached, and pages+update in place. Vite's update socket connects to its own address, since the server accepts+WebSockets only for collaboration.The editor a server serves knows it from the build (`__OPENPENCIL_CLOUD_SITE__`). Before it boots,it asks who is signed in: an account that may use the server gets the editor, and anyone else atdiff --git a/tools/dev/cloud/src/index.ts b/tools/dev/cloud/src/index.tsindex 4c47f2685..2c907d607 100644--- a/tools/dev/cloud/src/index.ts+++ b/tools/dev/cloud/src/index.ts@@ -76,8 +76,9 @@ async function authSecret(): Promise<string> {}async function start(project: string): Promise<void> {- const [cloudURL, mailURL, objectStorageURL, editorURL] = await Promise.all([+ const [cloudURL, siteURL, mailURL, objectStorageURL, editorURL] = await Promise.all([portlessURL('cloud.open-pencil'),+ portlessURL('cloud-site.open-pencil'),portlessURL('mail.open-pencil'),portlessURL('s3.open-pencil'),portlessURL('open-pencil')@@ -126,12 +127,12 @@ async function start(project: string): Promise<void> {])const betterAuthSecret = await authSecret()- // The server serves the editor and its own pages from this build, rebuilt on every change.- const site = Bun.spawn(['bun', 'run', 'build:cloud', '--watch'], {- cwd: root,- stdout: 'inherit',- stderr: 'inherit'- })+ // The server serves the editor and its own pages from Vite's development server, so an edit+ // shows at once, through hot updates, with no build.+ const site = Bun.spawn(+ ['bunx', 'portless', 'run', '--name', 'cloud-site.open-pencil', 'vite', '--mode', 'cloud'],+ { cwd: root, stdout: 'inherit', stderr: 'inherit' }+ )console.warn(`OpenPencil Cloud: ${cloudURL}`)console.warn(`Captured email: ${mailURL}`)@@ -155,7 +156,7 @@ async function start(project: string): Promise<void> {...process.env,HOST: '127.0.0.1',OPENPENCIL_CLOUD_CONFIG: generatedConfig,- OPENPENCIL_CLOUD_SITE_DIR: join(root, 'dist-cloud'),+ OPENPENCIL_CLOUD_DEV_SITE_URL: siteURL,DATABASE_URL: `postgresql://openpencil:${POSTGRES_PASSWORD}@127.0.0.1:${postgresPort}/openpencil`,BETTER_AUTH_SECRET: betterAuthSecret,S3_ACCESS_KEY_ID,@@ -164,7 +165,7 @@ async function start(project: string): Promise<void> {})} finally {- // The watcher stops with the server however the server ends.+ // The development server stops with the Cloud server however that ends.site.kill()}}diff --git a/vite.config.ts b/vite.config.tsindex ada163ba1..e05cd4260 100644--- a/vite.config.ts+++ b/vite.config.ts@@ -40,6 +40,8 @@ export default defineConfig(async ({ command, mode }) => {const env = loadEnv(mode, __dirname, 'VITE_')// `VITE_OPENPENCIL_CLOUD=off` builds the editor without OpenPencil Cloud.const cloud = env.VITE_OPENPENCIL_CLOUD !== 'off'+ // Agents automate the editor's own development server; a Cloud site, built or served, has none.+ const automation = !metrics && !cloudSiteawait Promise.all([ensureBrandAssets(['web']), metrics ? undefined : ensureDemoDocument()])return {resolve: {@@ -51,7 +53,9 @@ export default defineConfig(async ({ command, mode }) => {__OPENPENCIL_OFFICIAL_CLOUD_URL__: JSON.stringify(env.VITE_OPENPENCIL_OFFICIAL_CLOUD_URL ?? null),- __OPENPENCIL_LOCAL_AUTOMATION_TOKEN__: JSON.stringify(localAutomationToken(command)),+ __OPENPENCIL_LOCAL_AUTOMATION_TOKEN__: JSON.stringify(+ automation ? localAutomationToken(command) : null+ ),__OPENPENCIL_LOCAL_AUTOMATION_URL__: JSON.stringify(automationRoute.browserURL),__OPENPENCIL_LOCAL_AUTOMATION_HTTP_URL__: JSON.stringify(automationRoute.browserURL.replace(/^ws/, 'http')@@ -68,12 +72,17 @@ export default defineConfig(async ({ command, mode }) => {tailwindcss(),Icons({ compiler: 'vue3', customCollections: { ai: aiIconCollection() } }),Components({ resolvers: [IconsResolver({ prefix: 'icon', customCollections: ['ai'] })] }),- metrics ? undefined : openPencilAutomationPlugin(command, host),+ automation ? openPencilAutomationPlugin(command, host) : undefined,vue(),metrics ? metricsBrandPlugin((outDir) => resolve(outDir, '..')) : openPencilPwaPlugin(),cloudSite ? cloudSitePlugin() : undefined],clearScreen: false,+ // A Cloud server routes its own pages and falls back to the editor itself, so a development+ // server it takes files from answers only the files it has. Its dependency cache is its own,+ // so it runs beside the editor's development server in the same checkout.+ appType: cloudSite ? 'mpa' : 'spa',+ cacheDir: cloudSite ? 'node_modules/.vite-cloud' : undefined,publicDir: metrics ? false : 'public',base: metrics ? '/admin/' : '/',build: {@@ -94,6 +103,10 @@ export default defineConfig(async ({ command, mode }) => {}}},- server: createDevServerOptions(host, __dirname)+ server: createDevServerOptions(+ host,+ __dirname,+ cloudSite ? process.env.PORTLESS_URL : undefined+ )}})diff --git a/vite/server.ts b/vite/server.tsindex 3006ebcf5..ac4a2e7b6 100644--- a/vite/server.ts+++ b/vite/server.ts@@ -21,18 +21,31 @@ export const WATCH_IGNORED = [ignoreMarkdownOutsideSource]-export function createDevServerOptions(host: string | undefined, rootDir: string): ServerOptions {+function hmrOptions(host: string | undefined, hmrURL: string | undefined): ServerOptions['hmr'] {+ if (!hmrURL) return host ? { protocol: 'ws', host, port: 1421 } : undefined+ const url = new URL(hmrURL)+ const secure = url.protocol === 'https:'+ return {+ protocol: secure ? 'wss' : 'ws',+ host: url.hostname,+ clientPort: Number(url.port || (secure ? 443 : 80))+ }+}++/**+ * `hmrURL` is the server's own public address, for pages another server serves: the Cloud server+ * forwards their files here in `cloud:dev` but not WebSockets, so updates connect here directly.+ */+export function createDevServerOptions(+ host: string | undefined,+ rootDir: string,+ hmrURL?: string+): ServerOptions {return {port: 1420,strictPort: true,host: host || false,- hmr: host- ? {- protocol: 'ws',- host,- port: 1421- }- : undefined,+ hmr: hmrOptions(host, hmrURL),// Vite forwards browser logs to the terminal when an agent starts it. Vue warnings carry the// component's props, and serializing the editor state behind them freezes the tab, so only// errors are forwarded.