diff --git a/packages/render/src/@types/config-minimal.ts b/packages/render/src/@types/config-minimal.ts index 5d089ee2c..b334ecac5 100644 --- a/packages/render/src/@types/config-minimal.ts +++ b/packages/render/src/@types/config-minimal.ts @@ -2,9 +2,7 @@ import type { Renderer } from './astro'; export interface AstroConfigMinimal { /** production website, needed for some RSS & Sitemap functions */ - origin: string; - /** the web request (needed for dynamic routes) */ - pathname?: string; + site: string; /** Renderers for SSR */ renderers?: Renderer[]; } diff --git a/packages/render/src/config/index.ts b/packages/render/src/config/index.ts index 9d63fcecb..73c9495dd 100644 --- a/packages/render/src/config/index.ts +++ b/packages/render/src/config/index.ts @@ -1,9 +1,11 @@ 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 { - validateConfig(options); +export function configure(options: AstroConfigMinimal): void { if (!config) { 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}"!`) - } -} diff --git a/packages/render/src/index.ts b/packages/render/src/index.ts index 55dc67f68..3d8493860 100644 --- a/packages/render/src/index.ts +++ b/packages/render/src/index.ts @@ -1,5 +1,5 @@ export type { AstroConfigMinimal } from './@types/config-minimal'; export type { RenderResult } from './render'; -export { setConfig } from './config/index.js'; +export { configure } from './config/index.js'; export { render } from './render/index.js'; diff --git a/packages/render/src/render/index.ts b/packages/render/src/render/index.ts index ef01a8a56..9772f3c08 100644 --- a/packages/render/src/render/index.ts +++ b/packages/render/src/render/index.ts @@ -1,7 +1,7 @@ import type { AstroComponentFactory } from '../internal' import { createResult, renderToString, AstroElement } from './utils.js'; -import { config, validateConfig } from '../config/index.js'; +import { config } from '../config/index.js'; export interface RenderResult { html: { code: string }; @@ -9,9 +9,8 @@ export interface RenderResult { js: AstroElement[]; } -export async function render(Component: AstroComponentFactory, props: Record): Promise { - validateConfig(config); - const result = createResult(config!) +export async function render(Component: AstroComponentFactory, props: Record, req: { url: string }): Promise { + const result = createResult(config!, req); const template = await renderToString(result, Component, props); 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)); diff --git a/packages/render/src/render/utils.ts b/packages/render/src/render/utils.ts index 35208c0bf..f43176551 100644 --- a/packages/render/src/render/utils.ts +++ b/packages/render/src/render/utils.ts @@ -4,7 +4,7 @@ import type { Renderer } from '../@types/astro'; import type { AstroConfigMinimal } from '../@types/config-minimal' import type { AstroComponentFactory, AstroComponent } from '../internal'; -import { defineStyleVars, defineScriptVars, spreadAttributes } from '../internal'; +import { defineStyleVars, defineScriptVars, spreadAttributes } from '../internal/index.js'; export interface CreateResultOptions { /** production website, needed for some RSS & Sitemap functions */ @@ -14,15 +14,15 @@ export interface CreateResultOptions { renderers?: Renderer[]; } -export function createResult({ origin, pathname = '/', renderers = [] }: AstroConfigMinimal): SSRResult { +export function createResult({ site: origin, renderers = [] }: AstroConfigMinimal, req: { url: string }): SSRResult { const result: SSRResult = { styles: new Set(), scripts: new Set(), /** This function returns the `Astro` faux-global */ createAstro(astroGlobal: TopLevelAstro, props: Record, slots: Record | null) { const site = new URL(origin); - const url = new URL('.' + pathname, site); - const canonicalURL = getCanonicalURL('.' + pathname, origin); + const url = new URL(req.url); + const canonicalURL = getCanonicalURL(url.pathname, origin); return { __proto__: astroGlobal, @@ -94,6 +94,6 @@ export async function renderAstroComponent(component: InstanceType, slots: Record|null = null) { const Component = await componentFactory(result, props, slots); - let template = await renderAstroComponent(Component); + const template = await renderAstroComponent(Component); return template; }