Skip to content

Commit

Permalink
[ci] format
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewp authored and astrobot-houston committed Jun 5, 2023
1 parent 57f8d14 commit eb45957
Show file tree
Hide file tree
Showing 33 changed files with 225 additions and 202 deletions.
32 changes: 16 additions & 16 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,21 +453,19 @@ export interface AstroUserConfig {
*/
cacheDir?: string;



/**
* @docs
* @name redirects (Experimental)
* @type {RedirectConfig}
* @default `{}`
* @version 2.6.0
* @description Specify a mapping of redirects where the key is the route to match
* and the value is the path to redirect to.
* and the value is the path to redirect to.
*
* You can redirect both static and dynamic routes, but only to the same kind of route.
* For example you cannot have a `'/article': '/blog/[...slug]'` redirect.
*
*
*
*
* ```js
* {
* redirects: {
Expand All @@ -477,16 +475,16 @@ export interface AstroUserConfig {
* }
* ```
*
*
*
* For statically-generated sites with no adapter installed, this will produce a client redirect using a [`<meta http-equiv="refresh">` tag](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta#http-equiv) and does not support status codes.
*
* When using SSR or with a static adapter in `output: static`
* mode, status codes are supported.
* Astro will serve redirected GET requests with a status of `301`
* and use a status of `308` for any other request method.
*
*
* You can customize the [redirection status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#redirection_messages) using an object in the redirect config:
*
*
* ```js
* {
* redirects: {
Expand Down Expand Up @@ -791,8 +789,8 @@ export interface AstroUserConfig {
* Specifies whether redirects will be output to HTML during the build.
* This option only applies to `output: 'static'` mode; in SSR redirects
* are treated the same as all responses.
*
* This option is mostly meant to be used by adapters that have special
*
* This option is mostly meant to be used by adapters that have special
* configuration files for redirects and do not need/want HTML based redirects.
*
* ```js
Expand Down Expand Up @@ -1270,7 +1268,7 @@ export interface AstroUserConfig {
* }
* ```
*/
redirects?: boolean;
redirects?: boolean;
};

// Legacy options to be removed
Expand Down Expand Up @@ -1924,10 +1922,12 @@ export interface RoutePart {
spread: boolean;
}

type RedirectConfig = string | {
status: ValidRedirectStatus;
destination: string;
}
type RedirectConfig =
| string
| {
status: ValidRedirectStatus;
destination: string;
};

