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
1 change: 1 addition & 0 deletions packages/astro/src/core/app/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export type { RoutesList } from '../../types/astro.js';
export { App } from './app.js';
export { BaseApp, type RenderErrorOptions, type RenderOptions } from './base.js';
export { deserializeManifest, fromRoutingStrategy, toRoutingStrategy } from './common.js';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// <reference types="vite/client" />
import { joinPaths } from '@astrojs/internal-helpers/path';
import type { ExternalImageService } from 'astro';
import { baseService } from 'astro/assets';
Expand Down Expand Up @@ -25,7 +26,6 @@ const service: ExternalImageService = {
}

const imageEndpoint = joinPaths(
// @ts-expect-error Can't recognise import.meta.env
import.meta.env.BASE_URL,
'/cdn-cgi/image',
resizingParams.join(','),
Expand Down
39 changes: 20 additions & 19 deletions packages/integrations/cloudflare/src/entrypoints/server.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import type { ExecutionContext, ExportedHandlerFetchHandler } from '@cloudflare/workers-types';
import type { SSRManifest } from 'astro';
/// <reference types="vite/client" />

import { App } from 'astro/app';
import { handle } from '../utils/handler.js';
import type { ExportedHandler } from '@cloudflare/workers-types';
import type { RouteInfo, SSRManifest } from 'astro';
import { App, type BaseApp, createConsoleLogger, DevApp, type RoutesList } from 'astro/app';
import { type Env, handle } from '../utils/handler.js';

type Env = {
[key: string]: unknown;
ASSETS: { fetch: (req: Request | string) => Promise<Response> };
};
export function createExports(manifest: SSRManifest, routes: RouteInfo[]) {
let app: BaseApp;

export function createExports(manifest: SSRManifest) {
const app = new App(manifest);

const fetch = async (
request: Parameters<ExportedHandlerFetchHandler>[0],
env: Env,
context: ExecutionContext,
) => {
return await handle(manifest, app, request, env, context);
if (import.meta.env.DEV) {
const routesList: RoutesList = { routes: routes.map((r: RouteInfo) => r.routeData) };
const logger = createConsoleLogger('debug');
app = new DevApp(manifest, true, logger, routesList);
} else {
app = new App(manifest);
}
return {
default: {
async fetch(request, env, ctx) {
return await handle(manifest, app, request, env, ctx);
},
} satisfies ExportedHandler<Env>,
};

return { default: { fetch } };
}
15 changes: 8 additions & 7 deletions packages/integrations/cloudflare/src/utils/handler.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
// @ts-expect-error - It is safe to expect the error here.
import { env as globalEnv } from 'cloudflare:workers';
import type {
Response as CfResponse,
CacheStorage as CloudflareCacheStorage,
ExecutionContext,
ExportedHandlerFetchHandler,
} from '@cloudflare/workers-types';
import type { SSRManifest } from 'astro';
import type { App } from 'astro/app';
import type { BaseApp } from 'astro/app';
import { setGetEnv } from 'astro/env/setup';
import { createGetEnv } from '../utils/env.js';

type Env = {
export type Env = {
[key: string]: unknown;
ASSETS: { fetch: (req: Request | string) => Promise<Response> };
ASSETS: { fetch: (req: Request | string) => Promise<CfResponse> };
};

setGetEnv(createGetEnv(globalEnv as Env));
Expand Down Expand Up @@ -40,11 +41,11 @@ declare global {

export async function handle(
manifest: SSRManifest,
app: App,
app: BaseApp,
request: Parameters<ExportedHandlerFetchHandler>[0],
env: Env,
context: ExecutionContext,
) {
): Promise<CfResponse> {
const { pathname } = new URL(request.url);
const bindingName = globalThis.__ASTRO_SESSION_BINDING_NAME;
// Assigning the KV binding to globalThis allows unstorage to access it for session storage.
Expand Down Expand Up @@ -94,7 +95,7 @@ export async function handle(
routeData,
locals,
prerenderedErrorPageFetch: async (url) => {
return env.ASSETS.fetch(url.replace(/\.html$/, ''));
return env.ASSETS.fetch(url.replace(/\.html$/, '')) as unknown as Response;
},
},
);
Expand All @@ -105,5 +106,5 @@ export async function handle(
}
}

return response;
return response as unknown as CfResponse;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import { fileURLToPath } from 'node:url';

export default defineConfig({
adapter: cloudflare({
workerEntryPoint: {
path: 'src/worker.ts',
},
imageService: 'cloudflare-binding',
}),
vite: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { manifest } from "astro:serialized-manifest";
import { routes } from "astro:routes"
import { createExports } from "./worker";
import { createExports } from "@astrojs/cloudflare/entrypoints/server.js";

export default createExports(manifest, routes).default

This file was deleted.

Loading