Skip to content

Commit

Permalink
chore: rename to rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed May 6, 2024
1 parent 1b0f01a commit 128bbcb
Show file tree
Hide file tree
Showing 22 changed files with 52 additions and 52 deletions.
20 changes: 10 additions & 10 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ export interface AstroGlobal<
* }
* ```
*/
reroute: AstroSharedContext['reroute'];
rewrite: AstroSharedContext['rewrite'];
/**
* The <Astro.self /> element allows a component to reference itself recursively.
*
Expand Down Expand Up @@ -1933,19 +1933,19 @@ export interface AstroUserConfig {

/**
* @docs
* @name experimental.rerouting
* @name experimental.rewriting
* @type {boolean}
* @default `false`
* @version 4.8.0
* @description
*
* Enables the use of rerouting features in Astro pages, Endpoints and Astro middleware:
* Enables the use of rewriting features in Astro pages, Endpoints and Astro middleware:
*
* ```astro
* ---
* // src/pages/dashboard.astro
* if (!Astro.props.allowed) {
* return Astro.reroute("/")
* return Astro.rewrite("/")
* }
* ---
* ```
Expand All @@ -1954,7 +1954,7 @@ export interface AstroUserConfig {
* // src/pages/api.js
* export function GET(ctx) {
* if (!ctx.locals.allowed) {
* return ctx.reroute("/")
* return ctx.rewrite("/")
* }
* }
* ```
Expand All @@ -1969,7 +1969,7 @@ export interface AstroUserConfig {
* }
* ```
*/
rerouting: boolean;
rewriting: boolean;
};
}

Expand Down Expand Up @@ -2543,7 +2543,7 @@ interface AstroSharedContext<
* }
* ```
*/
reroute(reroutePayload: ReroutePayload): Promise<Response>;
rewrite(rewritePayload: RewritePayload): Promise<Response>;

/**
* Object accessed via Astro middleware
Expand Down Expand Up @@ -2672,7 +2672,7 @@ export interface APIContext<
* }
* ```
*/
reroute: AstroSharedContext['reroute'];
rewrite: AstroSharedContext['rewrite'];

/**
* An object that middlewares can use to store extra information related to the request.
Expand Down Expand Up @@ -2866,9 +2866,9 @@ export interface AstroIntegration {
};
}

export type ReroutePayload = string | URL | Request;
export type RewritePayload = string | URL | Request;

export type MiddlewareNext = (reroutePayload?: ReroutePayload) => Promise<Response>;
export type MiddlewareNext = (reroutePayload?: RewritePayload) => Promise<Response>;
export type MiddlewareHandler = (
context: APIContext,
next: MiddlewareNext
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/core/app/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
SSRElement,
SSRResult,
ComponentInstance,
ReroutePayload,
RewritePayload,
} from '../../@types/astro.js';
import { Pipeline } from '../base-pipeline.js';
import { DEFAULT_404_COMPONENT } from '../constants.js';
Expand Down Expand Up @@ -71,7 +71,7 @@ export class AppPipeline extends Pipeline {
return module.page();
}

async tryReroute(payload: ReroutePayload): Promise<[RouteData, ComponentInstance]> {
async tryRewrite(payload: RewritePayload): Promise<[RouteData, ComponentInstance]> {
let foundRoute;

for (const route of this.#manifestData!.routes) {
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/app/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export type SSRManifest = {
middleware: MiddlewareHandler;
checkOrigin: boolean;
// TODO: remove once the experimental flag is removed
reroutingEnabled: boolean;
rewritingEnabled: boolean;
};

export type SSRManifestI18n = {
Expand Down
6 changes: 3 additions & 3 deletions packages/astro/src/core/base-pipeline.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {
ComponentInstance,
MiddlewareHandler,
ReroutePayload,
RewritePayload,
RouteData,
RuntimeMode,
SSRLoadedRenderer,
Expand Down Expand Up @@ -69,9 +69,9 @@ export abstract class Pipeline {
*
* - if not `RouteData` is found
*
* @param {ReroutePayload} reroutePayload
* @param {RewritePayload} rewritePayload
*/
abstract tryReroute(reroutePayload: ReroutePayload): Promise<[RouteData, ComponentInstance]>;
abstract tryRewrite(rewritePayload: RewritePayload): Promise<[RouteData, ComponentInstance]>;

