[ci] format
This commit is contained in:
parent
df453e420f
commit
d361a3ec35
16 changed files with 104 additions and 151 deletions
|
@ -14,7 +14,12 @@ import { call as callEndpoint } from '../endpoint/index.js';
|
|||
import { consoleLogDestination } from '../logger/console.js';
|
||||
import { error } from '../logger/core.js';
|
||||
import { joinPaths, prependForwardSlash } from '../path.js';
|
||||
import { createEnvironment, Environment, createRenderContext, renderPage } from '../render/index.js';
|
||||
import {
|
||||
createEnvironment,
|
||||
createRenderContext,
|
||||
Environment,
|
||||
renderPage,
|
||||
} from '../render/index.js';
|
||||
import { RouteCache } from '../render/route-cache.js';
|
||||
import {
|
||||
createLinkStylesheetElementSet,
|
||||
|
|
|
@ -4,14 +4,17 @@ import { createRenderContext } from '../../render/index.js';
|
|||
import { call as callEndpoint } from '../index.js';
|
||||
|
||||
export async function call(options: SSROptions) {
|
||||
const { env, preload: [,mod] } = options;
|
||||
const {
|
||||
env,
|
||||
preload: [, mod],
|
||||
} = options;
|
||||
const endpointHandler = mod as unknown as EndpointHandler;
|
||||
|
||||
const ctx = createRenderContext({
|
||||
request: options.request,
|
||||
origin: options.origin,
|
||||
pathname: options.pathname,
|
||||
route: options.route
|
||||
route: options.route,
|
||||
});
|
||||
|
||||
return await callEndpoint(endpointHandler, env, ctx);
|
||||
|
|
|
@ -77,7 +77,7 @@ export async function call(
|
|||
routeCache: env.routeCache,
|
||||
pathname: ctx.pathname,
|
||||
logging: env.logging,
|
||||
ssr: env.ssr
|
||||
ssr: env.ssr,
|
||||
});
|
||||
|
||||
if (paramsAndPropsResp === GetParamsAndPropsError.NoMatchingStaticPath) {
|
||||
|
|
|
@ -1,15 +1,4 @@
|
|||
import type { MarkdownRenderingOptions } from '@astrojs/markdown-remark';
|
||||
import type {
|
||||
ComponentInstance,
|
||||
Params,
|
||||
Props,
|
||||
RouteData,
|
||||
RuntimeMode,
|
||||
SSRElement,
|
||||
SSRLoadedRenderer,
|
||||
} from '../../@types/astro';
|
||||
import type { LogOptions } from '../logger/core.js';
|
||||
import type { Environment } from './environment.js';
|
||||
import type { RouteData, SSRElement } from '../../@types/astro';
|
||||
|
||||
/**
|
||||
* The RenderContext represents the parts of rendering that are specific to one request.
|
||||
|
@ -29,7 +18,7 @@ export interface RenderContext {
|
|||
export type CreateRenderContextArgs = Partial<RenderContext> & {
|
||||
origin?: string;
|
||||
request: RenderContext['request'];
|
||||
}
|
||||
};
|
||||
|
||||
export function createRenderContext(options: CreateRenderContextArgs): RenderContext {
|
||||
const request = options.request;
|
||||
|
@ -40,6 +29,6 @@ export function createRenderContext(options: CreateRenderContextArgs): RenderCon
|
|||
...options,
|
||||
origin,
|
||||
pathname,
|
||||
url
|
||||
url,
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,12 +1,7 @@
|
|||
import type {
|
||||
ComponentInstance,
|
||||
Params,
|
||||
Props,
|
||||
RouteData,
|
||||
} from '../../@types/astro';
|
||||
import type { ComponentInstance, Params, Props, RouteData } from '../../@types/astro';
|
||||
import type { LogOptions } from '../logger/core.js';
|
||||
import type { Environment } from './environment.js';
|
||||
import type { RenderContext } from './context.js';
|
||||
import type { Environment } from './environment.js';
|
||||
|
||||
import { Fragment, renderPage as runtimeRenderPage } from '../../runtime/server/index.js';
|
||||
import { attachToResponse } from '../cookies/index.js';
|
||||
|
|
|
@ -1,20 +1,15 @@
|
|||
import type { MarkdownRenderingOptions } from '@astrojs/markdown-remark';
|
||||
import type { ViteDevServer } from 'vite';
|
||||
import type {
|
||||
AstroSettings,
|
||||
RuntimeMode,
|
||||
SSRLoadedRenderer,
|
||||
} from '../../../@types/astro';
|
||||
import type { Environment } from '../index';
|
||||
import type { AstroSettings, RuntimeMode } from '../../../@types/astro';
|
||||
import type { LogOptions } from '../../logger/core.js';
|
||||
import { RouteCache } from '../route-cache.js';
|
||||
import type { Environment } from '../index';
|
||||
import { createEnvironment } from '../index.js';
|
||||
import { RouteCache } from '../route-cache.js';
|
||||
import { createResolve } from './resolve.js';
|
||||
|
||||
export type DevelopmentEnvironment = Environment & {
|
||||
settings: AstroSettings;
|
||||
viteServer: ViteDevServer;
|
||||
}
|
||||
};
|
||||
|
||||
export function createDevelopmentEnvironment(
|
||||
settings: AstroSettings,
|
||||
|
@ -25,7 +20,7 @@ export function createDevelopmentEnvironment(
|
|||
let env = createEnvironment({
|
||||
adapterName: settings.adapter?.name,
|
||||
logging,
|
||||
markdown: {
|
||||
markdown: {
|
||||
...settings.config.markdown,
|
||||
isAstroFlavoredMd: settings.config.legacy.astroFlavoredMarkdown,
|
||||
},
|
||||
|
@ -42,6 +37,6 @@ export function createDevelopmentEnvironment(
|
|||
return {
|
||||
...env,
|
||||
viteServer,
|
||||
settings
|
||||
settings,
|
||||
};
|
||||
}
|
||||
|
|
|
@ -8,18 +8,18 @@ import type {
|
|||
SSRElement,
|
||||
SSRLoadedRenderer,
|
||||
} from '../../../@types/astro';
|
||||
import type { DevelopmentEnvironment } from './environment';
|
||||
import { PAGE_SCRIPT_ID } from '../../../vite-plugin-scripts/index.js';
|
||||
import { LogOptions } from '../../logger/core.js';
|
||||
import { isPage, resolveIdToUrl } from '../../util.js';
|
||||
import { renderPage as coreRenderPage, createRenderContext } from '../index.js';
|
||||
import { createRenderContext, renderPage as coreRenderPage } from '../index.js';
|
||||
import { filterFoundRenderers, loadRenderer } from '../renderer.js';
|
||||
import { RouteCache } from '../route-cache.js';
|
||||
import { collectMdMetadata } from '../util.js';
|
||||
import { getStylesForURL } from './css.js';
|
||||
import type { DevelopmentEnvironment } from './environment';
|
||||
import { getScriptsForURL } from './scripts.js';
|
||||
import { loadRenderer, filterFoundRenderers } from '../renderer.js';
|
||||
export { createDevelopmentEnvironment } from './environment.js';
|
||||
export type { DevelopmentEnvironment };
|
||||
export type { DevelopmentEnvironment };
|
||||
|
||||
export interface SSROptionsOld {
|
||||
/** an instance of the AstroSettings */
|
||||
|
@ -73,7 +73,6 @@ export interface SSROptions {
|
|||
request: Request;
|
||||
/** optional, in case we need to render something outside of a dev server */
|
||||
route?: RouteData;
|
||||
|
||||
}
|
||||
|
||||
export type ComponentPreload = [SSRLoadedRenderer[], ComponentInstance];
|
||||
|
@ -83,7 +82,7 @@ export async function loadRenderers(
|
|||
settings: AstroSettings
|
||||
): Promise<SSRLoadedRenderer[]> {
|
||||
const loader = (entry: string) => viteServer.ssrLoadModule(entry);
|
||||
const renderers = await Promise.all(settings.renderers.map(r => loadRenderer(r, loader)));
|
||||
const renderers = await Promise.all(settings.renderers.map((r) => loadRenderer(r, loader)));
|
||||
return filterFoundRenderers(renderers);
|
||||
}
|
||||
|
||||
|
@ -176,7 +175,7 @@ async function getScriptsAndStyles({ env, filePath }: GetScriptsAndStylesParams)
|
|||
children: content,
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
return { scripts, styles, links };
|
||||
}
|
||||
|
||||
|
@ -189,7 +188,7 @@ export async function renderPage(options: SSROptions): Promise<Response> {
|
|||
|
||||
const { scripts, links, styles } = await getScriptsAndStyles({
|
||||
env: options.env,
|
||||
filePath: options.filePath
|
||||
filePath: options.filePath,
|
||||
});
|
||||
|
||||
const ctx = createRenderContext({
|
||||
|
@ -199,7 +198,7 @@ export async function renderPage(options: SSROptions): Promise<Response> {
|
|||
scripts,
|
||||
links,
|
||||
styles,
|
||||
route: options.route
|
||||
route: options.route,
|
||||
});
|
||||
|
||||
return await coreRenderPage(mod, ctx, options.env); // NOTE: without "await", errors won’t get caught below
|
||||
|
|
|
@ -1,20 +1,20 @@
|
|||
import type { ViteDevServer } from 'vite';
|
||||
import { isPage, resolveIdToUrl } from '../../util.js';
|
||||
import { resolveIdToUrl } from '../../util.js';
|
||||
|
||||
export function createResolve(viteServer: ViteDevServer) {
|
||||
// Resolves specifiers in the inline hydrated scripts, such as:
|
||||
// - @astrojs/preact/client.js
|
||||
// - @/components/Foo.vue
|
||||
// - /Users/macos/project/src/Foo.vue
|
||||
// - C:/Windows/project/src/Foo.vue (normalized slash)
|
||||
return async function(s: string) {
|
||||
const url = await resolveIdToUrl(viteServer, s);
|
||||
// Vite does not resolve .jsx -> .tsx when coming from hydration script import,
|
||||
// clip it so Vite is able to resolve implicitly.
|
||||
if (url.startsWith('/@fs') && url.endsWith('.jsx')) {
|
||||
return url.slice(0, -4);
|
||||
} else {
|
||||
return url;
|
||||
}
|
||||
};
|
||||
// Resolves specifiers in the inline hydrated scripts, such as:
|
||||
// - @astrojs/preact/client.js
|
||||
// - @/components/Foo.vue
|
||||
// - /Users/macos/project/src/Foo.vue
|
||||
// - C:/Windows/project/src/Foo.vue (normalized slash)
|
||||
return async function (s: string) {
|
||||
const url = await resolveIdToUrl(viteServer, s);
|
||||
// Vite does not resolve .jsx -> .tsx when coming from hydration script import,
|
||||
// clip it so Vite is able to resolve implicitly.
|
||||
if (url.startsWith('/@fs') && url.endsWith('.jsx')) {
|
||||
return url.slice(0, -4);
|
||||
} else {
|
||||
return url;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,8 +1,5 @@
|
|||
import type { MarkdownRenderingOptions } from '@astrojs/markdown-remark';
|
||||
import type {
|
||||
RuntimeMode,
|
||||
SSRLoadedRenderer,
|
||||
} from '../../@types/astro';
|
||||
import type { RuntimeMode, SSRLoadedRenderer } from '../../@types/astro';
|
||||
import type { LogOptions } from '../logger/core.js';
|
||||
import { RouteCache } from './route-cache.js';
|
||||
|
||||
|
@ -34,7 +31,7 @@ export function createEnvironment(options: CreateEnvironmentArgs): Environment {
|
|||
|
||||
export type CreateBasicEnvironmentArgs = Partial<Environment> & {
|
||||
logging: CreateEnvironmentArgs['logging'];
|
||||
}
|
||||
};
|
||||
|
||||
export function createBasicEnvironment(options: CreateBasicEnvironmentArgs): Environment {
|
||||
const mode = options.mode ?? 'development';
|
||||
|
@ -46,7 +43,6 @@ export function createBasicEnvironment(options: CreateBasicEnvironmentArgs): Env
|
|||
resolve: options.resolve ?? ((s: string) => Promise.resolve(s)),
|
||||
routeCache: new RouteCache(options.logging, mode),
|
||||
ssr: options.ssr ?? true,
|
||||
streaming: options.streaming ?? true
|
||||
streaming: options.streaming ?? true,
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -1,22 +1,6 @@
|
|||
export type {
|
||||
Environment
|
||||
} from './environment';
|
||||
export type {
|
||||
RenderContext
|
||||
} from './context';
|
||||
|
||||
export {
|
||||
createBasicEnvironment,
|
||||
createEnvironment
|
||||
} from './environment.js';
|
||||
export {
|
||||
createRenderContext
|
||||
} from './context.js';
|
||||
export {
|
||||
getParamsAndProps,
|
||||
GetParamsAndPropsError,
|
||||
renderPage,
|
||||
} from './core.js';
|
||||
export {
|
||||
loadRenderer
|
||||
} from './renderer.js';
|
||||
export type { RenderContext } from './context';
|
||||
export { createRenderContext } from './context.js';
|
||||
export { getParamsAndProps, GetParamsAndPropsError, renderPage } from './core.js';
|
||||
export type { Environment } from './environment';
|
||||
export { createBasicEnvironment, createEnvironment } from './environment.js';
|
||||
export { loadRenderer } from './renderer.js';
|
||||
|
|
|
@ -6,23 +6,31 @@ export type RendererServerEntrypointModule = {
|
|||
export type MaybeRendererServerEntrypointModule = Partial<RendererServerEntrypointModule>;
|
||||
export type RendererLoader = (entryPoint: string) => Promise<MaybeRendererServerEntrypointModule>;
|
||||
|
||||
export async function loadRenderer(renderer: AstroRenderer, loader: RendererLoader): Promise<SSRLoadedRenderer | undefined> {
|
||||
export async function loadRenderer(
|
||||
renderer: AstroRenderer,
|
||||
loader: RendererLoader
|
||||
): Promise<SSRLoadedRenderer | undefined> {
|
||||
const mod = await loader(renderer.serverEntrypoint);
|
||||
if(typeof mod.default !== 'undefined') {
|
||||
if (typeof mod.default !== 'undefined') {
|
||||
return createLoadedRenderer(renderer, mod as RendererServerEntrypointModule);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function filterFoundRenderers(renderers: Array<SSRLoadedRenderer | undefined>): SSRLoadedRenderer[] {
|
||||
export function filterFoundRenderers(
|
||||
renderers: Array<SSRLoadedRenderer | undefined>
|
||||
): SSRLoadedRenderer[] {
|
||||
return renderers.filter((renderer): renderer is SSRLoadedRenderer => {
|
||||
return !!renderer;
|
||||
});
|
||||
}
|
||||
|
||||
export function createLoadedRenderer(renderer: AstroRenderer, mod: RendererServerEntrypointModule): SSRLoadedRenderer {
|
||||
export function createLoadedRenderer(
|
||||
renderer: AstroRenderer,
|
||||
mod: RendererServerEntrypointModule
|
||||
): SSRLoadedRenderer {
|
||||
return {
|
||||
...renderer,
|
||||
ssr: mod.default
|
||||
ssr: mod.default,
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import renderer from './renderer.js';
|
||||
import { __astro_tag_component__ } from '../runtime/server/index.js';
|
||||
import renderer from './renderer.js';
|
||||
|
||||
const ASTRO_JSX_RENDERER_NAME = renderer.name;
|
||||
|
||||
|
|
|
@ -1,6 +1,2 @@
|
|||
export {
|
||||
default as renderer
|
||||
} from './renderer.js';
|
||||
export {
|
||||
createAstroJSXComponent
|
||||
} from './component.js';
|
||||
export { createAstroJSXComponent } from './component.js';
|
||||
export { default as renderer } from './renderer.js';
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
export { createAstro } from './astro-global.js';
|
||||
export { renderEndpoint } from './endpoint.js';
|
||||
export { escapeHTML, HTMLBytes, HTMLString, markHTMLString, unescapeHTML } from './escape.js';
|
||||
export { renderJSX } from './jsx.js';
|
||||
export type { Metadata } from './metadata';
|
||||
export { createMetadata } from './metadata.js';
|
||||
export {
|
||||
|
@ -21,7 +22,6 @@ export {
|
|||
stringifyChunk,
|
||||
voidElementNames,
|
||||
} from './render/index.js';
|
||||
export { renderJSX } from './jsx.js';
|
||||
export type { AstroComponentFactory, RenderInstruction } from './render/index.js';
|
||||
import type { AstroComponentFactory } from './render/index.js';
|
||||
|
||||
|
|
|
@ -16,8 +16,8 @@ import {
|
|||
import { error, info, LogOptions, warn } from '../core/logger/core.js';
|
||||
import * as msg from '../core/messages.js';
|
||||
import { appendForwardSlash } from '../core/path.js';
|
||||
import { getParamsAndProps, GetParamsAndPropsError } from '../core/render/index.js';
|
||||
import { createDevelopmentEnvironment, preload, renderPage } from '../core/render/dev/index.js';
|
||||
import { getParamsAndProps, GetParamsAndPropsError } from '../core/render/index.js';
|
||||
import { createRequest } from '../core/request.js';
|
||||
import { createRouteManifest, matchAllRoutes } from '../core/routing/index.js';
|
||||
import { resolvePages } from '../core/util.js';
|
||||
|
@ -183,11 +183,7 @@ export function baseMiddleware(
|
|||
};
|
||||
}
|
||||
|
||||
async function matchRoute(
|
||||
pathname: string,
|
||||
env: DevelopmentEnvironment,
|
||||
manifest: ManifestData,
|
||||
) {
|
||||
async function matchRoute(pathname: string, env: DevelopmentEnvironment, manifest: ManifestData) {
|
||||
const { logging, settings, routeCache } = env;
|
||||
const matches = matchAllRoutes(pathname, manifest);
|
||||
|
||||
|
@ -288,24 +284,10 @@ async function handleRequest(
|
|||
|
||||
let filePath: URL | undefined;
|
||||
try {
|
||||
const matchedRoute = await matchRoute(
|
||||
pathname,
|
||||
env,
|
||||
manifest,
|
||||
);
|
||||
const matchedRoute = await matchRoute(pathname, env, manifest);
|
||||
filePath = matchedRoute?.filePath;
|
||||
|
||||
return await handleRoute(
|
||||
matchedRoute,
|
||||
url,
|
||||
pathname,
|
||||
body,
|
||||
origin,
|
||||
env,
|
||||
manifest,
|
||||
req,
|
||||
res
|
||||
);
|
||||
return await handleRoute(matchedRoute, url, pathname, body, origin, env, manifest, req, res);
|
||||
} catch (_err) {
|
||||
const err = fixViteErrorMessage(_err, viteServer, filePath);
|
||||
const errorWithMetadata = collectErrorMetadata(err);
|
||||
|
@ -358,13 +340,13 @@ async function handleRoute(
|
|||
});
|
||||
|
||||
const options: SSROptions = {
|
||||
env,
|
||||
filePath,
|
||||
origin,
|
||||
preload: preloadedComponent,
|
||||
pathname,
|
||||
request,
|
||||
route
|
||||
env,
|
||||
filePath,
|
||||
origin,
|
||||
preload: preloadedComponent,
|
||||
pathname,
|
||||
request,
|
||||
route,
|
||||
};
|
||||
|
||||
// Route successfully matched! Render it.
|
||||
|
@ -372,11 +354,7 @@ async function handleRoute(
|
|||
const result = await callEndpoint(options);
|
||||
if (result.type === 'response') {
|
||||
if (result.response.headers.get('X-Astro-Response') === 'Not-Found') {
|
||||
const fourOhFourRoute = await matchRoute(
|
||||
'/404',
|
||||
env,
|
||||
manifest
|
||||
);
|
||||
const fourOhFourRoute = await matchRoute('/404', env, manifest);
|
||||
return handleRoute(
|
||||
fourOhFourRoute,
|
||||
new URL('/404', url),
|
||||
|
|
|
@ -2,45 +2,50 @@ import { expect } from 'chai';
|
|||
|
||||
import { createComponent, render, renderSlot } from '../../../dist/runtime/server/index.js';
|
||||
import { jsx } from '../../../dist/jsx-runtime/index.js';
|
||||
import { createBasicEnvironment, createRenderContext, renderPage, loadRenderer } from '../../../dist/core/render/index.js';
|
||||
import {
|
||||
createBasicEnvironment,
|
||||
createRenderContext,
|
||||
renderPage,
|
||||
loadRenderer,
|
||||
} from '../../../dist/core/render/index.js';
|
||||
import { createAstroJSXComponent, renderer as jsxRenderer } from '../../../dist/jsx/index.js';
|
||||
import { defaultLogging as logging } from '../../test-utils.js';
|
||||
|
||||
const createAstroModule = AstroComponent => ({ default: AstroComponent });
|
||||
const loadJSXRenderer = () => loadRenderer(jsxRenderer, s => import(s));
|
||||
const createAstroModule = (AstroComponent) => ({ default: AstroComponent });
|
||||
const loadJSXRenderer = () => loadRenderer(jsxRenderer, (s) => import(s));
|
||||
|
||||
describe('core/render', () => {
|
||||
describe('Astro JSX components', () => {
|
||||
let env;
|
||||
before(async () => {
|
||||
before(async () => {
|
||||
env = createBasicEnvironment({
|
||||
logging,
|
||||
renderers: [await loadJSXRenderer()]
|
||||
renderers: [await loadJSXRenderer()],
|
||||
});
|
||||
})
|
||||
});
|
||||
|
||||
it('Can render slots', async () => {
|
||||
const Wrapper = createComponent((result, _props, slots = {}) => {
|
||||
return render`<div>${renderSlot(result, slots['myslot'])}</div>`;
|
||||
});
|
||||
|
||||
|
||||
const Page = createAstroJSXComponent(() => {
|
||||
return jsx(Wrapper, {
|
||||
children: [
|
||||
jsx('p', {
|
||||
slot: 'myslot',
|
||||
className: 'n',
|
||||
children: 'works'
|
||||
})
|
||||
]
|
||||
})
|
||||
children: 'works',
|
||||
}),
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
const ctx = createRenderContext({ request: new Request('http://example.com/' )});
|
||||
const ctx = createRenderContext({ request: new Request('http://example.com/') });
|
||||
const response = await renderPage(createAstroModule(Page), ctx, env);
|
||||
|
||||
|
||||
expect(response.status).to.equal(200);
|
||||
|
||||
|
||||
const html = await response.text();
|
||||
expect(html).to.include('<div><p class="n">works</p></div>');
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue