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: 4 additions & 1 deletion packages/astro/src/content/vite-plugin-content-assets.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { extname } from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import type { Plugin } from 'vite';
import { isRunnableDevEnvironment, type Plugin } from 'vite';
import { getAssetsPrefix } from '../assets/utils/getAssetsPrefix.js';
import type { BuildInternals } from '../core/build/internal.js';
import type { AstroBuildPlugin } from '../core/build/plugin.js';
Expand Down Expand Up @@ -65,6 +65,9 @@ export function astroContentAssetPropagationPlugin({
}
},
configureServer(server) {
if (!isRunnableDevEnvironment(server.environments.ssr)) {
return;
}
devModuleLoader = createViteLoader(server);
},
async transform(_, id, options) {
Expand Down
5 changes: 4 additions & 1 deletion packages/astro/src/content/vite-plugin-content-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { extname } from 'node:path';
import { pathToFileURL } from 'node:url';
import * as devalue from 'devalue';
import type { PluginContext } from 'rollup';
import type { Plugin, RunnableDevEnvironment } from 'vite';
import { isRunnableDevEnvironment, type Plugin, type RunnableDevEnvironment } from 'vite';
import { getProxyCode } from '../assets/utils/proxy.js';
import { AstroError } from '../core/errors/errors.js';
import { AstroErrorData } from '../core/errors/index.js';
Expand Down Expand Up @@ -158,6 +158,9 @@ export const _internal = {
}
},
configureServer(viteServer) {
if (!isRunnableDevEnvironment(viteServer.environments.ssr)) {
return;
}
viteServer.watcher.on('all', async (event, entry) => {
if (CHOKIDAR_MODIFIED_EVENTS.includes(event)) {
const environment = viteServer.environments.ssr;
Expand Down
6 changes: 4 additions & 2 deletions packages/astro/src/core/dev/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { performance } from 'node:perf_hooks';
import { green } from 'kleur/colors';
import { gt, major, minor, patch } from 'semver';
import type * as vite from 'vite';
import { isRunnableDevEnvironment } from 'vite';
import { getDataStoreFile, globalContentLayer } from '../../content/content-layer.js';
import { attachContentServerListeners } from '../../content/index.js';
import { MutableDataStore } from '../../content/mutable-data-store.js';
Expand Down Expand Up @@ -95,8 +96,9 @@ export default async function dev(inlineConfig: AstroInlineConfig): Promise<DevS
if (!store) {
logger.error('content', 'Failed to create data store');
}

await attachContentServerListeners(restart.container);
if (isRunnableDevEnvironment(restart.container.viteServer.environments.ssr)) {
await attachContentServerListeners(restart.container);
}

const config = globalContentConfigObserver.get();
if (config.status === 'error') {
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/encryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export async function encodeKey(key: CryptoKey) {
*/
export async function decodeKey(encoded: string): Promise<CryptoKey> {
const bytes = decodeBase64(encoded);
return crypto.subtle.importKey('raw', Buffer.from(bytes), ALGORITHM, true, [
return crypto.subtle.importKey('raw', bytes.buffer as ArrayBuffer, ALGORITHM, true, [
'encrypt',
'decrypt',
]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import MagicString from 'magic-string';
import type { ConfigEnv, RunnableDevEnvironment, Plugin as VitePlugin } from 'vite';
import {
type ConfigEnv,
isRunnableDevEnvironment,
type RunnableDevEnvironment,
type Plugin as VitePlugin,
} from 'vite';
import type { AstroPluginOptions } from '../../types/astro.js';
import type { AstroPluginMetadata } from '../../vite-plugin-astro/index.js';
import { AstroError, AstroErrorData } from '../errors/index.js';
Expand All @@ -20,6 +25,9 @@ export function vitePluginServerIslands({ settings }: AstroPluginOptions): ViteP
command = _command;
},
configureServer(_server) {
if (!isRunnableDevEnvironment(_server.environments.ssr)) {
return;
}
ssrEnvironment = getRunnableEnvironment(_server);
},
resolveId(name) {
Expand Down
8 changes: 8 additions & 0 deletions packages/astro/src/vite-plugin-astro-server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { mkdir, readFile, writeFile } from 'node:fs/promises';
import { IncomingMessage } from 'node:http';
import { fileURLToPath } from 'node:url';
import type * as vite from 'vite';
import { isRunnableDevEnvironment } from 'vite';
import { toFallbackType } from '../core/app/common.js';
import { toRoutingStrategy } from '../core/app/index.js';
import type { SSRManifest, SSRManifestCSP, SSRManifestI18n } from '../core/app/types.js';
Expand Down Expand Up @@ -44,6 +45,12 @@ export default function createVitePluginAstroServer({
return {
name: 'astro:server',
async configureServer(viteServer) {
// Cloudflare handles its own requests
// TODO: let this handle non-runnable environments that don't intercept requests
if (!isRunnableDevEnvironment(viteServer.environments.ssr)) {
return;
}

const loader = createViteLoader(viteServer);
const entrypoint = settings.adapter?.devEntrypoint ?? 'astro/app/dev';
const createExports = await loader.import(entrypoint.toString());
Expand Down Expand Up @@ -137,6 +144,7 @@ export default function createVitePluginAstroServer({
response.end();
return;
}

localStorage.run(request, () => {
handler(request, response);
});
Expand Down
5 changes: 4 additions & 1 deletion packages/astro/src/vite-plugin-head/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ModuleInfo } from 'rollup';
import type * as vite from 'vite';
import type { DevEnvironment } from 'vite';
import { type DevEnvironment, isRunnableDevEnvironment } from 'vite';
import { getParentModuleInfos, getTopLevelPageModuleInfos } from '../core/build/graph.js';
import type { BuildInternals } from '../core/build/internal.js';
import type { AstroBuildPlugin } from '../core/build/plugin.js';
Expand Down Expand Up @@ -48,6 +48,9 @@ export default function configHeadVitePlugin(): vite.Plugin {
enforce: 'pre',
apply: 'serve',
configureServer(server) {
if (!isRunnableDevEnvironment(server.environments.ssr)) {
return;
}
environment = server.environments.ssr;
},
resolveId(source, importer) {
Expand Down
1 change: 1 addition & 0 deletions packages/integrations/cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"dependencies": {
"@astrojs/internal-helpers": "workspace:*",
"@astrojs/underscore-redirects": "workspace:*",
"@cloudflare/vite-plugin": "^1.12.2",
"@cloudflare/workers-types": "^4.20250903.0",
"tinyglobby": "^0.2.14",
"vite": "^6.3.5",
Expand Down
14 changes: 14 additions & 0 deletions packages/integrations/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
removeLeadingForwardSlash,
} from '@astrojs/internal-helpers/path';
import { createRedirectsFromAstroRoutes, printAsRedirects } from '@astrojs/underscore-redirects';
import { cloudflare as cfVitePlugin } from '@cloudflare/vite-plugin';
import type {
AstroConfig,
AstroIntegration,
Expand Down Expand Up @@ -192,6 +193,7 @@ export default function createIntegration(args?: Options): AstroIntegration {
session,
vite: {
plugins: [
command === 'build' ? undefined : cfVitePlugin({ viteEnvironment: { name: 'ssr' } }),
// https://developers.cloudflare.com/pages/functions/module-support/
// Allows imports of '.wasm', '.bin', and '.txt' file types
cloudflareModulePlugin,
Expand All @@ -205,6 +207,18 @@ export default function createIntegration(args?: Options): AstroIntegration {
return null;
},
},
{
enforce: 'post',
name: 'vite:cf-externals',
applyToEnvironment: (environment) => environment.name === 'ssr',
config(conf) {
if (conf.ssr) {
// Cloudflare does not support externalizing modules in the ssr environment
conf.ssr.external = undefined;
conf.ssr.noExternal = true;
}
},
},
],
},
image: setImageConfig(args?.imageService ?? 'compile', config.image, command, logger),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
worker-configuration.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// @ts-check
import cloudflare from '@astrojs/cloudflare';
import { defineConfig } from 'astro/config';


export default defineConfig({
adapter: cloudflare({
workerEntryPoint: {
path: 'src/worker.ts',
}
}),
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@test/astro-cloudflare-vite-plugin",
"version": "0.0.0",
"private": true,
"dependencies": {
"@astrojs/cloudflare": "workspace:*",
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { manifest } from "astro:serialized-manifest";
import { createExports } from "./worker";

export default createExports(manifest).default
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
const workerRuntime = globalThis.navigator?.userAgent
export const prerender = false
---
<html>
<head>
<title>astro-cloudflare-custom-entryfile</title>
</head>
<body>
<h1>astro-cloudflare-custom-entryfile</h1>
<p>Runtime: {workerRuntime}</p>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
_worker.js
_routes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { SSRManifest } from 'astro';

import { App } from 'astro/app';
import { handle, type Env } from '@astrojs/cloudflare/handler'
import type { ExportedHandler } from '@cloudflare/workers-types';

export function createExports(manifest: SSRManifest) {
const app = new App(manifest);
return {
default: {
async fetch(request, env, ctx) {
return await handle(manifest, app, request, env, ctx);
},
} satisfies ExportedHandler<Env>,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "astro/tsconfigs/strictest",
"include": [
".astro/types.d.ts",
"**/*"
],
"exclude": [
"dist"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compatibility_date": "2025-05-21",
"main": "src/dev.ts",
"name": "astro-cloudflare-custom-entryfile",
"assets": {
"directory": "./dist",
"binding": "ASSETS"
}
}
Loading
Loading