diff --git a/packages/astro/src/core/app/index.ts b/packages/astro/src/core/app/index.ts index d08266fb1..f443ffeac 100644 --- a/packages/astro/src/core/app/index.ts +++ b/packages/astro/src/core/app/index.ts @@ -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, diff --git a/packages/astro/src/core/endpoint/dev/index.ts b/packages/astro/src/core/endpoint/dev/index.ts index 59bc3e730..889b82345 100644 --- a/packages/astro/src/core/endpoint/dev/index.ts +++ b/packages/astro/src/core/endpoint/dev/index.ts @@ -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); diff --git a/packages/astro/src/core/endpoint/index.ts b/packages/astro/src/core/endpoint/index.ts index e73f98306..f5407d7c5 100644 --- a/packages/astro/src/core/endpoint/index.ts +++ b/packages/astro/src/core/endpoint/index.ts @@ -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) { diff --git a/packages/astro/src/core/render/context.ts b/packages/astro/src/core/render/context.ts index 02e9b9439..ab7563862 100644 --- a/packages/astro/src/core/render/context.ts +++ b/packages/astro/src/core/render/context.ts @@ -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 & { 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, }; } diff --git a/packages/astro/src/core/render/core.ts b/packages/astro/src/core/render/core.ts index ed2f39634..ee84e0492 100644 --- a/packages/astro/src/core/render/core.ts +++ b/packages/astro/src/core/render/core.ts @@ -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'; diff --git a/packages/astro/src/core/render/dev/environment.ts b/packages/astro/src/core/render/dev/environment.ts index 3b8daec75..5a8009eac 100644 --- a/packages/astro/src/core/render/dev/environment.ts +++ b/packages/astro/src/core/render/dev/environment.ts @@ -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, }; } diff --git a/packages/astro/src/core/render/dev/index.ts b/packages/astro/src/core/render/dev/index.ts index fb0482415..fc14bc3a4 100644 --- a/packages/astro/src/core/render/dev/index.ts +++ b/packages/astro/src/core/render/dev/index.ts @@ -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 { 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 { 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 { 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 diff --git a/packages/astro/src/core/render/dev/resolve.ts b/packages/astro/src/core/render/dev/resolve.ts index c4fc4e6b3..baf18b4e6 100644 --- a/packages/astro/src/core/render/dev/resolve.ts +++ b/packages/astro/src/core/render/dev/resolve.ts @@ -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; + } + }; } diff --git a/packages/astro/src/core/render/environment.ts b/packages/astro/src/core/render/environment.ts index 0afad9517..8746df5f1 100644 --- a/packages/astro/src/core/render/environment.ts +++ b/packages/astro/src/core/render/environment.ts @@ -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 & { 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, }); } - diff --git a/packages/astro/src/core/render/index.ts b/packages/astro/src/core/render/index.ts index 4c543b598..67cf74deb 100644 --- a/packages/astro/src/core/render/index.ts +++ b/packages/astro/src/core/render/index.ts @@ -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'; diff --git a/packages/astro/src/core/render/renderer.ts b/packages/astro/src/core/render/renderer.ts index e82296a2a..3585a08c2 100644 --- a/packages/astro/src/core/render/renderer.ts +++ b/packages/astro/src/core/render/renderer.ts @@ -6,23 +6,31 @@ export type RendererServerEntrypointModule = { export type MaybeRendererServerEntrypointModule = Partial; export type RendererLoader = (entryPoint: string) => Promise; -export async function loadRenderer(renderer: AstroRenderer, loader: RendererLoader): Promise { +export async function loadRenderer( + renderer: AstroRenderer, + loader: RendererLoader +): Promise { 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[] { +export function filterFoundRenderers( + renderers: Array +): 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, }; } diff --git a/packages/astro/src/jsx/component.ts b/packages/astro/src/jsx/component.ts index 2c818334d..e0ce37ef2 100644 --- a/packages/astro/src/jsx/component.ts +++ b/packages/astro/src/jsx/component.ts @@ -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; diff --git a/packages/astro/src/jsx/index.ts b/packages/astro/src/jsx/index.ts index 00be71026..2d5904e04 100644 --- a/packages/astro/src/jsx/index.ts +++ b/packages/astro/src/jsx/index.ts @@ -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'; diff --git a/packages/astro/src/runtime/server/index.ts b/packages/astro/src/runtime/server/index.ts index 065fbea0a..74219f4c5 100644 --- a/packages/astro/src/runtime/server/index.ts +++ b/packages/astro/src/runtime/server/index.ts @@ -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'; diff --git a/packages/astro/src/vite-plugin-astro-server/index.ts b/packages/astro/src/vite-plugin-astro-server/index.ts index 54bd42909..2039c0a86 100644 --- a/packages/astro/src/vite-plugin-astro-server/index.ts +++ b/packages/astro/src/vite-plugin-astro-server/index.ts @@ -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), diff --git a/packages/astro/test/units/render/jsx.test.js b/packages/astro/test/units/render/jsx.test.js index f2c2ceead..8a45fb3f3 100644 --- a/packages/astro/test/units/render/jsx.test.js +++ b/packages/astro/test/units/render/jsx.test.js @@ -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`
${renderSlot(result, slots['myslot'])}
`; }); - + 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('

works

'); });