Skip to content

Commit

Permalink
feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed May 7, 2024
1 parent d3f22ca commit a63885f
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 30 deletions.
6 changes: 2 additions & 4 deletions .changeset/pink-ligers-share.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ export default defineConfig({
})
```

When enabled, you can use `rewrite()` to **render
** another page without changing the URL of the browser in Astro pages and endpoints.
When enabled, you can use `rewrite()` to **render** another page without changing the URL of the browser in Astro pages and endpoints.

```astro
---
Expand Down Expand Up @@ -47,5 +46,4 @@ export function onRequest(ctx, next) {
}
```

> **NOTE
**: please [read the RFC](https://github.com/withastro/roadmap/blob/feat/reroute/proposals/0047-rerouting.md) to understand the current expectations of the new APIs.
> **NOTE**: please [read the RFC](https://github.com/withastro/roadmap/blob/feat/reroute/proposals/0047-rerouting.md) to understand the current expectations of the new APIs.
8 changes: 4 additions & 4 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1944,7 +1944,7 @@ export interface AstroUserConfig {
* @version 4.8.0
* @description
*
* Enables a routing feature for rewriting requests in Astro pages, endpoints and Astro middleware, giving you programmatic control over your routes.
* Enables a routing feature for rewriting requests in Astro pages, endpoints and Astro middleware, giving you programmatic control over your routes.
*
* ```js
* {
Expand All @@ -1966,7 +1966,7 @@ export interface AstroUserConfig {
* ```
*
* Use `context.rewrite` in your endpoint files to reroute to a different page:
*
*
* ```js
* // src/pages/api.js
* export function GET(ctx) {
Expand All @@ -1977,7 +1977,7 @@ export interface AstroUserConfig {
* ```
*
* Use `next("/")` in your middleware file to reroute to a different page, and then call the next middleware function:
*
*
* ```js
* // src/middleware.js
* export function onRequest(ctx, next) {
Expand All @@ -1987,7 +1987,7 @@ export interface AstroUserConfig {
* return next();
* }
* ```
*
*
* For a complete overview, and to give feedback on this experimental API, see the [Rerouting RFC](https://github.com/withastro/roadmap/blob/feat/reroute/proposals/0047-rerouting.md).
*/
rewriting: boolean;
Expand Down
14 changes: 6 additions & 8 deletions packages/astro/src/core/app/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,9 @@ export class AppPipeline extends Pipeline {
foundRoute = route;
break;
}
} else {
if (route.pattern.test(decodeURI(payload))) {
foundRoute = route;
break;
}
} else if (route.pattern.test(decodeURI(payload))) {
foundRoute = route;
break;
}
}

Expand Down Expand Up @@ -122,9 +120,9 @@ export class AppPipeline extends Pipeline {
return await importComponentInstance();
} else if (this.manifest.pageModule) {
return this.manifest.pageModule;
throw new Error(
"Astro couldn't find the correct page to render, probably because it wasn't correctly mapped for SSR usage. This is an internal error, please file an issue."
);
throw new Error(
"Astro couldn't find the correct page to render, probably because it wasn't correctly mapped for SSR usage. This is an internal error, please file an issue."
);
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions packages/astro/src/core/build/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import type { PageBuildData, SinglePageBuiltModule, StaticBuildOptions } from '.
import { i18nHasFallback } from './util.js';
import { RedirectSinglePageBuiltModule } from '../redirects/index.js';
import { getOutDirWithinCwd } from './common.js';
import { RouteNotFound } from '../errors/errors-data.js';
import { AstroError } from '../errors/index.js';

/**
* The build pipeline is responsible to gather the files emitted by the SSR build and generate the pages by executing these files.
Expand Down Expand Up @@ -288,18 +290,16 @@ export class BuildPipeline extends Pipeline {
foundRoute = route;
break;
}
} else {
if (route.pattern.test(decodeURI(payload))) {
foundRoute = route;
break;
}
} else if (route.pattern.test(decodeURI(payload))) {
foundRoute = route;
break;
}
}
if (foundRoute) {
const componentInstance = await this.getComponentByRoute(foundRoute);
return [foundRoute, componentInstance];
} else {
throw new Error('Route not found');
throw new AstroError(RouteNotFound);
}
}

Expand Down
12 changes: 12 additions & 0 deletions packages/astro/src/core/errors/errors-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1483,6 +1483,18 @@ export const UnsupportedConfigTransformError = {
hint: 'See the devalue library for all supported types: https://github.com/rich-harris/devalue',
} satisfies ErrorData;

/**
* @docs
* @description
*
* Astro couldn't find a route matching the one provided by the user
*/
export const RouteNotFound = {
name: 'RouteNotFound',
title: 'Route not found.',
message: `Astro could find a route that matches the one you requested.`,
} satisfies ErrorData;

// Generic catch-all - Only use this in extreme cases, like if there was a cosmic ray bit flip.
export const UnknownError = { name: 'UnknownError', title: 'Unknown Error.' } satisfies ErrorData;

Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/core/render-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ export class RenderContext {
return new Response('Loop Detected', {
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/508
status: 508,
statusText: 'Astro detected a loop where you tried to call the rewriting logic more than four times.',
statusText:
'Astro detected a loop where you tried to call the rewriting logic more than four times.',
});
}
const lastNext = async (ctx: APIContext, payload?: RewritePayload) => {
Expand Down
13 changes: 6 additions & 7 deletions packages/astro/src/vite-plugin-astro-server/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { getInfoOutput } from '../cli/info/index.js';
import type { HeadElements } from '../core/base-pipeline.js';
import { ASTRO_VERSION, DEFAULT_404_COMPONENT } from '../core/constants.js';
import { enhanceViteSSRError } from '../core/errors/dev/index.js';
import { AggregateError, CSSError, MarkdownError } from '../core/errors/index.js';
import { AggregateError, AstroError, CSSError, MarkdownError } from '../core/errors/index.js';
import type { Logger } from '../core/logger/core.js';
import type { ModuleLoader } from '../core/module-loader/index.js';
import { loadRenderer, Pipeline } from '../core/render/index.js';
Expand All @@ -25,6 +25,7 @@ import { getComponentMetadata } from './metadata.js';
import { createResolve } from './resolve.js';
import { default404Page } from './response.js';
import { getScriptsForURL } from './scripts.js';
import { RouteNotFound } from '../core/errors/errors-data.js';

export class DevPipeline extends Pipeline {
// renderers are loaded on every request,
Expand Down Expand Up @@ -208,19 +209,17 @@ export class DevPipeline extends Pipeline {
foundRoute = route;
break;
}
} else {
if (route.pattern.test(decodeURI(payload))) {
foundRoute = route;
break;
}
} else if (route.pattern.test(decodeURI(payload))) {
foundRoute = route;
break;
}
}

if (foundRoute) {
const componentInstance = await this.getComponentByRoute(foundRoute);
return [foundRoute, componentInstance];
} else {
throw new Error('Route not found');
throw new AstroError(RouteNotFound);
}
}

Expand Down

0 comments on commit a63885f

Please sign in to comment.