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
3 changes: 2 additions & 1 deletion packages/astro/src/container/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ function createManifest(
trailingSlash: manifest?.trailingSlash ?? ASTRO_CONFIG_DEFAULTS.trailingSlash,
buildFormat: manifest?.buildFormat ?? ASTRO_CONFIG_DEFAULTS.build.format,
compressHTML: manifest?.compressHTML ?? ASTRO_CONFIG_DEFAULTS.compressHTML,
serverLike: manifest?.serverLike ?? false,
assets: manifest?.assets ?? new Set(),
assetsPrefix: manifest?.assetsPrefix ?? undefined,
entryModules: manifest?.entryModules ?? {},
Expand Down Expand Up @@ -254,6 +255,7 @@ type AstroContainerManifest = Pick<
| 'cacheDir'
| 'csp'
| 'allowedDomains'
| 'serverLike'
>;

type AstroContainerConstructor = {
Expand Down Expand Up @@ -287,7 +289,6 @@ export class experimental_AstroContainer {
}),
manifest: createManifest(manifest, renderers),
streaming,
serverLike: true,
renderers: renderers ?? manifest?.renderers ?? [],
resolve: async (specifier: string) => {
if (this.#withManifest) {
Expand Down
20 changes: 6 additions & 14 deletions packages/astro/src/container/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,18 @@ export class ContainerPipeline extends Pipeline {
SinglePageBuiltModule
>();

getName(): string {
return 'ContainerPipeline';
}

static create({
logger,
manifest,
renderers,
resolve,
serverLike,
streaming,
}: Pick<
ContainerPipeline,
'logger' | 'manifest' | 'renderers' | 'resolve' | 'serverLike' | 'streaming'
>) {
return new ContainerPipeline(
logger,
manifest,
'development',
renderers,
resolve,
serverLike,
streaming,
);
}: Pick<ContainerPipeline, 'logger' | 'manifest' | 'renderers' | 'resolve' | 'streaming'>) {
return new ContainerPipeline(logger, manifest, 'development', renderers, resolve, streaming);
}

componentMetadata(_routeData: RouteData): Promise<SSRResult['componentMetadata']> | void {}
Expand Down
7 changes: 5 additions & 2 deletions packages/astro/src/core/app/dev/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import { findRouteToRewrite } from '../../routing/rewrite.js';
type DevPipelineCreate = Pick<DevPipeline, 'logger' | 'manifest' | 'streaming'>;

export class DevPipeline extends Pipeline {
getName(): string {
return 'DevPipeline';
}

static create({ logger, manifest, streaming }: DevPipelineCreate) {
async function resolve(specifier: string): Promise<string> {
if (specifier.startsWith('/')) {
Expand All @@ -28,7 +32,6 @@ export class DevPipeline extends Pipeline {
'production',
manifest.renderers,
resolve,
true,
streaming,
undefined,
undefined,
Expand Down Expand Up @@ -126,7 +129,7 @@ export class DevPipeline extends Pipeline {
trailingSlash: this.manifest.trailingSlash,
buildFormat: this.manifest.buildFormat,
base: this.manifest.base,
outDir: this.serverLike ? this.manifest.buildClientDir : this.manifest.outDir,
outDir: this.manifest?.serverLike ? this.manifest.buildClientDir : this.manifest.outDir,
});

const componentInstance = await this.getComponentByRoute(routeData);
Expand Down
7 changes: 5 additions & 2 deletions packages/astro/src/core/app/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import {
import { findRouteToRewrite } from '../routing/rewrite.js';

export class AppPipeline extends Pipeline {
getName(): string {
return 'AppPipeline';
}

static create({
logger,
manifest,
Expand All @@ -32,7 +36,6 @@ export class AppPipeline extends Pipeline {
'production',
manifest.renderers,
resolve,
true,
streaming,
undefined,
undefined,
Expand Down Expand Up @@ -84,7 +87,7 @@ export class AppPipeline extends Pipeline {
trailingSlash: this.manifest.trailingSlash,
buildFormat: this.manifest.buildFormat,
base: this.manifest.base,
outDir: this.serverLike ? this.manifest.buildClientDir : this.manifest.outDir,
outDir: this.manifest?.serverLike ? this.manifest.buildClientDir : this.manifest.outDir,
});

const componentInstance = await this.getComponentByRoute(routeData);
Expand Down
7 changes: 7 additions & 0 deletions packages/astro/src/core/app/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ export type SSRManifest = {
compressHTML: boolean;
assetsPrefix?: AssetsPrefix;
renderers: SSRLoadedRenderer[];
/**
* Based on Astro config's `output` option, `true` if "server" or "hybrid".
*
* Whether this application is SSR-like. If so, this has some implications, such as
* the creation of `dist/client` and `dist/server` folders.
*/
serverLike: boolean;
/**
* Map of directive name (e.g. `load`) to the directive script code
*/
Expand Down
10 changes: 6 additions & 4 deletions packages/astro/src/core/base-pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ export abstract class Pipeline {
readonly runtimeMode: RuntimeMode,
readonly renderers: SSRLoadedRenderer[],
readonly resolve: (s: string) => Promise<string>,
/**
* Based on Astro config's `output` option, `true` if "server" or "hybrid".
*/
readonly serverLike: boolean,

readonly streaming: boolean,
/**
* Used to provide better error messages for `Astro.clientAddress`
Expand Down Expand Up @@ -106,6 +103,11 @@ export abstract class Pipeline {
*/
abstract getComponentByRoute(routeData: RouteData): Promise<ComponentInstance>;

/**
* The current name of the pipeline. Useful for debugging
*/
abstract getName(): string;

/**
* Resolves the middleware from the manifest, and returns the `onRequest` function. If `onRequest` isn't there,
* it returns a no-op function
Expand Down
6 changes: 3 additions & 3 deletions packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export async function generatePages(
const prerenderEntryFileName = internals.prerenderEntryFileName;
if (!prerenderEntryFileName) {
throw new Error(
`Prerender entry filename not found in build internals. This is likely a bug in Astro.`
`Prerender entry filename not found in build internals. This is likely a bug in Astro.`,
);
}
const prerenderEntryUrl = new URL(prerenderEntryFileName, prerenderOutputDir);
Expand Down Expand Up @@ -296,7 +296,7 @@ async function getPathsForRoute(
pipeline: BuildPipeline,
builtPaths: Set<string>,
): Promise<Array<string>> {
const { logger, options, routeCache, serverLike, manifest } = pipeline;
const { logger, options, routeCache, manifest } = pipeline;
let paths: Array<string> = [];
if (route.pathname) {
paths.push(route.pathname);
Expand All @@ -317,7 +317,7 @@ async function getPathsForRoute(
route,
routeCache,
logger,
ssr: serverLike,
ssr: manifest.serverLike,
base: manifest.base,
trailingSlash: manifest.trailingSlash,
}).catch((err) => {
Expand Down
29 changes: 16 additions & 13 deletions packages/astro/src/core/build/pipeline.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import type { ComponentInstance } from '../../types/astro.js';
import type { RewritePayload } from '../../types/public/common.js';
import type {
RouteData,
SSRElement,
SSRResult,
} from '../../types/public/internal.js';
import { VIRTUAL_PAGE_MODULE_ID, VIRTUAL_PAGE_RESOLVED_MODULE_ID } from '../../vite-plugin-pages/index.js';
import type { RouteData, SSRElement, SSRResult } from '../../types/public/internal.js';
import {
VIRTUAL_PAGE_MODULE_ID,
VIRTUAL_PAGE_RESOLVED_MODULE_ID,
} from '../../vite-plugin-pages/index.js';
import { getVirtualModulePageName } from '../../vite-plugin-pages/util.js';
import { BEFORE_HYDRATION_SCRIPT_ID, PAGE_SCRIPT_ID } from '../../vite-plugin-scripts/index.js';
import type { SSRManifest } from '../app/types.js';
Expand All @@ -24,6 +23,10 @@ import type { SinglePageBuiltModule, StaticBuildOptions } from './types.js';
* The build pipeline is responsible to gather the files emitted by the SSR build and generate the pages by executing these files.
*/
export class BuildPipeline extends Pipeline {
getName(): string {
return 'BuildPipeline';
}

#componentsInterner: WeakMap<RouteData, SinglePageBuiltModule> = new WeakMap<
RouteData,
SinglePageBuiltModule
Expand Down Expand Up @@ -69,17 +72,14 @@ export class BuildPipeline extends Pipeline {
return assetLink;
}

const serverLike = settings.buildOutput === 'server';
// We can skip streaming in SSG for performance as writing as strings are faster
const streaming = serverLike;
super(
options.logger,
manifest,
options.runtimeMode,
manifest.renderers,
resolve,
serverLike,
streaming,
manifest.serverLike,
);
}

Expand Down Expand Up @@ -143,9 +143,12 @@ export class BuildPipeline extends Pipeline {
retrieveRoutesToGenerate(): Map<RouteData, string> {
const pages = new Map<RouteData, string>();

for(const { routeData } of this.manifest.routes) {
for (const { routeData } of this.manifest.routes) {
// Here, we take the component path and transform it in the virtual module name
const moduleSpecifier = getVirtualModulePageName(VIRTUAL_PAGE_RESOLVED_MODULE_ID, routeData.component);
const moduleSpecifier = getVirtualModulePageName(
VIRTUAL_PAGE_RESOLVED_MODULE_ID,
routeData.component,
);
// We retrieve the original JS module
const filePath = this.internals.entrySpecifierToBundleMap.get(moduleSpecifier);
if (filePath) {
Expand Down Expand Up @@ -187,7 +190,7 @@ export class BuildPipeline extends Pipeline {
trailingSlash: this.config.trailingSlash,
buildFormat: this.config.build.format,
base: this.config.base,
outDir: this.serverLike ? this.manifest.buildClientDir : this.manifest.outDir,
outDir: this.manifest.serverLike ? this.manifest.buildClientDir : this.manifest.outDir,
});

const componentInstance = await this.getComponentByRoute(routeData);
Expand Down
19 changes: 11 additions & 8 deletions packages/astro/src/core/build/plugins/plugin-manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import type * as vite from 'vite';
import { getAssetsPrefix } from '../../../assets/utils/getAssetsPrefix.js';
import { normalizeTheLocale } from '../../../i18n/index.js';
import { runHookBuildSsr } from '../../../integrations/hooks.js';
import {
SERIALIZED_MANIFEST_RESOLVED_ID,
} from '../../../manifest/serialized.js';
import { SERIALIZED_MANIFEST_RESOLVED_ID } from '../../../manifest/serialized.js';
import { BEFORE_HYDRATION_SCRIPT_ID, PAGE_SCRIPT_ID } from '../../../vite-plugin-scripts/index.js';
import { toFallbackType } from '../../app/common.js';
import { serializeRouteData, toRoutingStrategy } from '../../app/index.js';
Expand Down Expand Up @@ -68,15 +66,19 @@ const replaceExp = new RegExp(`['"]${MANIFEST_REPLACE}['"]`, 'g');
export async function manifestBuildPostHook(
options: StaticBuildOptions,
internals: BuildInternals,
{ ssrOutputs, prerenderOutputs, mutate }: {
ssrOutputs: vite.Rollup.RollupOutput[],
prerenderOutputs: vite.Rollup.RollupOutput[],
{
ssrOutputs,
prerenderOutputs,
mutate,
}: {
ssrOutputs: vite.Rollup.RollupOutput[];
prerenderOutputs: vite.Rollup.RollupOutput[];
mutate: (chunk: OutputChunk, envs: ['server'], code: string) => void;
},
) {
const manifest = await createManifest(options, internals);

if(ssrOutputs.length > 0) {
if (ssrOutputs.length > 0) {
let manifestEntryChunk: OutputChunk | undefined;

// Find the serialized manifest chunk in SSR outputs
Expand Down Expand Up @@ -245,7 +247,7 @@ async function buildManifest(
});

// Add the built .html file as a staticFile
if(route.prerender && route.pathname) {
if (route.prerender && route.pathname) {
const outFolder = getOutFolder(opts.settings, route.pathname, route);
const outFile = getOutFile(opts.settings.config, outFolder, route.pathname, route);
const file = outFile.toString().replace(opts.settings.config.build.client.toString(), '');
Expand Down Expand Up @@ -319,6 +321,7 @@ async function buildManifest(
buildServerDir: opts.settings.config.build.server.toString(),
adapterName: opts.settings.adapter?.name ?? '',
routes,
serverLike: opts.settings.buildOutput === 'server',
site: settings.config.site,
base: settings.config.base,
userAssetsBase: settings.config?.vite?.base,
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/middleware/sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function sequence(...handlers: MiddlewareHandler[]): MiddlewareHandler {
// This case isn't valid because when building for SSR, the prerendered route disappears from the server output because it becomes an HTML file,
// so Astro can't retrieve it from the emitted manifest.
if (
pipeline.serverLike === true &&
pipeline.manifest.serverLike === true &&
handleContext.isPrerendered === false &&
routeData.prerender === true
) {
Expand Down
8 changes: 4 additions & 4 deletions packages/astro/src/core/render-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export class RenderContext {
slots: Record<string, any> = {},
): Promise<Response> {
const { middleware, pipeline } = this;
const { logger, serverLike, streaming, manifest } = pipeline;
const { logger, streaming, manifest } = pipeline;

const props =
Object.keys(this.props).length > 0
Expand All @@ -175,7 +175,7 @@ export class RenderContext {
routeCache: this.pipeline.routeCache,
pathname: this.pathname,
logger,
serverLike,
serverLike: manifest.serverLike,
base: manifest.base,
trailingSlash: manifest.trailingSlash,
});
Expand Down Expand Up @@ -207,7 +207,7 @@ export class RenderContext {
// This case isn't valid because when building for SSR, the prerendered route disappears from the server output because it becomes an HTML file,
// so Astro can't retrieve it from the emitted manifest.
if (
this.pipeline.serverLike === true &&
this.pipeline.manifest.serverLike === true &&
this.routeData.prerender === false &&
routeData.prerender === true
) {
Expand Down Expand Up @@ -360,7 +360,7 @@ export class RenderContext {
// Allow i18n fallback rewrites - if the target route has fallback routes, this is likely an i18n scenario
const isI18nFallback = routeData.fallbackRoutes && routeData.fallbackRoutes.length > 0;
if (
this.pipeline.serverLike &&
this.pipeline.manifest.serverLike &&
!this.routeData.prerender &&
routeData.prerender &&
!isI18nFallback
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/manifest/serialized.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ async function createSerializedManifest(settings: AstroSettings): Promise<Serial
trailingSlash: settings.config.trailingSlash,
buildFormat: settings.config.build.format,
compressHTML: settings.config.compressHTML,
serverLike: settings.buildOutput === 'server',
assets: [],
entryModules: {},
routes: [],
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/vite-plugin-app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ async function matchRoute(
pipeline: AstroServerPipeline,
manifest: SSRManifest,
): Promise<MatchedRoute | undefined> {
const { logger, routeCache, serverLike } = pipeline;
const { logger, routeCache } = pipeline;
const matches = matchAllRoutes(pathname, routesList);

const preloadedMatches = await getSortedPreloadedMatches({
Expand All @@ -504,7 +504,7 @@ async function matchRoute(
routeCache,
pathname: pathname,
logger,
serverLike,
serverLike: pipeline.manifest.serverLike,
base: manifest.base,
trailingSlash: manifest.trailingSlash,
});
Expand Down
7 changes: 5 additions & 2 deletions packages/astro/src/vite-plugin-app/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ import { createResolve } from '../vite-plugin-astro-server/resolve.js';
import { PAGE_SCRIPT_ID } from '../vite-plugin-scripts/index.js';

export class AstroServerPipeline extends Pipeline {
getName(): string {
return 'AstroServerPipeline';
}

// renderers are loaded on every request,
// so it needs to be mutable here unlike in other environments
override renderers = new Array<SSRLoadedRenderer>();
Expand All @@ -45,9 +49,8 @@ export class AstroServerPipeline extends Pipeline {
readonly defaultRoutes = createDefaultRoutes(manifest),
) {
const resolve = createResolve(loader, manifest.rootDir);
const serverLike = settings?.buildOutput === 'server';
const streaming = true;
super(logger, manifest, 'development', [], resolve, serverLike, streaming);
super(logger, manifest, 'development', [], resolve, streaming);
}

static create(
Expand Down
Loading
Loading