diff --git a/.changeset/wet-chefs-love.md b/.changeset/wet-chefs-love.md new file mode 100644 index 000000000..4ffc4a765 --- /dev/null +++ b/.changeset/wet-chefs-love.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix `astro:server:setup` middlewares not applying. This resolves an issue with the Partytown integration in dev. diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index e658a0c76..ab7208b7d 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -19,6 +19,7 @@ import envVitePlugin from '../vite-plugin-env/index.js'; import astroHeadPlugin from '../vite-plugin-head/index.js'; import htmlVitePlugin from '../vite-plugin-html/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 astroLoadFallbackPlugin from '../vite-plugin-load-fallback/index.js'; import markdownVitePlugin from '../vite-plugin-markdown/index.js'; @@ -119,6 +120,7 @@ export async function createVite( htmlVitePlugin(), jsxVitePlugin({ settings, logging }), astroPostprocessVitePlugin({ settings }), + mode === 'dev' && astroIntegrationsContainerPlugin({ settings, logging }), astroScriptsPageSSRPlugin({ settings }), astroHeadPlugin({ settings }), astroScannerPlugin({ settings }), diff --git a/packages/astro/src/core/dev/container.ts b/packages/astro/src/core/dev/container.ts index cdc52c7e1..b65a2c48b 100644 --- a/packages/astro/src/core/dev/container.ts +++ b/packages/astro/src/core/dev/container.ts @@ -8,7 +8,6 @@ import { runHookConfigDone, runHookConfigSetup, runHookServerDone, - runHookServerSetup, runHookServerStart, } from '../../integrations/index.js'; import { createDefaultDevSettings, resolveRoot } from '../config/index.js'; @@ -91,7 +90,6 @@ export async function createContainer(params: CreateContainerParams = {}): Promi ); await runHookConfigDone({ settings, logging }); const viteServer = await vite.createServer(viteConfig); - runHookServerSetup({ config: settings.config, server: viteServer, logging }); const container: Container = { configFlag: params.configFlag, diff --git a/packages/astro/src/vite-plugin-integrations-container/index.ts b/packages/astro/src/vite-plugin-integrations-container/index.ts new file mode 100644 index 000000000..0169a6f71 --- /dev/null +++ b/packages/astro/src/vite-plugin-integrations-container/index.ts @@ -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 }); + }, + }; +} diff --git a/packages/astro/test/fixtures/integration-server-setup/astro.config.mjs b/packages/astro/test/fixtures/integration-server-setup/astro.config.mjs new file mode 100644 index 000000000..0a0a33697 --- /dev/null +++ b/packages/astro/test/fixtures/integration-server-setup/astro.config.mjs @@ -0,0 +1,6 @@ +import { defineConfig } from 'rollup' +import test from './integration.js' + +export default defineConfig({ + integrations: [test()] +}) diff --git a/packages/astro/test/fixtures/integration-server-setup/integration.js b/packages/astro/test/fixtures/integration-server-setup/integration.js new file mode 100644 index 000000000..bcbd86228 --- /dev/null +++ b/packages/astro/test/fixtures/integration-server-setup/integration.js @@ -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(); + } + ); + } + } + } +} diff --git a/packages/astro/test/fixtures/integration-server-setup/package.json b/packages/astro/test/fixtures/integration-server-setup/package.json new file mode 100644 index 000000000..c3ded67ef --- /dev/null +++ b/packages/astro/test/fixtures/integration-server-setup/package.json @@ -0,0 +1,9 @@ +{ + "name": "@test/integration-server-setup", + "type": "module", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/integration-server-setup.test.js b/packages/astro/test/integration-server-setup.test.js new file mode 100644 index 000000000..abe41d2bb --- /dev/null +++ b/packages/astro/test/integration-server-setup.test.js @@ -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'); + }); +}); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cfc4d3a50..08c38a58a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2139,6 +2139,12 @@ importers: dependencies: astro: link:../../.. + packages/astro/test/fixtures/integration-server-setup: + specifiers: + astro: workspace:* + dependencies: + astro: link:../../.. + packages/astro/test/fixtures/jsx: specifiers: '@astrojs/preact': workspace:*