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/memory-route-filter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Reduces build memory by filtering routes per environment so each only builds the pages it needs
2 changes: 1 addition & 1 deletion .github/workflows/preview-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Preview release

on:
pull_request:
branches: [main]
branches: [main, next]
types: [labeled]

concurrency:
Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/assets/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ export async function prepareAssetsGenerationEnv(
const isServerOutput = settings.buildOutput === 'server';
let serverRoot: URL, clientRoot: URL;
if (isServerOutput) {
serverRoot = manifest.buildServerDir;
// Images are collected during prerender, which outputs to .prerender/ subdirectory
serverRoot = new URL('.prerender/', manifest.buildServerDir);
clientRoot = manifest.buildClientDir;
} else {
serverRoot = getOutDirWithinCwd(manifest.outDir);
Expand Down
33 changes: 32 additions & 1 deletion packages/astro/src/vite-plugin-pages/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Plugin as VitePlugin } from 'vite';
import { DEFAULT_COMPONENTS } from '../core/routing/default.js';
import { routeIsRedirect } from '../core/routing/index.js';
import type { RoutesList } from '../types/astro.js';
import type { RouteData } from '../types/public/internal.js';
import { VIRTUAL_PAGE_MODULE_ID } from './const.js';
import { getVirtualModulePageName } from './util.js';
import { ASTRO_VITE_ENVIRONMENT_NAMES } from '../core/constants.js';
Expand All @@ -13,6 +14,23 @@ interface PagesPluginOptions {
routesList: RoutesList;
}

/**
* Filters routes for a specific build environment.
* Redirects need their target route included so the redirect response can be generated at runtime.
*/
function getRoutesForEnvironment(routes: RouteData[], isPrerender: boolean): Set<RouteData> {
const result = new Set<RouteData>();
for (const route of routes) {
if (route.prerender === isPrerender) {
result.add(route);
}
if (route.redirectRoute) {
result.add(route.redirectRoute);
}
}
return result;
}

export function pluginPages({ routesList }: PagesPluginOptions): VitePlugin {
return {
name: '@astro/plugin-pages',
Expand Down Expand Up @@ -40,7 +58,20 @@ export function pluginPages({ routesList }: PagesPluginOptions): VitePlugin {
const pageMap: string[] = [];
let i = 0;

for (const route of routesList.routes) {
// Filter routes based on the build environment to reduce memory usage.
// Each environment only builds the pages it needs:
// - SSR environment: builds only on-demand rendered pages (prerender: false)
// - Prerender environment: builds only static pages (prerender: true)
// - Other environments (e.g. client): get all routes
const envName = this.environment.name;
const isSSR = envName === ASTRO_VITE_ENVIRONMENT_NAMES.ssr;
const isPrerender = envName === ASTRO_VITE_ENVIRONMENT_NAMES.prerender;
const routes =
isSSR || isPrerender
? getRoutesForEnvironment(routesList.routes, isPrerender)
: new Set(routesList.routes);

for (const route of routes) {
if (routeIsRedirect(route)) {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/test/test-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export default function ({
async render(request, { routeData, clientAddress, locals, addCookieHeader, prerenderedErrorPageFetch } = {}) {
const url = new URL(request.url);
if(this.#manifest.assets.has(url.pathname)) {
const filePath = new URL('../../client/' + this.removeBase(url.pathname), import.meta.url);
const filePath = new URL(this.removeBase(url.pathname).replace(/^\\//, ''), this.#manifest.buildClientDir);
const data = await fs.promises.readFile(filePath);
return new Response(data);
}
Expand Down