export interface RouteData {
route: string;
Expand All @@ -1947,7 +1947,7 @@ export interface RouteData {

export type RedirectRouteData = RouteData & {
redirect: string;
}
};

export type SerializedRouteData = Omit<RouteData, 'generate' | 'pattern'> & {
generate: undefined;
Expand Down
10 changes: 6 additions & 4 deletions packages/astro/src/core/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { consoleLogDestination } from '../logger/console.js';
import { error, type LogOptions } from '../logger/core.js';
import { callMiddleware } from '../middleware/callMiddleware.js';
import { prependForwardSlash, removeTrailingForwardSlash } from '../path.js';
import { RedirectComponentInstance } from '../redirects/index.js';
import {
createEnvironment,
createRenderContext,
Expand All @@ -28,7 +29,6 @@ import {
createStylesheetElementSet,
} from '../render/ssr-element.js';
import { matchRoute } from '../routing/match.js';
import { RedirectComponentInstance } from '../redirects/index.js';
export { deserializeManifest } from './common.js';

const clientLocalsSymbol = Symbol.for('astro.locals');
Expand Down Expand Up @@ -172,12 +172,14 @@ export class App {
}

async #getModuleForRoute(route: RouteData): Promise<ComponentInstance> {
if(route.type === 'redirect') {
if (route.type === 'redirect') {
return RedirectComponentInstance;
} else {
const importComponentInstance = this.#manifest.pageMap.get(route.component);
if(!importComponentInstance) {
throw new Error(`Unexpectedly unable to find a component instance for route ${route.route}`);
if (!importComponentInstance) {
throw new Error(
`Unexpectedly unable to find a component instance for route ${route.route}`
);
}
const built = await importComponentInstance();
return built.page();
Expand Down
46 changes: 27 additions & 19 deletions packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import type {
EndpointOutput,
ImageTransform,
MiddlewareResponseHandler,
RedirectRouteData,
RouteData,
RouteType,
SSRError,
Expand All @@ -24,9 +23,9 @@ import {
} from '../../assets/generate.js';
import {
eachPageDataFromEntryPoint,
eachRedirectPageData,
hasPrerenderedPages,
type BuildInternals,
eachRedirectPageData,
} from '../../core/build/internal.js';
import {
prependForwardSlash,
Expand All @@ -40,10 +39,14 @@ import { callEndpoint, createAPIContext, throwIfRedirectNotAllowed } from '../en
import { AstroError } from '../errors/index.js';
import { debug, info } from '../logger/core.js';
import { callMiddleware } from '../middleware/callMiddleware.js';
import {
getRedirectLocationOrThrow,
RedirectComponentInstance,
routeIsRedirect,
} from '../redirects/index.js';
import { createEnvironment, createRenderContext, renderPage } from '../render/index.js';
import { callGetStaticPaths } from '../render/route-cache.js';
import { getRedirectLocationOrThrow, RedirectComponentInstance, routeIsRedirect } from '../redirects/index.js';
import {
import {
createAssetLink,
createModuleScriptsSet,
createStylesheetElementSet,
Expand All @@ -52,7 +55,12 @@ import { createRequest } from '../request.js';
import { matchRoute } from '../routing/match.js';
import { getOutputFilename } from '../util.js';
import { getOutDirWithinCwd, getOutFile, getOutFolder } from './common.js';
import { cssOrder, getPageDataByComponent, mergeInlineCss, getEntryFilePathFromComponentPath } from './internal.js';
import {
cssOrder,
getEntryFilePathFromComponentPath,
getPageDataByComponent,
mergeInlineCss,
} from './internal.js';
import type {
PageBuildData,
SinglePageBuiltModule,
Expand All @@ -62,24 +70,24 @@ import type {
import { getTimeStat } from './util.js';

const StaticMiddlewareInstance: AstroMiddlewareInstance<unknown> = {
onRequest: (ctx, next) => next()
onRequest: (ctx, next) => next(),
};

function createEntryURL(filePath: string, outFolder: URL) {
return new URL('./' + filePath + `?time=${Date.now()}`, outFolder);
}
}

async function getEntryForRedirectRoute(
route: RouteData,
internals: BuildInternals,
outFolder: URL
): Promise<SinglePageBuiltModule> {
if(route.type !== 'redirect') {
if (route.type !== 'redirect') {
throw new Error(`Expected a redirect route.`);
}
if(route.redirectRoute) {
if (route.redirectRoute) {
const filePath = getEntryFilePathFromComponentPath(internals, route.redirectRoute.component);
if(filePath) {
if (filePath) {
const url = createEntryURL(filePath, outFolder);
const ssrEntryPage: SinglePageBuiltModule = await import(url.toString());
return ssrEntryPage;
Expand All @@ -89,8 +97,8 @@ async function getEntryForRedirectRoute(
return {
page: () => Promise.resolve(RedirectComponentInstance),
middleware: StaticMiddlewareInstance,
renderers: []
}
renderers: [],
};
}

function shouldSkipDraft(pageModule: ComponentInstance, settings: AstroSettings): boolean {
Expand Down Expand Up @@ -143,13 +151,13 @@ export async function generatePages(opts: StaticBuildOptions, internals: BuildIn
if (ssr) {
for (const [pageData, filePath] of eachPageDataFromEntryPoint(internals)) {
if (pageData.route.prerender) {
const ssrEntryURLPage =createEntryURL(filePath, outFolder);
const ssrEntryURLPage = createEntryURL(filePath, outFolder);
const ssrEntryPage: SinglePageBuiltModule = await import(ssrEntryURLPage.toString());

await generatePage(opts, internals, pageData, ssrEntryPage, builtPaths);
}
}
for(const pageData of eachRedirectPageData(internals)) {
for (const pageData of eachRedirectPageData(internals)) {
const entry = await getEntryForRedirectRoute(pageData.route, internals, outFolder);
await generatePage(opts, internals, pageData, entry, builtPaths);
}
Expand All @@ -160,7 +168,7 @@ export async function generatePages(opts: StaticBuildOptions, internals: BuildIn

await generatePage(opts, internals, pageData, ssrEntryPage, builtPaths);
}
for(const pageData of eachRedirectPageData(internals)) {
for (const pageData of eachRedirectPageData(internals)) {
const entry = await getEntryForRedirectRoute(pageData.route, internals, outFolder);
await generatePage(opts, internals, pageData, entry, builtPaths);
}
Expand Down Expand Up @@ -208,7 +216,7 @@ async function generatePage(
ssrEntry: SinglePageBuiltModule,
builtPaths: Set<string>
) {
if(routeIsRedirect(pageData.route) &&!opts.settings.config.experimental.redirects) {
if (routeIsRedirect(pageData.route) && !opts.settings.config.experimental.redirects) {
throw new Error(`To use redirects first set experimental.redirects to \`true\``);
}

Expand Down Expand Up @@ -578,17 +586,17 @@ async function generatePath(
throw err;
}

if(response.status >= 300 && response.status < 400) {
if (response.status >= 300 && response.status < 400) {
// If redirects is set to false, don't output the HTML
if(!opts.settings.config.build.redirects) {
if (!opts.settings.config.build.redirects) {
return;
}
const location = getRedirectLocationOrThrow(response.headers);
body = `<!doctype html>
<title>Redirecting to: ${location}</title>
<meta http-equiv="refresh" content="0;url=${location}" />`;
// A dynamic redirect, set the location so that integrations know about it.
if(pageData.route.type !== 'redirect') {
if (pageData.route.type !== 'redirect') {
pageData.route.redirect = location;
}
} else {
Expand Down
10 changes: 7 additions & 3 deletions packages/astro/src/core/build/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import type { SSRResult } from '../../@types/astro';
import type { PageOptions } from '../../vite-plugin-astro/types';
import { prependForwardSlash, removeFileExtension } from '../path.js';
import { viteID } from '../util.js';
import { ASTRO_PAGE_EXTENSION_POST_PATTERN, ASTRO_PAGE_MODULE_ID, getVirtualModulePageIdFromPath } from './plugins/plugin-pages.js';
import {
ASTRO_PAGE_EXTENSION_POST_PATTERN,
ASTRO_PAGE_MODULE_ID,
getVirtualModulePageIdFromPath,
} from './plugins/plugin-pages.js';
import type { PageBuildData, StylesheetAsset, ViteID } from './types';

export interface BuildInternals {
Expand Down Expand Up @@ -218,8 +222,8 @@ export function* eachPageData(internals: BuildInternals) {
}

export function* eachRedirectPageData(internals: BuildInternals) {
for(const pageData of eachPageData(internals)) {
if(pageData.route.type === 'redirect') {
for (const pageData of eachPageData(internals)) {
if (pageData.route.type === 'redirect') {
yield pageData;
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/core/build/plugins/plugin-pages.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { extname } from 'node:path';
import type { Plugin as VitePlugin } from 'vite';
import { routeIsRedirect } from '../../redirects/index.js';
import { addRollupInput } from '../add-rollup-input.js';
import { type BuildInternals } from '../internal.js';
import type { AstroBuildPlugin } from '../plugin';
import type { StaticBuildOptions } from '../types';
import { MIDDLEWARE_MODULE_ID } from './plugin-middleware.js';
import { routeIsRedirect } from '../../redirects/index.js';
import { RENDERERS_MODULE_ID } from './plugin-renderers.js';

export const ASTRO_PAGE_MODULE_ID = '@astro-page:';
Expand Down Expand Up @@ -44,7 +44,7 @@ function vitePluginPages(opts: StaticBuildOptions, internals: BuildInternals): V
const inputs: Set<string> = new Set();

for (const [path, pageData] of Object.entries(opts.allPages)) {
if(routeIsRedirect(pageData.route)) {
if (routeIsRedirect(pageData.route)) {
continue;
}
inputs.add(getVirtualModulePageNameFromPath(path));
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/core/build/plugins/plugin-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import { isHybridOutput } from '../../../prerender/utils.js';
import { BEFORE_HYDRATION_SCRIPT_ID, PAGE_SCRIPT_ID } from '../../../vite-plugin-scripts/index.js';
import type { SerializedRouteInfo, SerializedSSRManifest } from '../../app/types';
import { joinPaths, prependForwardSlash } from '../../path.js';
import { routeIsRedirect } from '../../redirects/index.js';
import { serializeRouteData } from '../../routing/index.js';
import { addRollupInput } from '../add-rollup-input.js';
import { getOutFile, getOutFolder } from '../common.js';
import { cssOrder, mergeInlineCss, type BuildInternals } from '../internal.js';
import type { AstroBuildPlugin } from '../plugin';
import type { StaticBuildOptions } from '../types';
import { MIDDLEWARE_MODULE_ID } from './plugin-middleware.js';
import { routeIsRedirect } from '../../redirects/index.js';
import { getVirtualModulePageNameFromPath } from './plugin-pages.js';
import { RENDERERS_MODULE_ID } from './plugin-renderers.js';

Expand Down Expand Up @@ -57,7 +57,7 @@ function vitePluginSSR(
const pageMap: string[] = [];

for (const [path, pageData] of Object.entries(allPages)) {
if(routeIsRedirect(pageData.route)) {
if (routeIsRedirect(pageData.route)) {
continue;
}
const virtualModuleName = getVirtualModulePageNameFromPath(path);
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export function resolveFlags(flags: Partial<Flags>): CLIFlags {
experimentalMiddleware:
typeof flags.experimentalMiddleware === 'boolean' ? flags.experimentalMiddleware : undefined,
experimentalRedirects:
typeof flags.experimentalRedirects === 'boolean' ? flags.experimentalRedirects : undefined
typeof flags.experimentalRedirects === 'boolean' ? flags.experimentalRedirects : undefined,
};
}

Expand Down
12 changes: 6 additions & 6 deletions packages/astro/src/core/errors/errors-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -750,12 +750,12 @@ See https://docs.astro.build/en/guides/server-side-rendering/ for more informati
`\`Astro.glob(${globStr})\` did not return any matching files. Check the pattern for typos.`,
},
/**
* @docs
* @see
* - [Astro.redirect](https://docs.astro.build/en/guides/server-side-rendering/#astroredirect)
* @description
* A redirect must be given a location with the `Location` header.
*/
* @docs
* @see
* - [Astro.redirect](https://docs.astro.build/en/guides/server-side-rendering/#astroredirect)
* @description
* A redirect must be given a location with the `Location` header.
*/
RedirectWithNoLocation: {
title: 'A redirect must be given a location with the `Location` header.',
code: 3037,
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/core/redirects/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { ComponentInstance } from '../../@types/astro';
export const RedirectComponentInstance: ComponentInstance = {
default() {
return new Response(null, {
status: 301
status: 301,
});
}
},
};
Loading

0 comments on commit eb45957

Please sign in to comment.