diff --git a/packages/astro/src/@types/astro-core.ts b/packages/astro/src/@types/astro-core.ts index 575a4ece47..ea5adc02c3 100644 --- a/packages/astro/src/@types/astro-core.ts +++ b/packages/astro/src/@types/astro-core.ts @@ -2,7 +2,7 @@ import type babel from '@babel/core'; import type vite from 'vite'; import type { z } from 'zod'; import type { AstroConfigSchema } from '../core/config'; -import type { AstroComponentFactory } from '../runtime/server'; +import type { AstroComponentFactory, Metadata } from '../runtime/server'; export interface AstroComponentMetadata { displayName: string; @@ -141,6 +141,7 @@ export interface CollectionRSS { export interface ComponentInstance { default: AstroComponentFactory; css?: string[]; + $$metadata: Metadata; getStaticPaths?: (options: GetStaticPathsOptions) => GetStaticPathsResult; } diff --git a/packages/astro/src/core/build/index.ts b/packages/astro/src/core/build/index.ts index 16070de5e7..e19664da3a 100644 --- a/packages/astro/src/core/build/index.ts +++ b/packages/astro/src/core/build/index.ts @@ -10,7 +10,7 @@ import { fileURLToPath } from 'url'; import { createVite } from '../create-vite.js'; import { pad } from '../dev/util.js'; import { defaultLogOptions, levels, warn } from '../logger.js'; -import { ssr } from '../ssr/index.js'; +import { loadSsrModule, ssr } from '../ssr/index.js'; import { generatePaginateFunction } from '../ssr/paginate.js'; import { createRouteManifest, validateGetStaticPathsModule, validateGetStaticPathsResult } from '../ssr/routing.js'; import { generateRssFunction } from '../ssr/rss.js'; @@ -75,6 +75,8 @@ class AstroBuilder { this.manifest.routes.map(async (route) => { const { pathname } = route; const filePath = new URL(`./${route.component}`, this.config.projectRoot); + const { mod } = await loadSsrModule(filePath, this.config, viteServer); + console.log("METADATA", mod.$$metadata); // static pages if (pathname) { allPages.push( diff --git a/packages/astro/src/core/ssr/index.ts b/packages/astro/src/core/ssr/index.ts index 906af24eb1..c2da54f576 100644 --- a/packages/astro/src/core/ssr/index.ts +++ b/packages/astro/src/core/ssr/index.ts @@ -76,15 +76,22 @@ async function resolveRenderers(viteServer: ViteDevServer, ids: string[]): Promi return renderers; } -/** use Vite to SSR */ -export async function ssr({ astroConfig, filePath, logging, mode, origin, pathname, route, routeCache, viteServer }: SSROptions): Promise { - try { - // 1. resolve renderers +export async function loadSsrModule(filePath: URL, astroConfig: AstroConfig, viteServer: ViteDevServer) { + // 1. resolve renderers // Important this happens before load module in case a renderer provides polyfills. const renderers = await resolveRenderers(viteServer, astroConfig.renderers); // 1.5. load module const mod = (await viteServer.ssrLoadModule(fileURLToPath(filePath))) as ComponentInstance; + return { renderers, mod }; +} + +/** use Vite to SSR */ +export async function ssr({ astroConfig, filePath, logging, mode, origin, pathname, route, routeCache, viteServer }: SSROptions): Promise { + try { + // 1. + // Import the module and renderers + const { renderers, mod } = await loadSsrModule(filePath, astroConfig, viteServer); // 2. handle dynamic routes let params: Params = {}; diff --git a/packages/astro/src/runtime/server/index.ts b/packages/astro/src/runtime/server/index.ts index 3f41e702a3..ece4e9b2c3 100644 --- a/packages/astro/src/runtime/server/index.ts +++ b/packages/astro/src/runtime/server/index.ts @@ -6,7 +6,7 @@ import { pathToFileURL } from 'url'; import { valueToEstree } from 'estree-util-value-to-estree'; import * as astring from 'astring'; import shorthash from 'shorthash'; -export { createMetadata } from './metadata.js'; +export { createMetadata, Metadata } from './metadata.js'; const { generate, GENERATOR } = astring; diff --git a/packages/astro/src/runtime/server/metadata.ts b/packages/astro/src/runtime/server/metadata.ts index cb01adb401..12b5726fd9 100644 --- a/packages/astro/src/runtime/server/metadata.ts +++ b/packages/astro/src/runtime/server/metadata.ts @@ -10,7 +10,7 @@ interface ComponentMetadata { componentUrl: string; } -class Metadata { +export class Metadata { public fileURL: URL; private metadataCache: Map; constructor(fileURL: string, public modules: ModuleInfo[], components: any[]) {