Improve HMR (#1896)

This commit is contained in:
Drew Powers 2021-11-18 23:55:24 -07:00 committed by GitHub
parent 6b9ec716ee
commit b1586a87ac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 10 deletions

View file

@ -21,6 +21,7 @@ import { createVite } from '../create-vite.js';
import * as msg from './messages.js';
import notFoundTemplate, { subpathNotUsedTemplate } from './template/4xx.js';
import serverErrorTemplate from './template/5xx.js';
import { viteifyURL } from '../util.js';
export interface DevOptions {
logging: LogOptions;
@ -305,9 +306,6 @@ export class AstroDevServer {
next();
return;
}
this.mostRecentRoute = route;
// handle .astro and .md pages
filePath = new URL(`./${route.component}`, this.config.projectRoot);
const html = await ssr({
@ -321,6 +319,7 @@ export class AstroDevServer {
routeCache: this.routeCache,
viteServer: this.viteServer,
});
this.mostRecentRoute = route;
info(this.logging, 'astro', msg.req({ url: pathname, statusCode: 200, reqTime: performance.now() - reqStart }));
res.writeHead(200, {
'Content-Type': mime.getType('.html') as string,
@ -329,9 +328,17 @@ export class AstroDevServer {
res.write(html);
res.end();
} catch (err: any) {
this.viteServer.ws.send({ type: 'error', err });
const statusCode = 500;
const html = serverErrorTemplate({
const mod = filePath && this.viteServer.moduleGraph.getModuleById(viteifyURL(filePath));
if (mod) {
for (const m of [mod, ...mod.importedModules]) {
this.viteServer.moduleGraph.invalidateModule(m);
}
} else {
this.viteServer.moduleGraph.invalidateAll();
}
this.viteServer.ws.send({ type: 'error', err });
let html = serverErrorTemplate({
statusCode,
title: 'Internal Error',
tabTitle: '500: Error',
@ -339,6 +346,7 @@ export class AstroDevServer {
url: err.url || undefined,
stack: stripAnsi(err.stack),
});
html = await this.viteServer.transformIndexHtml(pathname, html, pathname);
info(this.logging, 'astro', msg.req({ url: pathname, statusCode: 500, reqTime: performance.now() - reqStart }));
res.writeHead(statusCode, {
'Content-Type': mime.getType('.html') as string,

View file

@ -123,10 +123,10 @@ ${err.url}`;
throw err;
}
},
async handleHotUpdate(context) {
if (devServer) {
return devServer.handleHotUpdate(context);
}
},
// async handleHotUpdate(context) {
// if (devServer) {
// return devServer.handleHotUpdate(context);
// }
// },
};
}