Fix: astro:server:setup middleware (#6781)

* Revert "Fix: stop executing `astro:server:setup` twice (#6693)"

This reverts commit c0b7864a41.

* fix: delay `astro:server:setup` to `configureServer`

* test: middleware from astro:server:setup

* chore: lock

* chore: changeset

* chore: remove minimal example change

* chore: revert minimal env change
This commit is contained in:
Ben Holmes 2023-04-07 12:28:55 -04:00 committed by GitHub
parent 8a0336c362
commit 7f74326b76
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 87 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix `astro:server:setup` middlewares not applying. This resolves an issue with the Partytown integration in dev.

View file

@ -19,6 +19,7 @@ import envVitePlugin from '../vite-plugin-env/index.js';
import astroHeadPlugin from '../vite-plugin-head/index.js'; import astroHeadPlugin from '../vite-plugin-head/index.js';
import htmlVitePlugin from '../vite-plugin-html/index.js'; import htmlVitePlugin from '../vite-plugin-html/index.js';
import { astroInjectEnvTsPlugin } from '../vite-plugin-inject-env-ts/index.js'; import { astroInjectEnvTsPlugin } from '../vite-plugin-inject-env-ts/index.js';
import astroIntegrationsContainerPlugin from '../vite-plugin-integrations-container/index.js';
import jsxVitePlugin from '../vite-plugin-jsx/index.js'; import jsxVitePlugin from '../vite-plugin-jsx/index.js';
import astroLoadFallbackPlugin from '../vite-plugin-load-fallback/index.js'; import astroLoadFallbackPlugin from '../vite-plugin-load-fallback/index.js';
import markdownVitePlugin from '../vite-plugin-markdown/index.js'; import markdownVitePlugin from '../vite-plugin-markdown/index.js';
@ -119,6 +120,7 @@ export async function createVite(
htmlVitePlugin(), htmlVitePlugin(),
jsxVitePlugin({ settings, logging }), jsxVitePlugin({ settings, logging }),
astroPostprocessVitePlugin({ settings }), astroPostprocessVitePlugin({ settings }),
mode === 'dev' && astroIntegrationsContainerPlugin({ settings, logging }),
astroScriptsPageSSRPlugin({ settings }), astroScriptsPageSSRPlugin({ settings }),
astroHeadPlugin({ settings }), astroHeadPlugin({ settings }),
astroScannerPlugin({ settings }), astroScannerPlugin({ settings }),

View file

@ -8,7 +8,6 @@ import {
runHookConfigDone, runHookConfigDone,
runHookConfigSetup, runHookConfigSetup,
runHookServerDone, runHookServerDone,
runHookServerSetup,
runHookServerStart, runHookServerStart,
} from '../../integrations/index.js'; } from '../../integrations/index.js';
import { createDefaultDevSettings, resolveRoot } from '../config/index.js'; import { createDefaultDevSettings, resolveRoot } from '../config/index.js';
@ -91,7 +90,6 @@ export async function createContainer(params: CreateContainerParams = {}): Promi
); );
await runHookConfigDone({ settings, logging }); await runHookConfigDone({ settings, logging });
const viteServer = await vite.createServer(viteConfig); const viteServer = await vite.createServer(viteConfig);
runHookServerSetup({ config: settings.config, server: viteServer, logging });
const container: Container = { const container: Container = {
configFlag: params.configFlag, configFlag: params.configFlag,

View file

@ -0,0 +1,20 @@
import type { Plugin as VitePlugin } from 'vite';
import type { AstroSettings } from '../@types/astro.js';
import type { LogOptions } from '../core/logger/core.js';
import { runHookServerSetup } from '../integrations/index.js';
/** Connect Astro integrations into Vite, as needed. */
export default function astroIntegrationsContainerPlugin({
settings,
logging,
}: {
settings: AstroSettings;
logging: LogOptions;
}): VitePlugin {
return {
name: 'astro:integration-container',
configureServer(server) {
runHookServerSetup({ config: settings.config, server, logging });
},
};
}

View file

@ -0,0 +1,6 @@
import { defineConfig } from 'rollup'
import test from './integration.js'
export default defineConfig({
integrations: [test()]
})

View file

@ -0,0 +1,15 @@
export default function() {
return {
name: '@astrojs/test-integration',
hooks: {
'astro:server:setup': ({ server }) => {
server.middlewares.use(
function middleware(req, res, next) {
res.setHeader('x-middleware', 'true');
next();
}
);
}
}
}
}

View file

@ -0,0 +1,9 @@
{
"name": "@test/integration-server-setup",
"type": "module",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}

View file

@ -0,0 +1,24 @@
import { expect } from 'chai';
import { loadFixture } from './test-utils.js';
describe('Integration server setup', () => {
/** @type {import('./test-utils').DevServer} */
let devServer;
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({ root: './fixtures/integration-server-setup/' });
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
});
it('Adds middlewares in dev', async () => {
const res = await fixture.fetch('/');
expect(res.headers.get('x-middleware')).to.equal('true');
});
});

View file

@ -2139,6 +2139,12 @@ importers:
dependencies: dependencies:
astro: link:../../.. astro: link:../../..
packages/astro/test/fixtures/integration-server-setup:
specifiers:
astro: workspace:*
dependencies:
astro: link:../../..
packages/astro/test/fixtures/jsx: packages/astro/test/fixtures/jsx:
specifiers: specifiers:
'@astrojs/preact': workspace:* '@astrojs/preact': workspace:*