Expose a way to get routes

This commit is contained in:
Matthew Phillips 2023-02-13 08:21:09 -05:00
parent 4ce0b918b1
commit 26458cf4f4
4 changed files with 24 additions and 2 deletions

View file

@ -44,6 +44,7 @@
},
"./app": "./dist/core/app/index.js",
"./app/node": "./dist/core/app/node.js",
"./app/dev": "./dist/core/app/dev.js",
"./client/*": "./dist/runtime/client/*",
"./components": "./components/index.ts",
"./components/*": "./components/*",

View file

@ -37,8 +37,18 @@ export class DevApp extends App {
this.#root = root;
this.#createContainerParams = params;
}
get loaded() {
return !!this.#container;
}
async load() {
if(this.loaded) {
await this.close();
this.#container = null;
this.#env = null;
}
const container = this.#container = await createContainer(this.#createContainerParams);
this.#manifest.trailingSlash = container.settings.config.trailingSlash;
@ -66,7 +76,7 @@ export class DevApp extends App {
}
async render(request: Request, route?: RouteData | undefined): Promise<Response> {
if(!this.#env) {
if(!this.loaded) {
await this.load();
}
if(!route) {
@ -74,13 +84,19 @@ export class DevApp extends App {
}
if(route) {
const filePath = new URL(route.component, this.#root);
debugger;
// Always run preload so that if there has been a change in the file, the new
// version will run.
const [renderers, mod] = await preload({
env: this.#env!,
filePath
});
// Always reset the renderers as they might have changed.
this.#manifest.renderers.length = 0;
this.#manifest.renderers.push(...renderers);
// Save this module in the pageMap, so that super.render() finds it.
this.#manifest.pageMap.set(route.component, mod);
}
return super.render(request, route);

View file

@ -87,6 +87,9 @@ export class App {
};
this.#routeDataToRouteInfo = new Map(routes.map((route) => [route.routeData, route]));
}
get routes() {
return this.#manifestData.routes;
}
removeBase(pathname: string) {
if (pathname.startsWith(this.#base)) {
return pathname.slice(this.#baseWithoutTrailingSlash.length + 1);

View file

@ -1,5 +1,6 @@
import type { MarkdownRenderingOptions } from '@astrojs/markdown-remark';
import type {
AstroConfig,
ComponentInstance,
PropagationHint,
RouteData,
@ -37,6 +38,7 @@ export interface SSRManifest {
entryModules: Record<string, string>;
assets: Set<string>;
propagation: SSRResult['propagation'];
trailingSlash: AstroConfig['trailingSlash'];
}
export type SerializedSSRManifest = Omit<SSRManifest, 'routes' | 'assets' | 'propagation'> & {