/**
* Tells the pipeline how to retrieve a component give a `RouteData`
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ async function getPathsForRoute(
const label = staticPaths.length === 1 ? 'page' : 'pages';
logger.debug(
'build',
`├── ${bold(green(''))} ${route.component}${magenta(`[${staticPaths.length} ${label}]`)}`
`├── ${bold(green(''))} ${route.component}${magenta(`[${staticPaths.length} ${label}]`)}`
);

paths = staticPaths
Expand Down Expand Up @@ -556,7 +556,7 @@ function createBuildManifest(
i18n: i18nManifest,
buildFormat: settings.config.build.format,
middleware,
reroutingEnabled: settings.config.experimental.rerouting,
rewritingEnabled: settings.config.experimental.rewriting,
checkOrigin: settings.config.experimental.security?.csrfProtection?.origin ?? false,
};
}
4 changes: 2 additions & 2 deletions packages/astro/src/core/build/pipeline.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {
ComponentInstance,
ReroutePayload,
RewritePayload,
RouteData,
SSRLoadedRenderer,
SSRResult,
Expand Down Expand Up @@ -272,7 +272,7 @@ export class BuildPipeline extends Pipeline {
}
}

async tryReroute(payload: ReroutePayload): Promise<[RouteData, ComponentInstance]> {
async tryRewrite(payload: RewritePayload): Promise<[RouteData, ComponentInstance]> {
let foundRoute: RouteData | undefined;
// options.manifest is the actual type that contains the information
for (const route of this.options.manifest.routes) {
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/build/plugins/plugin-manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,6 @@ function buildManifest(
i18n: i18nManifest,
buildFormat: settings.config.build.format,
checkOrigin: settings.config.experimental.security?.csrfProtection?.origin ?? false,
reroutingEnabled: settings.config.experimental.rerouting,
rewritingEnabled: settings.config.experimental.rewriting,
};
}
4 changes: 2 additions & 2 deletions packages/astro/src/core/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const ASTRO_CONFIG_DEFAULTS = {
globalRoutePriority: false,
i18nDomains: false,
security: {},
rerouting: false,
rewriting: false,
},
} satisfies AstroUserConfig & { server: { open: boolean } };

Expand Down Expand Up @@ -526,7 +526,7 @@ export const AstroConfigSchema = z.object({
.optional()
.default(ASTRO_CONFIG_DEFAULTS.experimental.security),
i18nDomains: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.experimental.i18nDomains),
rerouting: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.experimental.rerouting),
rewriting: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.experimental.rewriting),
})
.strict(
`Invalid or outdated experimental feature.\nCheck for incorrect spelling or outdated Astro version.\nSee https://docs.astro.build/en/reference/configuration-reference/#experimental-flags for a list of all current experiments.`
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/core/middleware/callMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {
APIContext,
MiddlewareHandler,
MiddlewareNext,
ReroutePayload,
RewritePayload,
} from '../../@types/astro.js';
import { AstroError, AstroErrorData } from '../errors/index.js';
import type { Logger } from '../logger/core.js';
Expand Down Expand Up @@ -46,7 +46,7 @@ export async function callMiddleware(
apiContext: APIContext,
responseFunction: (
apiContext: APIContext,
reroutePayload?: ReroutePayload
reroutePayload?: RewritePayload
) => Promise<Response> | Response,
// TODO: remove these two arguments once rerouting goes out of experimental
enableRerouting: boolean,
Expand Down
6 changes: 3 additions & 3 deletions packages/astro/src/core/middleware/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { APIContext, MiddlewareHandler, Params, ReroutePayload } from '../../@types/astro.js';
import type { APIContext, MiddlewareHandler, Params, RewritePayload } from '../../@types/astro.js';
import {
computeCurrentLocale,
computePreferredLocale,
Expand Down Expand Up @@ -47,7 +47,7 @@ function createContext({
const route = url.pathname;

// TODO verify that this function works in an edge middleware environment
const reroute = (_reroutePayload: ReroutePayload) => {
const reroute = (_reroutePayload: RewritePayload) => {
// return dummy response
return Promise.resolve(new Response(null));
};
Expand All @@ -59,7 +59,7 @@ function createContext({
site: undefined,
generator: `Astro v${ASTRO_VERSION}`,
props: {},
reroute,
rewrite: reroute,
redirect(path, status) {
return new Response(null, {
status: status || 302,
Expand Down
6 changes: 3 additions & 3 deletions packages/astro/src/core/middleware/sequence.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { APIContext, MiddlewareHandler, ReroutePayload } from '../../@types/astro.js';
import type { APIContext, MiddlewareHandler, RewritePayload } from '../../@types/astro.js';
import { defineMiddleware } from './index.js';
import { AstroCookies } from '../cookies/cookies.js';

Expand All @@ -20,15 +20,15 @@ export function sequence(...handlers: MiddlewareHandler[]): MiddlewareHandler {
/**
* This variable is used to carry the rerouting payload across middleware functions.
*/
let carriedPayload: ReroutePayload | undefined = undefined;
let carriedPayload: RewritePayload | undefined = undefined;
return applyHandle(0, context);

