Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cloudflare-prerender-console-output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/cloudflare': patch
---

Surfaces `console.log` and `console.warn` output from workerd during prerendering
19 changes: 17 additions & 2 deletions packages/integrations/cloudflare/src/prerenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
AssetsGlobalStaticImagesList,
PathWithRoute,
} from 'astro';
import { preview, type PreviewServer as VitePreviewServer } from 'vite';
import { preview, createLogger, type PreviewServer as VitePreviewServer } from 'vite';
import { fileURLToPath } from 'node:url';
import { mkdir } from 'node:fs/promises';
import { cloudflare as cfVitePlugin, type PluginConfig } from '@cloudflare/vite-plugin';
Expand Down Expand Up @@ -53,6 +53,21 @@ export function createCloudflarePrerenderer({
// Ensure client dir exists (CF plugin expects it for assets)
await mkdir(clientDir, { recursive: true });

// Create a custom logger that filters out internal HTTP request logs (e.g. "POST /__astro_prerender 200 OK")
// from the Cloudflare vite plugin while still allowing user console.log output to pass through.
// We strip ANSI codes before testing because the Cloudflare vite plugin wraps messages in color codes.
const defaultLogger = createLogger('info');
// eslint-disable-next-line no-control-regex
const ansiRe = /\x1b\[[0-9;]*m/g;
const astroRequestLogRe = /^(?:GET|POST|PUT|DELETE|PATCH|HEAD|OPTIONS)\s+\/__astro_/;
const customLogger: ReturnType<typeof createLogger> = {
...defaultLogger,
info(msg, opts) {
if (astroRequestLogRe.test(msg.replace(ansiRe, ''))) return;
defaultLogger.info(msg, opts);
},
};

previewServer = await preview({
configFile: false,
base,
Expand All @@ -61,7 +76,7 @@ export function createCloudflarePrerenderer({
outDir: fileURLToPath(serverDir),
},
root: fileURLToPath(root),
logLevel: 'error',
customLogger,
preview: {
host: 'localhost',
port: 0, // Let the OS pick a free port
Expand Down
Loading