Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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: 3 additions & 0 deletions packages/astro/src/config/entrypoint.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// IMPORTANT: this file is the entrypoint for "astro/config". Keep it as light as possible!

import type { SharpImageServiceConfig } from '../assets/services/sharp.js';

export { createConsoleLogger, createNodeLogger } from '../core/config/index.js';
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason to export this from here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch. It's not needed anymore


import type { ImageServiceConfig } from '../types/public/index.js';

export { defineAstroFontProvider, fontProviders } from '../assets/fonts/providers/index.js';
Expand Down
15 changes: 8 additions & 7 deletions packages/astro/src/container/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,15 @@ function createManifest(
};
}

const root = new URL(import.meta.url);
return {
hrefRoot: import.meta.url,
srcDir: manifest?.srcDir ?? ASTRO_CONFIG_DEFAULTS.srcDir,
buildClientDir: manifest?.buildClientDir ?? ASTRO_CONFIG_DEFAULTS.build.client,
buildServerDir: manifest?.buildServerDir ?? ASTRO_CONFIG_DEFAULTS.build.server,
publicDir: manifest?.publicDir ?? ASTRO_CONFIG_DEFAULTS.publicDir,
outDir: manifest?.outDir ?? ASTRO_CONFIG_DEFAULTS.outDir,
cacheDir: manifest?.cacheDir ?? ASTRO_CONFIG_DEFAULTS.cacheDir,
rootDir: root,
srcDir: manifest?.srcDir ?? new URL(ASTRO_CONFIG_DEFAULTS.srcDir, root),
buildClientDir: manifest?.buildClientDir ?? new URL(ASTRO_CONFIG_DEFAULTS.build.client, root),
buildServerDir: manifest?.buildServerDir ?? new URL(ASTRO_CONFIG_DEFAULTS.build.server, root),
publicDir: manifest?.publicDir ?? new URL(ASTRO_CONFIG_DEFAULTS.publicDir, root),
outDir: manifest?.outDir ?? new URL(ASTRO_CONFIG_DEFAULTS.outDir, root),
cacheDir: manifest?.cacheDir ?? new URL(ASTRO_CONFIG_DEFAULTS.cacheDir, root),
trailingSlash: manifest?.trailingSlash ?? ASTRO_CONFIG_DEFAULTS.trailingSlash,
buildFormat: manifest?.buildFormat ?? ASTRO_CONFIG_DEFAULTS.build.format,
compressHTML: manifest?.compressHTML ?? ASTRO_CONFIG_DEFAULTS.compressHTML,
Expand Down
7 changes: 7 additions & 0 deletions packages/astro/src/core/app/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ export function deserializeManifest(
return { onRequest: NOOP_MIDDLEWARE_FN };
},
...serializedManifest,
rootDir: new URL(serializedManifest.rootDir),
srcDir: new URL(serializedManifest.srcDir),
publicDir: new URL(serializedManifest.publicDir),
outDir: new URL(serializedManifest.outDir),
cacheDir: new URL(serializedManifest.cacheDir),
buildClientDir: new URL(serializedManifest.buildClientDir),
buildServerDir: new URL(serializedManifest.buildServerDir),
assets,
componentMetadata,
inlinedScripts,
Expand Down
81 changes: 49 additions & 32 deletions packages/astro/src/core/app/dev/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
MiddlewareNotAResponse,
NoMatchingStaticPathFound,
} from '../../errors/errors-data.js';
import { type AstroError, isAstroError } from '../../errors/index.js';
import { type AstroError, createSafeError, isAstroError } from '../../errors/index.js';
import type { Logger } from '../../logger/core.js';
import { req } from '../../messages.js';
import { loadMiddleware } from '../../middleware/loadMiddleware.js';
Expand All @@ -43,18 +43,18 @@ import type { SSRManifest } from '../types.js';
import { DevPipeline } from './pipeline.js';

