0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-30 22:03:56 -05:00

Expose the ssr manifest (#6435)

* Expose the ssr manifest

* Add changeset

* Add types for virtual mod
This commit is contained in:
Matthew Phillips 2023-03-07 11:35:54 -05:00 committed by GitHub
parent 2751584387
commit a206106098
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 102 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'astro': minor
---
Expose the manifest to plugins via the astro:ssr-manifest virtual module

View file

@ -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

View file

@ -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}';
${

View file

@ -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),

View file

@ -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();

View file

@ -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;
},
};
}

View file

@ -0,0 +1,8 @@
{
"name": "@test/ssr-manifest",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}

View file

@ -0,0 +1,17 @@
---
import { manifest } from 'astro:ssr-manifest';
---
<html>
<head>
<title>Testing</title>
<style>
body {
background: green;
}
</style>
</head>
<body>
<h1>Testing</h1>
<div id="assets" set:html={JSON.stringify([...manifest.assets])}></div>
</body>
</html>

View file

@ -0,0 +1,30 @@
import { expect } from 'chai';
import { loadFixture } from './test-utils.js';
import testAdapter from './test-adapter.js';
import * as cheerio from 'cheerio';
describe('astro:ssr-manifest', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/ssr-manifest/',
output: 'server',
adapter: testAdapter(),
});
await fixture.build();
});
it('works', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/');
const response = await app.render(request);
const html = await response.text();
const $ = cheerio.load(html);
expect($('#assets').text()).to.equal('["/_astro/index.1bad7273.css"]');
});
});

View file

@ -2410,6 +2410,12 @@ importers:
dependencies:
astro: link:../../..
packages/astro/test/fixtures/ssr-manifest:
specifiers:
astro: workspace:*
dependencies:
astro: link:../../..
packages/astro/test/fixtures/ssr-markdown:
specifiers:
astro: workspace:*