0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-03-31 23:31:30 -05:00

[ci] format

This commit is contained in:
Arsh 2024-03-08 16:40:20 +00:00 committed by astrobot-houston
parent dc87214141
commit dfdf6b3a0b
9 changed files with 62 additions and 44 deletions

View file

@ -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;

View file

@ -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 {};
}

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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.

View file

@ -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();

View file

@ -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;

View file

@ -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) {

View file

@ -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 () => {
const devServer = await fixture.startDevServer();
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 - dev', async () => {
const devServer = await fixture.startDevServer();
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 () => {
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);
});
});