function applyHandle(i: number, handleContext: APIContext) {
const handle = filtered[i];
// @ts-expect-error
// SAFETY: Usually `next` always returns something in user land, but in `sequence` we are actually
// doing a loop over all the `next` functions, and eventually we call the last `next` that returns the `Response`.
const result = handle(handleContext, async (payload: ReroutePayload) => {
const result = handle(handleContext, async (payload: RewritePayload) => {
if (i < length - 1) {
if (payload) {
let newRequest;
Expand Down
22 changes: 11 additions & 11 deletions packages/astro/src/core/render-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
ComponentInstance,
MiddlewareHandler,
MiddlewareNext,
ReroutePayload,
RewritePayload,
RouteData,
SSRResult,
} from '../@types/astro.js';
Expand Down Expand Up @@ -111,11 +111,11 @@ export class RenderContext {
statusText: 'Loop Detected',
});
}
const lastNext = async (ctx: APIContext, payload?: ReroutePayload) => {
const lastNext = async (ctx: APIContext, payload?: RewritePayload) => {
if (payload) {
if (this.pipeline.manifest.reroutingEnabled) {
if (this.pipeline.manifest.rewritingEnabled) {
try {
const [routeData, component] = await pipeline.tryReroute(payload);
const [routeData, component] = await pipeline.tryRewrite(payload);
this.routeData = routeData;
componentInstance = component;
} catch (e) {
Expand Down Expand Up @@ -178,7 +178,7 @@ export class RenderContext {
middleware,
apiContext,
lastNext,
this.pipeline.manifest.reroutingEnabled,
this.pipeline.manifest.rewritingEnabled,
this.pipeline.logger
);
if (response.headers.get(ROUTE_TYPE_HEADER)) {
Expand All @@ -198,10 +198,10 @@ export class RenderContext {
const redirect = (path: string, status = 302) =>
new Response(null, { status, headers: { Location: path } });

const reroute = async (reroutePayload: ReroutePayload) => {
const rewrite = async (reroutePayload: RewritePayload) => {
pipeline.logger.debug('router', 'Called rerouting to:', reroutePayload);
try {
const [routeData, component] = await pipeline.tryReroute(reroutePayload);
const [routeData, component] = await pipeline.tryRewrite(reroutePayload);
this.routeData = routeData;
if (reroutePayload instanceof Request) {
this.request = reroutePayload;
Expand Down Expand Up @@ -257,7 +257,7 @@ export class RenderContext {
},
props,
redirect,
reroute,
rewrite,
request: this.request,
site: pipeline.site,
url,
Expand Down Expand Up @@ -385,10 +385,10 @@ export class RenderContext {
return new Response(null, { status, headers: { Location: path } });
};

const reroute = async (reroutePayload: ReroutePayload) => {
const rewrite = async (reroutePayload: RewritePayload) => {
try {
pipeline.logger.debug('router', 'Calling rerouting: ', reroutePayload);
const [routeData, component] = await pipeline.tryReroute(reroutePayload);
const [routeData, component] = await pipeline.tryRewrite(reroutePayload);
this.routeData = routeData;
if (reroutePayload instanceof Request) {
this.request = reroutePayload;
Expand Down Expand Up @@ -431,7 +431,7 @@ export class RenderContext {
},
locals,
redirect,
reroute,
rewrite,
request: this.request,
response,
site: pipeline.site,
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 @@ -4,7 +4,7 @@ import type {
ComponentInstance,
DevToolbarMetadata,
ManifestData,
ReroutePayload,
RewritePayload,
RouteData,
SSRElement,
SSRLoadedRenderer,
Expand Down Expand Up @@ -190,7 +190,7 @@ export class DevPipeline extends Pipeline {
}
}

async tryReroute(payload: ReroutePayload): Promise<[RouteData, ComponentInstance]> {
async tryRewrite(payload: RewritePayload): Promise<[RouteData, ComponentInstance]> {
let foundRoute;
if (!this.manifestData) {
throw new Error('Missing manifest data');
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/vite-plugin-astro-server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export function createDevelopmentManifest(settings: AstroSettings): SSRManifest
inlinedScripts: new Map(),
i18n: i18nManifest,
checkOrigin: settings.config.experimental.security?.csrfProtection?.origin ?? false,
reroutingEnabled: settings.config.experimental.rerouting,
rewritingEnabled: settings.config.experimental.rewriting,
middleware(_, next) {
return next();
},
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/test/fixtures/reroute/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { defineConfig } from 'astro/config';
// https://astro.build/config
export default defineConfig({
experimental: {
rerouting: true
rewriting: true
},
site: "https://example.com"
});
2 changes: 1 addition & 1 deletion packages/astro/test/fixtures/reroute/src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const second = async (context, next) => {
if (context.url.pathname.includes('/auth')) {
if (context.url.pathname.includes('/auth/dashboard')) {
contextReroute = true;
return await context.reroute('/');
return await context.rewrite('/');
}
if (context.url.pathname.includes('/auth/base')) {
return await next('/');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
return Astro.reroute(new URL("../../", Astro.url))
return Astro.rewrite(new URL("../../", Astro.url))
---
<html>
<head>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
return Astro.reroute(new Request(new URL("../../", Astro.url)))
return Astro.rewrite(new Request(new URL("../../", Astro.url)))
---
<html>
<head>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function getStaticPaths() {
}
return Astro.reroute("/")
return Astro.rewrite("/")
---

Expand Down
Loading

0 comments on commit 128bbcb

Please sign in to comment.