export class DevApp extends BaseApp<DevPipeline> {
settings: AstroSettings;
settings?: AstroSettings;
logger: Logger;
loader: ModuleLoader;
loader?: ModuleLoader;
manifestData: RoutesList;
currentRenderContext: RenderContext | undefined = undefined;
constructor(
manifest: SSRManifest,
streaming = true,
settings: AstroSettings,
logger: Logger,
loader: ModuleLoader,
manifestData: RoutesList,
loader?: ModuleLoader,
settings?: AstroSettings,
) {
super(manifest, streaming, settings, logger, loader, manifestData);
this.settings = settings;
Expand All @@ -66,11 +66,11 @@ export class DevApp extends BaseApp<DevPipeline> {
static async create(
manifest: SSRManifest,
routesList: RoutesList,
settings: AstroSettings,
logger: Logger,
loader: ModuleLoader,
loader?: ModuleLoader,
settings?: AstroSettings,
): Promise<DevApp> {
return new DevApp(manifest, true, settings, logger, loader, routesList);
return new DevApp(manifest, true, logger, routesList, loader, settings);
}

createPipeline(
Expand Down Expand Up @@ -104,14 +104,13 @@ export class DevApp extends BaseApp<DevPipeline> {
incomingResponse,
isHttps,
}: HandleRequest): Promise<void> {
const { config } = this.pipeline;
const origin = `${isHttps ? 'https' : 'http'}://${
incomingRequest.headers[':authority'] ?? incomingRequest.headers.host
}`;

const url = new URL(origin + incomingRequest.url);
let pathname: string;
if (config.trailingSlash === 'never' && !incomingRequest.url) {
if (this.manifest.trailingSlash === 'never' && !incomingRequest.url) {
pathname = '';
} else {
// We already have a middleware that checks if there's an incoming URL that has invalid URI, so it's safe
Expand All @@ -120,10 +119,10 @@ export class DevApp extends BaseApp<DevPipeline> {
}

// Add config.base back to url before passing it to SSR
url.pathname = removeTrailingForwardSlash(config.base) + url.pathname;
url.pathname = removeTrailingForwardSlash(this.manifest.base) + url.pathname;
if (
url.pathname.endsWith('/') &&
!shouldAppendForwardSlash(config.trailingSlash, config.build.format)
!shouldAppendForwardSlash(this.manifest.trailingSlash, this.manifest.buildFormat)
) {
url.pathname = url.pathname.slice(0, -1);
}
Expand All @@ -145,7 +144,12 @@ export class DevApp extends BaseApp<DevPipeline> {
controller,
pathname,
async run() {
const matchedRoute = await matchRoute(pathname, self.manifestData, self.pipeline);
const matchedRoute = await matchRoute(
pathname,
self.manifestData,
self.pipeline,
self.manifest,
);
const resolvedPathname = matchedRoute?.resolvedPathname ?? pathname;
return await self.handleRoute({
matchedRoute,
Expand All @@ -157,13 +161,16 @@ export class DevApp extends BaseApp<DevPipeline> {
});
},
onError(_err) {
const { error, errorWithMetadata } = recordServerError(
self.loader,
config,
self.logger,
_err,
);
handle500Response(self.loader, incomingResponse, errorWithMetadata);
const error = createSafeError(_err);
if (self.loader) {
const { errorWithMetadata } = recordServerError(
self.loader,
self.manifest,
self.logger,
error,
);
handle500Response(self.loader, incomingResponse, errorWithMetadata);
}
return error;
},
});
Expand All @@ -178,7 +185,7 @@ export class DevApp extends BaseApp<DevPipeline> {
pathname,
}: HandleRoute): Promise<void> {
const timeStart = performance.now();
const { config, loader, logger } = this.pipeline;
const { loader, logger } = this.pipeline;

if (!matchedRoute) {
// This should never happen, because ensure404Route will add a 404 route if none exists.
Expand Down Expand Up @@ -207,7 +214,7 @@ export class DevApp extends BaseApp<DevPipeline> {
});

// Set user specified headers to response object.
for (const [name, value] of Object.entries(config.server.headers ?? {})) {
for (const [name, value] of Object.entries(this.settings?.config.server.headers ?? {})) {
if (value) incomingResponse.setHeader(name, value);
}

Expand Down Expand Up @@ -276,7 +283,12 @@ export class DevApp extends BaseApp<DevPipeline> {
response.body === null &&
response.headers.get(REROUTE_DIRECTIVE_HEADER) !== 'no'
) {
const fourOhFourRoute = await matchRoute('/404', this.manifestData, this.pipeline);
const fourOhFourRoute = await matchRoute(
'/404',
this.manifestData,
this.pipeline,
this.manifest,
);
if (fourOhFourRoute) {
renderContext = await this.createRenderContext({
locals,
Expand Down Expand Up @@ -327,8 +339,8 @@ export class DevApp extends BaseApp<DevPipeline> {
response.status >= 300 &&
response.status < 400 &&
routeIsRedirect(route) &&
!config.build.redirects &&
this.pipeline.settings.buildOutput === 'static'
!this.settings?.config.build.redirects &&
this.settings?.buildOutput === 'static'
) {
// If we're here, it means that the calling static redirect that was configured by the user
// We try to replicate the same behaviour that we provide during a static build
Expand Down Expand Up @@ -397,7 +409,7 @@ export class DevApp extends BaseApp<DevPipeline> {
}

try {
const filePath500 = new URL(`./${custom500.component}`, this.settings.config.root);
const filePath500 = new URL(`./${custom500.component}`, this.manifest.rootDir);
const preloaded500Component = await this.pipeline.preload(custom500, filePath500);
const renderContext = await this.createRenderContext({
locals,
Expand Down Expand Up @@ -479,11 +491,16 @@ async function matchRoute(
pathname: string,
routesList: RoutesList,
pipeline: DevPipeline,
manifest: SSRManifest,
): Promise<MatchedRoute | undefined> {
const { config, logger, routeCache, serverLike, settings } = pipeline;
const { logger, routeCache, serverLike } = pipeline;
const matches = matchAllRoutes(pathname, routesList);

const preloadedMatches = await getSortedPreloadedMatches({ pipeline, matches, settings });
const preloadedMatches = await getSortedPreloadedMatches({
pipeline,
matches,
manifest,
});

for await (const { route: maybeRoute, filePath } of preloadedMatches) {
// attempt to get static paths
Expand All @@ -496,8 +513,8 @@ async function matchRoute(
pathname: pathname,
logger,
serverLike,
base: config.base,
trailingSlash: config.trailingSlash,
base: manifest.base,
trailingSlash: manifest.trailingSlash,
});
return {
route: maybeRoute,
Expand All @@ -519,7 +536,7 @@ async function matchRoute(
const altPathname = pathname.replace(/\/index\.html$/, '/').replace(/\.html$/, '');

if (altPathname !== pathname) {
return await matchRoute(altPathname, routesList, pipeline);
return await matchRoute(altPathname, routesList, pipeline, manifest);
}

if (matches.length) {
Expand All @@ -536,7 +553,7 @@ async function matchRoute(
const custom404 = getCustom404Route(routesList);

if (custom404) {
const filePath = new URL(`./${custom404.component}`, config.root);
const filePath = new URL(`./${custom404.component}`, manifest.rootDir);

return {
route: custom404,
Expand Down
9 changes: 4 additions & 5 deletions packages/astro/src/core/app/dev/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,23 @@ import { DevApp } from './app.js';
export { DevApp };

export default async function createExports(
settings: AstroSettings,
controller: DevServerController,
loader: ModuleLoader,
settings?: AstroSettings,
loader?: ModuleLoader,
) {
const logger = new Logger({
dest: nodeLogDestination,
level: 'info',
});
const routesList: RoutesList = { routes: routes.map((r: RouteInfo) => r.routeData) };
const app = await DevApp.create(manifest, routesList, settings, logger, loader);

const app = await DevApp.create(manifest, routesList, logger, loader, settings);
return {
handler(incomingRequest: http.IncomingMessage, incomingResponse: http.ServerResponse) {
app.handleRequest({
controller,
incomingRequest,
incomingResponse,
isHttps: loader.isHttps(),
isHttps: loader?.isHttps() ?? false,
});
},
};
Expand Down
Loading
Loading