refactor: cleanup render
package
This commit is contained in:
parent
2a6f2601d6
commit
4eafad8017
5 changed files with 15 additions and 24 deletions
|
@ -2,9 +2,7 @@ import type { Renderer } from './astro';
|
||||||
|
|
||||||
export interface AstroConfigMinimal {
|
export interface AstroConfigMinimal {
|
||||||
/** production website, needed for some RSS & Sitemap functions */
|
/** production website, needed for some RSS & Sitemap functions */
|
||||||
origin: string;
|
site: string;
|
||||||
/** the web request (needed for dynamic routes) */
|
|
||||||
pathname?: string;
|
|
||||||
/** Renderers for SSR */
|
/** Renderers for SSR */
|
||||||
renderers?: Renderer[];
|
renderers?: Renderer[];
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
import type { AstroConfigMinimal } from '../@types/config-minimal';
|
import type { AstroConfigMinimal } from '../@types/config-minimal';
|
||||||
|
|
||||||
export let config: AstroConfigMinimal | null = null;
|
export let config: AstroConfigMinimal = {
|
||||||
|
site: 'https://example.com',
|
||||||
|
renderers: []
|
||||||
|
};
|
||||||
|
|
||||||
export function setConfig(options: AstroConfigMinimal): void {
|
export function configure(options: AstroConfigMinimal): void {
|
||||||
validateConfig(options);
|
|
||||||
if (!config) {
|
if (!config) {
|
||||||
config = {} as any;
|
config = {} as any;
|
||||||
}
|
}
|
||||||
|
@ -12,11 +14,3 @@ export function setConfig(options: AstroConfigMinimal): void {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function validateConfig(config: unknown) {
|
|
||||||
if (!config) {
|
|
||||||
throw new Error(`[Astro]: You must call \`setConfig(value)\` before rendering!`)
|
|
||||||
}
|
|
||||||
if (typeof config !== 'object') {
|
|
||||||
throw new Error(`[Astro]: \`config\` must be of type "object". Found typeof "${typeof config}"!`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
export type { AstroConfigMinimal } from './@types/config-minimal';
|
export type { AstroConfigMinimal } from './@types/config-minimal';
|
||||||
export type { RenderResult } from './render';
|
export type { RenderResult } from './render';
|
||||||
|
|
||||||
export { setConfig } from './config/index.js';
|
export { configure } from './config/index.js';
|
||||||
export { render } from './render/index.js';
|
export { render } from './render/index.js';
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import type { AstroComponentFactory } from '../internal'
|
import type { AstroComponentFactory } from '../internal'
|
||||||
|
|
||||||
import { createResult, renderToString, AstroElement } from './utils.js';
|
import { createResult, renderToString, AstroElement } from './utils.js';
|
||||||
import { config, validateConfig } from '../config/index.js';
|
import { config } from '../config/index.js';
|
||||||
|
|
||||||
export interface RenderResult {
|
export interface RenderResult {
|
||||||
html: { code: string };
|
html: { code: string };
|
||||||
|
@ -9,9 +9,8 @@ export interface RenderResult {
|
||||||
js: AstroElement[];
|
js: AstroElement[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function render(Component: AstroComponentFactory, props: Record<string, any>): Promise<RenderResult> {
|
export async function render(Component: AstroComponentFactory, props: Record<string, any>, req: { url: string }): Promise<RenderResult> {
|
||||||
validateConfig(config);
|
const result = createResult(config!, req);
|
||||||
const result = createResult(config!)
|
|
||||||
const template = await renderToString(result, Component, props);
|
const template = await renderToString(result, Component, props);
|
||||||
const styles = Array.from(result.styles).map(({ props, children }: any) => new AstroElement('style', props, children));
|
const styles = Array.from(result.styles).map(({ props, children }: any) => new AstroElement('style', props, children));
|
||||||
const scripts = Array.from(result.scripts).map(({ props, children }: any) => new AstroElement('script', props, children));
|
const scripts = Array.from(result.scripts).map(({ props, children }: any) => new AstroElement('script', props, children));
|
||||||
|
|
|
@ -4,7 +4,7 @@ import type { Renderer } from '../@types/astro';
|
||||||
import type { AstroConfigMinimal } from '../@types/config-minimal'
|
import type { AstroConfigMinimal } from '../@types/config-minimal'
|
||||||
import type { AstroComponentFactory, AstroComponent } from '../internal';
|
import type { AstroComponentFactory, AstroComponent } from '../internal';
|
||||||
|
|
||||||
import { defineStyleVars, defineScriptVars, spreadAttributes } from '../internal';
|
import { defineStyleVars, defineScriptVars, spreadAttributes } from '../internal/index.js';
|
||||||
|
|
||||||
export interface CreateResultOptions {
|
export interface CreateResultOptions {
|
||||||
/** production website, needed for some RSS & Sitemap functions */
|
/** production website, needed for some RSS & Sitemap functions */
|
||||||
|
@ -14,15 +14,15 @@ export interface CreateResultOptions {
|
||||||
renderers?: Renderer[];
|
renderers?: Renderer[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createResult({ origin, pathname = '/', renderers = [] }: AstroConfigMinimal): SSRResult {
|
export function createResult({ site: origin, renderers = [] }: AstroConfigMinimal, req: { url: string }): SSRResult {
|
||||||
const result: SSRResult = {
|
const result: SSRResult = {
|
||||||
styles: new Set(),
|
styles: new Set(),
|
||||||
scripts: new Set(),
|
scripts: new Set(),
|
||||||
/** This function returns the `Astro` faux-global */
|
/** This function returns the `Astro` faux-global */
|
||||||
createAstro(astroGlobal: TopLevelAstro, props: Record<string, any>, slots: Record<string, any> | null) {
|
createAstro(astroGlobal: TopLevelAstro, props: Record<string, any>, slots: Record<string, any> | null) {
|
||||||
const site = new URL(origin);
|
const site = new URL(origin);
|
||||||
const url = new URL('.' + pathname, site);
|
const url = new URL(req.url);
|
||||||
const canonicalURL = getCanonicalURL('.' + pathname, origin);
|
const canonicalURL = getCanonicalURL(url.pathname, origin);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
__proto__: astroGlobal,
|
__proto__: astroGlobal,
|
||||||
|
@ -94,6 +94,6 @@ export async function renderAstroComponent(component: InstanceType<typeof AstroC
|
||||||
|
|
||||||
export async function renderToString(result: SSRResult, componentFactory: AstroComponentFactory, props: Record<string|number, any>, slots: Record<string, AstroComponentFactory>|null = null) {
|
export async function renderToString(result: SSRResult, componentFactory: AstroComponentFactory, props: Record<string|number, any>, slots: Record<string, AstroComponentFactory>|null = null) {
|
||||||
const Component = await componentFactory(result, props, slots);
|
const Component = await componentFactory(result, props, slots);
|
||||||
let template = await renderAstroComponent(Component);
|
const template = await renderAstroComponent(Component);
|
||||||
return template;
|
return template;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue