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/static-output-cloudflare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/cloudflare": patch
---

Fixes fully static sites to not output server-side worker code. When all routes are prerendered, the `_worker.js` directory is now removed from the build output.
21 changes: 14 additions & 7 deletions packages/integrations/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { createReadStream, existsSync, readFileSync } from 'node:fs';
import { appendFile, stat } from 'node:fs/promises';
import { appendFile, rm, stat } from 'node:fs/promises';
import { createInterface } from 'node:readline/promises';
import { removeLeadingForwardSlash } from '@astrojs/internal-helpers/path';
import { createRedirectsFromAstroRoutes, printAsRedirects } from '@astrojs/underscore-redirects';
import { cloudflare as cfVitePlugin, type PluginConfig } from '@cloudflare/vite-plugin';
import type {
AstroConfig,
AstroIntegration,
HookParameters,
IntegrationResolvedRoute,
} from 'astro';
import { astroFrontmatterScanPlugin } from './esbuild-plugin-astro-frontmatter.js';
Expand Down Expand Up @@ -79,9 +78,9 @@ export type Options = {

export default function createIntegration(args?: Options): AstroIntegration {
let _config: AstroConfig;
let finalBuildOutput: HookParameters<'astro:config:done'>['buildOutput'];

let _routes: IntegrationResolvedRoute[];
let _isFullyStatic = false;

const sessionKVBindingName = args?.sessionKVBindingName ?? DEFAULT_SESSION_KV_BINDING_NAME;
const imagesBindingName = args?.imagesBindingName ?? DEFAULT_IMAGES_BINDING_NAME;
Expand Down Expand Up @@ -233,10 +232,13 @@ export default function createIntegration(args?: Options): AstroIntegration {
},
'astro:routes:resolved': ({ routes }) => {
_routes = routes;
// Check if all non-internal routes are prerendered (fully static site)
const nonInternalRoutes = routes.filter((route) => route.origin !== 'internal');
_isFullyStatic =
nonInternalRoutes.length > 0 && nonInternalRoutes.every((route) => route.isPrerendered);
},
'astro:config:done': ({ setAdapter, config, buildOutput, injectTypes, logger }) => {
'astro:config:done': ({ setAdapter, config, injectTypes, logger }) => {
_config = config;
finalBuildOutput = buildOutput;

injectTypes({
filename: 'cloudflare.d.ts',
Expand All @@ -254,7 +256,7 @@ export default function createIntegration(args?: Options): AstroIntegration {
supportedAstroFeatures: {
serverOutput: 'stable',
hybridOutput: 'stable',
staticOutput: 'unsupported',
staticOutput: 'stable',
i18nDomains: 'experimental',
sharpImageService: {
support: 'limited',
Expand Down Expand Up @@ -387,7 +389,7 @@ export default function createIntegration(args?: Options): AstroIntegration {
),
),
dir,
buildOutput: finalBuildOutput,
buildOutput: _isFullyStatic ? 'static' : 'server',
assets,
});

Expand All @@ -402,6 +404,11 @@ export default function createIntegration(args?: Options): AstroIntegration {
}
}

// For fully static sites, remove the worker directory as it's not needed
if (_isFullyStatic) {
await rm(_config.build.server, { recursive: true, force: true });
}

// Delete this variable so the preview server opens the server build.
delete process.env.CLOUDFLARE_VITE_BUILD;
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import cloudflare from '@astrojs/cloudflare';
import { defineConfig } from 'astro/config';

export default defineConfig({
adapter: cloudflare(),
output: 'static',
});
12 changes: 12 additions & 0 deletions packages/integrations/cloudflare/test/fixtures/static/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@test/astro-cloudflare-static",
"version": "0.0.0",
"private": true,
"scripts": {
"build": "astro build"
},
"dependencies": {
"@astrojs/cloudflare": "workspace:*",
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<head>
<title>Static Site</title>
</head>
<body>
<h1>Static Site</h1>
<p>This is a fully static Astro site using the Cloudflare adapter.</p>
</body>
</html>
21 changes: 21 additions & 0 deletions packages/integrations/cloudflare/test/static.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { describe, it } from 'node:test';
import { loadFixture } from './_test-utils.js';
import assert from 'node:assert/strict';
import { existsSync } from 'node:fs';
import { fileURLToPath } from 'node:url';

describe('Static output', () => {
let fixture;

it('should not output a _worker.js directory for fully static sites', async () => {
fixture = await loadFixture({
root: './fixtures/static',
});

await fixture.build();

const workerExists = existsSync(fileURLToPath(new URL('_worker.js', fixture.config.outDir)));

assert.ok(!workerExists, '_worker.js directory should not exist for static sites');
});
});
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.