Skip to content

Commit

Permalink
Fix: astro:server:setup middleware (#6781)
Browse files Browse the repository at this point in the history
* Revert "Fix: stop executing `astro:server:setup` twice (#6693)"

This reverts commit c0b7864.

* 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
  • Loading branch information
bholmesdev authored Apr 7, 2023
1 parent 8a0336c commit 7f74326
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/wet-chefs-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix `astro:server:setup` middlewares not applying. This resolves an issue with the Partytown integration in dev.
2 changes: 2 additions & 0 deletions packages/astro/src/core/create-vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 }),
Expand Down
2 changes: 0 additions & 2 deletions packages/astro/src/core/dev/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
runHookConfigDone,
runHookConfigSetup,
runHookServerDone,
runHookServerSetup,
runHookServerStart,
} from '../../integrations/index.js';
import { createDefaultDevSettings, resolveRoot } from '../config/index.js';
Expand Down Expand Up @@ -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,
Expand Down
20 changes: 20 additions & 0 deletions packages/astro/src/vite-plugin-integrations-container/index.ts
Original file line number Diff line number Diff line change
@@ -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 });
},
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from 'rollup'
import test from './integration.js'

export default defineConfig({
integrations: [test()]
})
Original file line number Diff line number Diff line change
@@ -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();
}
);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@test/integration-server-setup",
"type": "module",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
24 changes: 24 additions & 0 deletions packages/astro/test/integration-server-setup.test.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7f74326

Please sign in to comment.