diff --git a/.changeset/heavy-kids-heal.md b/.changeset/heavy-kids-heal.md new file mode 100644 index 000000000000..41a1f5824d8d --- /dev/null +++ b/.changeset/heavy-kids-heal.md @@ -0,0 +1,5 @@ +--- +'astro': minor +--- + +Expose the manifest to plugins via the astro:ssr-manifest virtual module diff --git a/packages/astro/client-base.d.ts b/packages/astro/client-base.d.ts index 33ff9cef7c99..5e03583bbb07 100644 --- a/packages/astro/client-base.d.ts +++ b/packages/astro/client-base.d.ts @@ -140,6 +140,10 @@ declare module '*.mdx' { export default load; } +declare module 'astro:ssr-manifest' { + export const manifest: import('./dist/@types/astro').SSRManifest; +} + // Everything below are Vite's types (apart from image types, which are in `client.d.ts`) // CSS modules diff --git a/packages/astro/src/core/build/plugins/plugin-ssr.ts b/packages/astro/src/core/build/plugins/plugin-ssr.ts index a49544659adb..9bf7a059f9d1 100644 --- a/packages/astro/src/core/build/plugins/plugin-ssr.ts +++ b/packages/astro/src/core/build/plugins/plugin-ssr.ts @@ -38,10 +38,12 @@ export function vitePluginSSR(internals: BuildInternals, adapter: AstroAdapter): return `import * as adapter from '${adapter.serverEntrypoint}'; import * as _main from '${pagesVirtualModuleId}'; import { deserializeManifest as _deserializeManifest } from 'astro/app'; +import { _privateSetManifestDontUseThis } from 'astro:ssr-manifest'; const _manifest = Object.assign(_deserializeManifest('${manifestReplace}'), { pageMap: _main.pageMap, renderers: _main.renderers }); +_privateSetManifestDontUseThis(_manifest); const _args = ${adapter.args ? JSON.stringify(adapter.args) : 'undefined'}; export * from '${pagesVirtualModuleId}'; ${ diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index ca4df426e27a..2b540a8486af 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -26,6 +26,7 @@ import markdownVitePlugin from '../vite-plugin-markdown/index.js'; import astroScannerPlugin from '../vite-plugin-scanner/index.js'; import astroScriptsPlugin from '../vite-plugin-scripts/index.js'; import astroScriptsPageSSRPlugin from '../vite-plugin-scripts/page-ssr.js'; +import { vitePluginSSRManifest } from '../vite-plugin-ssr-manifest/index.js'; interface CreateViteOptions { settings: AstroSettings; @@ -116,6 +117,7 @@ export async function createVite( astroContentVirtualModPlugin({ settings }), astroContentImportPlugin({ fs, settings }), astroContentAssetPropagationPlugin({ mode }), + vitePluginSSRManifest(), settings.config.experimental.assets ? [astroAssetsPlugin({ settings, logging, mode })] : [], ], publicDir: fileURLToPath(settings.config.publicDir), diff --git a/packages/astro/src/vite-plugin-astro-server/plugin.ts b/packages/astro/src/vite-plugin-astro-server/plugin.ts index 9fd2a164583f..b15f98fa8b74 100644 --- a/packages/astro/src/vite-plugin-astro-server/plugin.ts +++ b/packages/astro/src/vite-plugin-astro-server/plugin.ts @@ -50,7 +50,8 @@ export default function createVitePluginAstroServer({ handle: baseMiddleware(settings, logging), }); } - viteServer.middlewares.use(async (req, res) => { + // Note that this function has a name so other middleware can find it. + viteServer.middlewares.use(async function astroDevHandler(req, res) { if (req.url === undefined || !req.method) { res.writeHead(500, 'Incomplete request'); res.end(); diff --git a/packages/astro/src/vite-plugin-ssr-manifest/index.ts b/packages/astro/src/vite-plugin-ssr-manifest/index.ts new file mode 100644 index 000000000000..a6cb94915b0c --- /dev/null +++ b/packages/astro/src/vite-plugin-ssr-manifest/index.ts @@ -0,0 +1,26 @@ + +import type { Plugin as VitePlugin } from 'vite'; + +const manifestVirtualModuleId = 'astro:ssr-manifest'; +const resolvedManifestVirtualModuleId = '\0' + manifestVirtualModuleId; + +export function vitePluginSSRManifest(): VitePlugin { + return { + name: '@astrojs/vite-plugin-astro-ssr-manifest', + enforce: 'post', + resolveId(id, parent) { + if(id === manifestVirtualModuleId) { + return resolvedManifestVirtualModuleId; + } + }, + load(id) { + if (id === resolvedManifestVirtualModuleId) { + return `export let manifest = {}; +export function _privateSetManifestDontUseThis(ssrManifest) { + manifest = ssrManifest; +}`; + } + return void 0; + }, + }; +} diff --git a/packages/astro/test/fixtures/ssr-manifest/package.json b/packages/astro/test/fixtures/ssr-manifest/package.json new file mode 100644 index 000000000000..95e82614f10c --- /dev/null +++ b/packages/astro/test/fixtures/ssr-manifest/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/ssr-manifest", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/ssr-manifest/src/pages/index.astro b/packages/astro/test/fixtures/ssr-manifest/src/pages/index.astro new file mode 100644 index 000000000000..f189e711c19a --- /dev/null +++ b/packages/astro/test/fixtures/ssr-manifest/src/pages/index.astro @@ -0,0 +1,17 @@ +--- +import { manifest } from 'astro:ssr-manifest'; +--- + +
+