Get scripts and styles in devapp
This commit is contained in:
parent
26458cf4f4
commit
c0febd56f9
4 changed files with 34 additions and 33 deletions
|
@ -1,10 +1,20 @@
|
||||||
import type { ManifestData, RouteData } from '../../@types/astro';
|
import type { ComponentInstance, ManifestData, RouteData } from '../../@types/astro';
|
||||||
import type { SSRManifest as Manifest } from './types';
|
import type { SSRManifest as Manifest } from './types';
|
||||||
import { createContainer, type CreateContainerParams } from '../dev/index.js';
|
import { createContainer, type CreateContainerParams } from '../dev/index.js';
|
||||||
import { createViteLoader } from '../module-loader/index.js';
|
import { createViteLoader } from '../module-loader/index.js';
|
||||||
import { createRouteManifest } from '../routing/index.js';
|
import { createRouteManifest } from '../routing/index.js';
|
||||||
import { createDevelopmentEnvironment, preload, type DevelopmentEnvironment } from '../render/dev/index.js';
|
import {
|
||||||
|
createDevelopmentEnvironment,
|
||||||
|
getScriptsAndStyles,
|
||||||
|
preload,
|
||||||
|
type DevelopmentEnvironment
|
||||||
|
} from '../render/dev/index.js';
|
||||||
|
import {
|
||||||
|
createRenderContext,
|
||||||
|
renderPage as coreRenderPage
|
||||||
|
} from '../render/index.js';
|
||||||
import { App, MatchOptions } from './index.js';
|
import { App, MatchOptions } from './index.js';
|
||||||
|
import { RenderContext, Environment } from '../render';
|
||||||
|
|
||||||
export type DevAppParams = Partial<CreateContainerParams> & {
|
export type DevAppParams = Partial<CreateContainerParams> & {
|
||||||
root: URL;
|
root: URL;
|
||||||
|
@ -16,6 +26,7 @@ export class DevApp extends App {
|
||||||
#container: Awaited<ReturnType<typeof createContainer>> | null = null;
|
#container: Awaited<ReturnType<typeof createContainer>> | null = null;
|
||||||
#env: DevelopmentEnvironment | null = null;
|
#env: DevelopmentEnvironment | null = null;
|
||||||
#root: URL;
|
#root: URL;
|
||||||
|
#modToRoute = new Map<ComponentInstance, RouteData>();
|
||||||
constructor(params: DevAppParams) {
|
constructor(params: DevAppParams) {
|
||||||
const { root, userConfig } = params;
|
const { root, userConfig } = params;
|
||||||
const manifest: Manifest = {
|
const manifest: Manifest = {
|
||||||
|
@ -98,7 +109,24 @@ export class DevApp extends App {
|
||||||
|
|
||||||
// Save this module in the pageMap, so that super.render() finds it.
|
// Save this module in the pageMap, so that super.render() finds it.
|
||||||
this.#manifest.pageMap.set(route.component, mod);
|
this.#manifest.pageMap.set(route.component, mod);
|
||||||
|
this.#modToRoute.set(mod, route);
|
||||||
}
|
}
|
||||||
return super.render(request, route);
|
return super.render(request, route);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
renderPage = async (mod: ComponentInstance, ctx: RenderContext, env: Environment) => {
|
||||||
|
const route = this.#modToRoute.get(mod)!;
|
||||||
|
|
||||||
|
const { scripts, links, styles, propagationMap } = await getScriptsAndStyles({
|
||||||
|
env: this.#env!,
|
||||||
|
filePath: new URL(route.component, this.#root),
|
||||||
|
});
|
||||||
|
|
||||||
|
ctx.scripts = scripts;
|
||||||
|
ctx.links = links;
|
||||||
|
ctx.styles = styles;
|
||||||
|
ctx.propagation = propagationMap;
|
||||||
|
|
||||||
|
return coreRenderPage(mod, ctx, env);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,6 +47,7 @@ export class App {
|
||||||
};
|
};
|
||||||
#base: string;
|
#base: string;
|
||||||
#baseWithoutTrailingSlash: string;
|
#baseWithoutTrailingSlash: string;
|
||||||
|
renderPage: typeof renderPage;
|
||||||
|
|
||||||
constructor(manifest: Manifest, streaming = true) {
|
constructor(manifest: Manifest, streaming = true) {
|
||||||
this.#manifest = manifest;
|
this.#manifest = manifest;
|
||||||
|
@ -80,6 +81,7 @@ export class App {
|
||||||
|
|
||||||
this.#base = this.#manifest.base || '/';
|
this.#base = this.#manifest.base || '/';
|
||||||
this.#baseWithoutTrailingSlash = removeTrailingForwardSlash(this.#base);
|
this.#baseWithoutTrailingSlash = removeTrailingForwardSlash(this.#base);
|
||||||
|
this.renderPage = renderPage;
|
||||||
}
|
}
|
||||||
updateRoutes(routes: RouteInfo[]) {
|
updateRoutes(routes: RouteInfo[]) {
|
||||||
this.#manifestData = {
|
this.#manifestData = {
|
||||||
|
@ -212,7 +214,7 @@ export class App {
|
||||||
status,
|
status,
|
||||||
});
|
});
|
||||||
|
|
||||||
const response = await renderPage(mod, ctx, this.#env);
|
const response = await this.renderPage(mod, ctx, this.#env);
|
||||||
return response;
|
return response;
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
error(this.#logging, 'ssr', err.stack || err.message || String(err));
|
error(this.#logging, 'ssr', err.stack || err.message || String(err));
|
||||||
|
|
|
@ -74,7 +74,7 @@ interface GetScriptsAndStylesParams {
|
||||||
filePath: URL;
|
filePath: URL;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getScriptsAndStyles({ env, filePath }: GetScriptsAndStylesParams) {
|
export async function getScriptsAndStyles({ env, filePath }: GetScriptsAndStylesParams) {
|
||||||
// Add hoisted script tags
|
// Add hoisted script tags
|
||||||
const scripts = await getScriptsForURL(filePath, env.loader);
|
const scripts = await getScriptsForURL(filePath, env.loader);
|
||||||
|
|
||||||
|
|
|
@ -52,40 +52,11 @@ describe('base configuration', () => {
|
||||||
try {
|
try {
|
||||||
const request = new Request(`http://localhost:8080/docs`);
|
const request = new Request(`http://localhost:8080/docs`);
|
||||||
|
|
||||||
debugger;
|
|
||||||
const response = await app.render(request);
|
const response = await app.render(request);
|
||||||
|
|
||||||
expect(response.status).to.equal(200);
|
expect(response.status).to.equal(200);
|
||||||
console.log(await response.text());
|
|
||||||
} finally {
|
} finally {
|
||||||
await app.close();
|
await app.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
|
|
||||||
await runInContainer(
|
|
||||||
{
|
|
||||||
fs,
|
|
||||||
root,
|
|
||||||
userConfig: {
|
|
||||||
base: '/docs',
|
|
||||||
trailingSlash: 'never',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
async (container) => {
|
|
||||||
const { req, res, done, text } = createRequestAndResponse({
|
|
||||||
method: 'GET',
|
|
||||||
url: '/docs',
|
|
||||||
});
|
|
||||||
container.handle(req, res);
|
|
||||||
await done;
|
|
||||||
expect(res.statusCode).to.equal(200);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
*/
|
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue