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

Speeds up `astro sync` by no longer starting the Cloudflare runtime during type generation
26 changes: 21 additions & 5 deletions packages/integrations/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export default function createIntegration({
const needsImagesBindingForDev = isCompile && command === 'dev';
const usesContentCollections = hasContentCollectionsConfig(config.srcDir);
const prebundleContentRuntime = command === 'dev' && usesContentCollections;
const isTypeGenPhase = command === 'build' || command === 'sync';

const adapterPluginConfig: Partial<PluginConfig> = {
config: cloudflareConfigCustomizer({
Expand Down Expand Up @@ -241,6 +242,21 @@ export default function createIntegration({
// include, and esbuildOptions (e.g. loader) entries are respected.
const userOptimizeDeps = config.vite?.optimizeDeps;

const cloudflareVitePlugins = cfVitePlugin({
...cfPluginConfig,
viteEnvironment: { name: 'ssr' },
assetsOnly: () => _buildOutput === 'static',
});
// `sync` and `build` both run type generation (build via its internal sync
// pass), which creates a temporary Vite server and fires `configureServer`
// the hook that boots the Cloudflare/workerd runtime. Drop it in both so
// type generation doesn't pay that startup cost. See #16332.
if (isTypeGenPhase) {
for (const plugin of cloudflareVitePlugins) {
plugin.configureServer = undefined;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

does this currently start the dev server during the build? I assume it probably does not.. but not obvious from the logic.

I'm betting that the server starts in sync by not in the build. in which case I would suggest getting rid of the variable and just inlining the conditions with a comment, as the variable could make someone thing something is wrong that the configureServer function is removed from the build.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

build does start the Cloudflare runtime during its internal type-gen pass: it calls syncInternal({ command: 'build' }) (core/build/index.ts#L174), which spins up a temporary Vite server — and creating that server fires every plugin’s configureServer, including @cloudflare/vite-plugin’s. The only way to skip it is the programmatic sync: false path, which isn’t what astro build does.

So the cleanest fix is to strip configureServer from the plugin during this phase. The other time saver is skipping dep pre-bundling in the same phase.

We could inline the condition in both places or maybe rename the variable to something like isTypeGenPhase or skipCloudflareRuntime. What do you think?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Variable name doesn't matter so much as understanding what you just said, so if you could just add a comment that explains why its needed for both that will be enough.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Clarity added in 95b9339

}
}

updateConfig({
build: {
redirects: false,
Expand All @@ -251,11 +267,7 @@ export default function createIntegration({
...(prerenderEnvironment === 'node' && command === 'dev'
? [createNodePrerenderPlugin()]
: []),
cfVitePlugin({
...cfPluginConfig,
viteEnvironment: { name: 'ssr' },
assetsOnly: () => _buildOutput === 'static',
}),
cloudflareVitePlugins,
{
name: '@astrojs/cloudflare:cf-imports',
enforce: 'pre',
Expand All @@ -271,6 +283,10 @@ export default function createIntegration({
{
name: '@astrojs/cloudflare:environment',
configEnvironment(environmentName, _options) {
// Skip dependency pre-bundling during type generation (see `isTypeGenPhase` above).
if (isTypeGenPhase) {
return { optimizeDeps: { noDiscovery: true, include: [] } };
}
const isServerEnvironment = ['astro', 'ssr', 'prerender'].includes(
environmentName,
);
Expand Down
Loading