Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
32 changes: 21 additions & 11 deletions packages/integrations/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ import {
} from './utils/cloudflare-module-loader.js';
import { createGetEnv } from './utils/env.js';
import { createRoutesFile, getParts } from './utils/generate-routes-json.js';
import { setImageConfig } from './utils/image-config.js';
import { type ImageService, setImageConfig } from './utils/image-config.js';

export type { Runtime } from './entrypoints/server.js';

export type Options = {
/** Options for handling images. */
imageService?: 'passthrough' | 'cloudflare' | 'compile' | 'custom';
imageService?: ImageService;
/** Configuration for `_routes.json` generation. A _routes.json file controls when your Function is invoked. This file will include three different properties:
*
* - version: Defines the version of the schema. Currently there is only one version of the schema (version 1), however, we may add more in the future and aim to be backwards compatible.
Expand Down Expand Up @@ -148,12 +148,17 @@ export default function createIntegration(args?: Options): AstroIntegration {
if (!session?.driver) {
const sessionDir = isBuild ? undefined : createCodegenDir();
const bindingName = args?.sessionKVBindingName ?? 'SESSION';

logger.info(
`Enabling sessions with ${isBuild ? 'Cloudflare KV' : 'filesystem storage'}. Be sure to define a KV binding named "${bindingName}".`,
);
logger.info(
`If you see the error "Invalid binding \`${bindingName}\`" in your build output, you need to add the binding to your wrangler config file.`,
`Enabling sessions with Cloudflare KV for production with the "${bindingName}" KV binding.`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is misleading because in dev it's not using CLoudflare KV. I'd move this inside the conditional block

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does say it's Cloudflare KV for production, so I wouldn't think that would apply in dev if I were reading that.

I am not against moving this into the conditional, though! I was thinking, and I guess most people wanting to use Sessions will either look into the docs and configure manually or see the Cloudflare adapter docs and just use the default config, so being reminded of this in dev might not be necessary.

);

if (isBuild) {
logger.info(
`If you see the error "Invalid binding \`${bindingName}\`" in your build output, you need to add the binding to your wrangler config file.`,
);
}

session = isBuild
? {
...session,
Expand Down Expand Up @@ -238,11 +243,16 @@ export default function createIntegration(args?: Options): AstroIntegration {
hybridOutput: 'stable',
staticOutput: 'unsupported',
i18nDomains: 'experimental',
sharpImageService: {
support: 'limited',
message:
'Cloudflare does not support sharp. You can use the `compile` image service to compile images at build time. It will not work for any on-demand rendered images.',
},
// For explicitly set image services, this hack is used to suppress the warning about using `sharp`
// by inferring the user is aware of its implications.
// TODO: If an option to supress the warnings for `supportedAstroFeatures` is added, we should use it instead.
sharpImageService: args?.imageService
? 'stable'
: {
support: 'limited',
message:
'Cloudflare does not support sharp at runtime, but the `compile` image service can be used to optimize images with sharp on pre-rendered pages during build time.',
},
envGetSecret: 'stable',
},
});
Expand Down
6 changes: 4 additions & 2 deletions packages/integrations/cloudflare/src/utils/image-config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { AstroConfig, AstroIntegrationLogger, HookParameters } from 'astro';
import { passthroughImageService, sharpImageService } from 'astro/config';

export type ImageService = 'passthrough' | 'cloudflare' | 'compile' | 'custom';

export function setImageConfig(
service: string,
service: ImageService,
config: AstroConfig['image'],
command: HookParameters<'astro:config:setup'>['command'],
logger: AstroIntegrationLogger,
Expand Down Expand Up @@ -35,7 +37,7 @@ export function setImageConfig(
default:
if (config.service.entrypoint === 'astro/assets/services/sharp') {
logger.warn(
`The current configuration does not support image optimization. To allow your project to build with the original, unoptimized images, the image service has been automatically switched to the 'noop' option. See https://docs.astro.build/en/reference/configuration-reference/#imageservice`,
`The current configuration does not support image optimization. To allow your project to build with the original, unoptimized images, the image service has been automatically switched to the 'passthrough' option. See https://docs.astro.build/en/reference/configuration-reference/#imageservice`,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit of an extra, the option name itself is not noop (although yes, it's a non-operational service). Mostly to make things consistent.

);
return { ...config, service: passthroughImageService() };
}
Expand Down