Skip to content

Commit

Permalink
[ci] format
Browse files Browse the repository at this point in the history
  • Loading branch information
lilnasy authored and astrobot-houston committed Mar 8, 2024
1 parent dc87214 commit dfdf6b3
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 42 deletions.
16 changes: 11 additions & 5 deletions packages/astro/src/core/app/index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion packages/astro/src/core/render/params-and-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
}

Expand Down
8 changes: 4 additions & 4 deletions packages/astro/src/core/routing/astro-designed-error-pages.ts
Original file line number Diff line number Diff line change
@@ -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: () => '',
Expand All @@ -14,7 +14,7 @@ export function ensure404Route(manifest: ManifestData) {
route: '/404',
fallbackRoutes: [],
isIndex: false,
})
});
}
return manifest;
}
3 changes: 2 additions & 1 deletion packages/astro/src/runtime/server/render/astro/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/vite-plugin-astro-server/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 5 additions & 3 deletions packages/astro/src/vite-plugin-astro-server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand All @@ -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();
Expand Down
19 changes: 10 additions & 9 deletions packages/astro/src/vite-plugin-astro-server/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 9 additions & 5 deletions packages/astro/src/vite-plugin-astro-server/route.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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) {
Expand Down
24 changes: 12 additions & 12 deletions packages/astro/test/virtual-routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit dfdf6b3

Please sign in to comment.