From 7f74326b762bfc174ebe8e37ae03733563e4214f Mon Sep 17 00:00:00 2001 From: Ben Holmes Date: Fri, 7 Apr 2023 12:28:55 -0400 Subject: [PATCH] Fix: `astro:server:setup` middleware (#6781) * Revert "Fix: stop executing `astro:server:setup` twice (#6693)" This reverts commit c0b7864a41dd9f31e5a588208d1ff806d4edf047. * 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 --- .changeset/wet-chefs-love.md | 5 ++++ packages/astro/src/core/create-vite.ts | 2 ++ packages/astro/src/core/dev/container.ts | 2 -- .../index.ts | 20 ++++++++++++++++ .../integration-server-setup/astro.config.mjs | 6 +++++ .../integration-server-setup/integration.js | 15 ++++++++++++ .../integration-server-setup/package.json | 9 +++++++ .../test/integration-server-setup.test.js | 24 +++++++++++++++++++ pnpm-lock.yaml | 6 +++++ 9 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 .changeset/wet-chefs-love.md create mode 100644 packages/astro/src/vite-plugin-integrations-container/index.ts create mode 100644 packages/astro/test/fixtures/integration-server-setup/astro.config.mjs create mode 100644 packages/astro/test/fixtures/integration-server-setup/integration.js create mode 100644 packages/astro/test/fixtures/integration-server-setup/package.json create mode 100644 packages/astro/test/integration-server-setup.test.js diff --git a/.changeset/wet-chefs-love.md b/.changeset/wet-chefs-love.md new file mode 100644 index 000000000000..4ffc4a7657f7 --- /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 e658a0c76be3..ab7208b7d01c 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 cdc52c7e19ca..b65a2c48bbb6 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 000000000000..0169a6f71560 --- /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 000000000000..0a0a336976b4 --- /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 000000000000..bcbd86228a89 --- /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 000000000000..c3ded67ef3a2 --- /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 000000000000..abe41d2bb8a3 --- /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 cfc4d3a50bb0..08c38a58a260 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:*