Remove hardcoded Vite middleware handling (#4531)

This commit is contained in:
Bjorn Lu 2022-08-29 23:11:18 +08:00 committed by GitHub
parent ca28d7578b
commit 2d2e38e473
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 6 additions and 17 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Remove hardcoded Vite middleware handling

View file

@ -72,7 +72,6 @@ class AstroBuilder {
hmr: false, hmr: false,
middlewareMode: true, middlewareMode: true,
}, },
appType: 'custom',
}, },
{ astroConfig: this.config, logging, mode: 'build' } { astroConfig: this.config, logging, mode: 'build' }
); );

View file

@ -65,6 +65,7 @@ export async function createVite(
cacheDir: fileURLToPath(new URL('./node_modules/.vite/', astroConfig.root)), // using local caches allows Astro to be used in monorepos, etc. cacheDir: fileURLToPath(new URL('./node_modules/.vite/', astroConfig.root)), // using local caches allows Astro to be used in monorepos, etc.
clearScreen: false, // we want to control the output, not Vite clearScreen: false, // we want to control the output, not Vite
logLevel: 'warn', // log warnings and errors only logLevel: 'warn', // log warnings and errors only
appType: 'custom',
optimizeDeps: { optimizeDeps: {
entries: ['src/**/*'], entries: ['src/**/*'],
exclude: ['node-fetch'], exclude: ['node-fetch'],

View file

@ -28,20 +28,6 @@ interface AstroPluginOptions {
logging: LogOptions; logging: LogOptions;
} }
const BAD_VITE_MIDDLEWARE = [
'viteIndexHtmlMiddleware',
'vite404Middleware',
'viteSpaFallbackMiddleware',
];
function removeViteHttpMiddleware(server: vite.Connect.Server) {
for (let i = server.stack.length - 1; i > 0; i--) {
// @ts-expect-error using internals until https://github.com/vitejs/vite/pull/4640 is merged
if (BAD_VITE_MIDDLEWARE.includes(server.stack[i].handle.name)) {
server.stack.splice(i, 1);
}
}
}
function truncateString(str: string, n: number) { function truncateString(str: string, n: number) {
if (str.length > n) { if (str.length > n) {
return str.substring(0, n) + '…'; return str.substring(0, n) + '…';
@ -386,8 +372,6 @@ export default function createPlugin({ config, logging }: AstroPluginOptions): v
viteServer.watcher.on('unlink', rebuildManifest.bind(null, true)); viteServer.watcher.on('unlink', rebuildManifest.bind(null, true));
viteServer.watcher.on('change', rebuildManifest.bind(null, false)); viteServer.watcher.on('change', rebuildManifest.bind(null, false));
return () => { return () => {
removeViteHttpMiddleware(viteServer.middlewares);
// Push this middleware to the front of the stack so that it can intercept responses. // Push this middleware to the front of the stack so that it can intercept responses.
if (config.base !== '/') { if (config.base !== '/') {
viteServer.middlewares.stack.unshift({ viteServer.middlewares.stack.unshift({