|
1 | 1 | import type { RoutesList } from '../../types/astro.js';
|
2 | 2 | import type { RouteData } from '../../types/public/internal.js';
|
| 3 | +import { SERVER_ISLAND_BASE_PREFIX, SERVER_ISLAND_COMPONENT } from '../server-islands/endpoint.js'; |
3 | 4 |
|
4 | 5 | /** Find matching route from pathname */
|
5 | 6 | export function matchRoute(pathname: string, manifest: RoutesList): RouteData | undefined {
|
@@ -37,3 +38,41 @@ export function isRoute500(route: string) {
|
37 | 38 | export function isRoute404or500(route: RouteData): boolean {
|
38 | 39 | return isRoute404(route.route) || isRoute500(route.route);
|
39 | 40 | }
|
| 41 | + |
| 42 | +/** |
| 43 | + * Determines if a given route is associated with the server island component. |
| 44 | + * |
| 45 | + * @param {RouteData} route - The route data object to evaluate. |
| 46 | + * @return {boolean} Returns true if the route's component is the server island component, otherwise false. |
| 47 | + */ |
| 48 | +export function isRouteServerIsland(route: RouteData): boolean { |
| 49 | + return route.component === SERVER_ISLAND_COMPONENT; |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Determines whether the given `Request` is targeted to a "server island" based on its URL. |
| 54 | + * |
| 55 | + * @param {Request} request - The request object to be evaluated. |
| 56 | + * @param {string} [base=''] - The base path provided via configuration. |
| 57 | + * @return {boolean} - Returns `true` if the request is for a server island, otherwise `false`. |
| 58 | + */ |
| 59 | +export function isRequestServerIsland(request: Request, base = ''): boolean { |
| 60 | + const url = new URL(request.url); |
| 61 | + const pathname = url.pathname.slice(base.length); |
| 62 | + |
| 63 | + return pathname.startsWith(SERVER_ISLAND_BASE_PREFIX); |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Checks if the given request corresponds to a 404 or 500 route based on the specified base path. |
| 68 | + * |
| 69 | + * @param {Request} request - The HTTP request object to be checked. |
| 70 | + * @param {string} [base=''] - The base path to trim from the request's URL before checking the route. Default is an empty string. |
| 71 | + * @return {boolean} Returns true if the request matches a 404 or 500 route; otherwise, returns false. |
| 72 | + */ |
| 73 | +export function requestIs404Or500(request: Request, base = '') { |
| 74 | + const url = new URL(request.url); |
| 75 | + const pathname = url.pathname.slice(base.length); |
| 76 | + |
| 77 | + return isRoute404(pathname) || isRoute500(pathname); |
| 78 | +} |
0 commit comments