diff options
9 files changed, 60 insertions, 42 deletions
| diff --git a/packages/astro/src/core/app/index.ts b/packages/astro/src/core/app/index.ts index bf0c61232..0e2d745ef 100644 --- a/packages/astro/src/core/app/index.ts +++ b/packages/astro/src/core/app/index.ts @@ -1,5 +1,10 @@ +import type { +	ComponentInstance, +	ManifestData, +	RouteData, +	SSRManifest, +} from '../../@types/astro.js';  import { normalizeTheLocale } from '../../i18n/index.js'; -import type { ComponentInstance, ManifestData, RouteData, SSRManifest } from '../../@types/astro.js';  import type { SinglePageBuiltModule } from '../build/types.js';  import {  	DEFAULT_404_COMPONENT, @@ -23,9 +28,9 @@ import {  import { RedirectSinglePageBuiltModule } from '../redirects/index.js';  import { RenderContext } from '../render-context.js';  import { createAssetLink } from '../render/ssr-element.js'; +import { ensure404Route } from '../routing/astro-designed-error-pages.js';  import { matchRoute } from '../routing/match.js';  import { AppPipeline } from './pipeline.js'; -import { ensure404Route } from '../routing/astro-designed-error-pages.js';  export { deserializeManifest } from './common.js';  export interface RenderOptions { @@ -479,9 +484,10 @@ export class App {  	async #getModuleForRoute(route: RouteData): Promise<SinglePageBuiltModule> {  		if (route.component === DEFAULT_404_COMPONENT) {  			return { -				page: async () => ({ default: () => new Response(null, { status: 404 }) }) as ComponentInstance, -				renderers: [] -			} +				page: async () => +					({ default: () => new Response(null, { status: 404 }) }) as ComponentInstance, +				renderers: [], +			};  		}  		if (route.type === 'redirect') {  			return RedirectSinglePageBuiltModule; diff --git a/packages/astro/src/core/render/params-and-props.ts b/packages/astro/src/core/render/params-and-props.ts index eeae1a9b4..fb02a997d 100644 --- a/packages/astro/src/core/render/params-and-props.ts +++ b/packages/astro/src/core/render/params-and-props.ts @@ -25,7 +25,11 @@ export async function getProps(opts: GetParamsAndPropsOptions): Promise<Props> {  		return {};  	} -	if (routeIsRedirect(route) || routeIsFallback(route) || route.component === DEFAULT_404_COMPONENT) { +	if ( +		routeIsRedirect(route) || +		routeIsFallback(route) || +		route.component === DEFAULT_404_COMPONENT +	) {  		return {};  	} diff --git a/packages/astro/src/core/routing/astro-designed-error-pages.ts b/packages/astro/src/core/routing/astro-designed-error-pages.ts index ac2b08274..e3f2fd553 100644 --- a/packages/astro/src/core/routing/astro-designed-error-pages.ts +++ b/packages/astro/src/core/routing/astro-designed-error-pages.ts @@ -1,8 +1,8 @@ -import type { ManifestData } from "../../@types/astro.js"; -import { DEFAULT_404_COMPONENT } from "../constants.js"; +import type { ManifestData } from '../../@types/astro.js'; +import { DEFAULT_404_COMPONENT } from '../constants.js';  export function ensure404Route(manifest: ManifestData) { -	if (!manifest.routes.some(route => route.route === '/404')) { +	if (!manifest.routes.some((route) => route.route === '/404')) {  		manifest.routes.push({  			component: DEFAULT_404_COMPONENT,  			generate: () => '', @@ -14,7 +14,7 @@ export function ensure404Route(manifest: ManifestData) {  			route: '/404',  			fallbackRoutes: [],  			isIndex: false, -		}) +		});  	}  	return manifest;  } diff --git a/packages/astro/src/runtime/server/render/astro/instance.ts b/packages/astro/src/runtime/server/render/astro/instance.ts index 2119823c4..5f6b26c72 100644 --- a/packages/astro/src/runtime/server/render/astro/instance.ts +++ b/packages/astro/src/runtime/server/render/astro/instance.ts @@ -57,7 +57,8 @@ export class AstroComponentInstance {  			await this.init(this.result);  		} -		let value: Promise<AstroFactoryReturnValue> | AstroFactoryReturnValue | undefined = this.returnValue; +		let value: Promise<AstroFactoryReturnValue> | AstroFactoryReturnValue | undefined = +			this.returnValue;  		if (isPromise(value)) {  			value = await value;  		} diff --git a/packages/astro/src/vite-plugin-astro-server/pipeline.ts b/packages/astro/src/vite-plugin-astro-server/pipeline.ts index 157f0c603..0b7859846 100644 --- a/packages/astro/src/vite-plugin-astro-server/pipeline.ts +++ b/packages/astro/src/vite-plugin-astro-server/pipeline.ts @@ -22,8 +22,8 @@ import { PAGE_SCRIPT_ID } from '../vite-plugin-scripts/index.js';  import { getStylesForURL } from './css.js';  import { getComponentMetadata } from './metadata.js';  import { createResolve } from './resolve.js'; -import { getScriptsForURL } from './scripts.js';  import { default404Page } from './response.js'; +import { getScriptsForURL } from './scripts.js';  export class DevPipeline extends Pipeline {  	// renderers are loaded on every request, @@ -138,7 +138,7 @@ export class DevPipeline extends Pipeline {  	async preload(filePath: URL) {  		const { loader } = this;  		if (filePath.href === new URL(DEFAULT_404_COMPONENT, this.config.root).href) { -			return { default: default404Page } as any as ComponentInstance +			return { default: default404Page } as any as ComponentInstance;  		}  		// Important: This needs to happen first, in case a renderer provides polyfills. diff --git a/packages/astro/src/vite-plugin-astro-server/plugin.ts b/packages/astro/src/vite-plugin-astro-server/plugin.ts index be0f6a8ed..b08bcb4eb 100644 --- a/packages/astro/src/vite-plugin-astro-server/plugin.ts +++ b/packages/astro/src/vite-plugin-astro-server/plugin.ts @@ -9,6 +9,7 @@ import { AstroError, AstroErrorData } from '../core/errors/index.js';  import { patchOverlay } from '../core/errors/overlay.js';  import type { Logger } from '../core/logger/core.js';  import { createViteLoader } from '../core/module-loader/index.js'; +import { ensure404Route } from '../core/routing/astro-designed-error-pages.js';  import { createRouteManifest } from '../core/routing/index.js';  import { toRoutingStrategy } from '../i18n/utils.js';  import { baseMiddleware } from './base.js'; @@ -17,7 +18,6 @@ import { recordServerError } from './error.js';  import { DevPipeline } from './pipeline.js';  import { handleRequest } from './request.js';  import { setRouteError } from './server-state.js'; -import { ensure404Route } from '../core/routing/astro-designed-error-pages.js';  export interface AstroPluginOptions {  	settings: AstroSettings; @@ -36,10 +36,12 @@ export default function createVitePluginAstroServer({  			const loader = createViteLoader(viteServer);  			const manifest = createDevelopmentManifest(settings);  			const pipeline = DevPipeline.create({ loader, logger, manifest, settings }); -			let manifestData: ManifestData = ensure404Route(createRouteManifest({ settings, fsMod }, logger)); +			let manifestData: ManifestData = ensure404Route( +				createRouteManifest({ settings, fsMod }, logger) +			);  			const controller = createController({ loader });  			const localStorage = new AsyncLocalStorage(); -			 +  			/** rebuild the route cache + manifest, as needed. */  			function rebuildManifest(needsManifestRebuild: boolean) {  				pipeline.clearRouteCache(); diff --git a/packages/astro/src/vite-plugin-astro-server/response.ts b/packages/astro/src/vite-plugin-astro-server/response.ts index 6dccc753f..bdf0e3c15 100644 --- a/packages/astro/src/vite-plugin-astro-server/response.ts +++ b/packages/astro/src/vite-plugin-astro-server/response.ts @@ -23,15 +23,16 @@ export async function handle404Response(  	writeHtmlResponse(res, 404, html);  } -export async function default404Page( -	{ pathname }: { pathname: string } -) { -	return new Response(notFoundTemplate({ -		statusCode: 404, -		title: 'Not found', -		tabTitle: '404: Not Found', -		pathname, -	}), { status: 404, headers: { 'Content-Type': 'text/html; charset=utf-8' } }); +export async function default404Page({ pathname }: { pathname: string }) { +	return new Response( +		notFoundTemplate({ +			statusCode: 404, +			title: 'Not found', +			tabTitle: '404: Not Found', +			pathname, +		}), +		{ status: 404, headers: { 'Content-Type': 'text/html; charset=utf-8' } } +	);  }  // mark the function as an AstroComponentFactory for the rendering internals  default404Page.isAstroComponentFactory = true; diff --git a/packages/astro/src/vite-plugin-astro-server/route.ts b/packages/astro/src/vite-plugin-astro-server/route.ts index a38130dca..f10d9e184 100644 --- a/packages/astro/src/vite-plugin-astro-server/route.ts +++ b/packages/astro/src/vite-plugin-astro-server/route.ts @@ -1,6 +1,10 @@  import type http from 'node:http';  import type { ComponentInstance, ManifestData, RouteData } from '../@types/astro.js'; -import { DEFAULT_404_COMPONENT, REROUTE_DIRECTIVE_HEADER, clientLocalsSymbol } from '../core/constants.js'; +import { +	DEFAULT_404_COMPONENT, +	REROUTE_DIRECTIVE_HEADER, +	clientLocalsSymbol, +} from '../core/constants.js';  import { AstroErrorData, isAstroError } from '../core/errors/index.js';  import { req } from '../core/messages.js';  import { loadMiddleware } from '../core/middleware/loadMiddleware.js'; @@ -94,18 +98,18 @@ export async function matchRoute(  	}  	const custom404 = getCustom404Route(manifestData); -	 +  	if (custom404 && custom404.component === DEFAULT_404_COMPONENT) {  		const component: ComponentInstance = { -			default: default404Page -		} +			default: default404Page, +		};  		return {  			route: custom404,  			filePath: new URL(`file://${custom404.component}`),  			resolvedPathname: pathname,  			preloadedComponent: component,  			mod: component, -		} +		};  	}  	if (custom404) { diff --git a/packages/astro/test/virtual-routes.test.js b/packages/astro/test/virtual-routes.test.js index 2c9286e8e..e0a64aefe 100644 --- a/packages/astro/test/virtual-routes.test.js +++ b/packages/astro/test/virtual-routes.test.js @@ -10,21 +10,21 @@ describe('virtual routes - dev', () => {  		fixture = await loadFixture({  			root: './fixtures/virtual-routes/',  		}); -        await fixture.build(); +		await fixture.build();  	}); -     -    it('should render a virtual route - dev', async () => { + +	it('should render a virtual route - dev', async () => {  		const devServer = await fixture.startDevServer(); -        const response = await fixture.fetch('/virtual'); -        const html = await response.text(); -        assert.equal(html.includes('Virtual!!'), true); +		const response = await fixture.fetch('/virtual'); +		const html = await response.text(); +		assert.equal(html.includes('Virtual!!'), true);  		await devServer.stop(); -    }); +	}); -    it('should render a virtual route - app', async () => { +	it('should render a virtual route - app', async () => {  		const app = await fixture.loadTestAdapterApp(); -        const response = await app.render(new Request('https://example.com/virtual')); -        const html = await response.text(); -        assert.equal(html.includes('Virtual!!'), true); -    }); +		const response = await app.render(new Request('https://example.com/virtual')); +		const html = await response.text(); +		assert.equal(html.includes('Virtual!!'), true); +	});  }); | 
