Handle initial content.json errors with hot reloading.

This commit is contained in:
Dillon Kearns 2021-04-23 13:51:12 -07:00
parent 0e8f48bb72
commit a2d17c26c8
2 changed files with 35 additions and 26 deletions

View File

@ -219,8 +219,13 @@ async function start(options) {
}
} catch (error) {
console.log({ error });
res.writeHead(500, { "Content-Type": "text/html" });
res.end(errorHtml());
if (req.url.includes("content.json")) {
res.writeHead(500, { "Content-Type": "application/json" });
res.end(JSON.stringify(error.errorsJson));
} else {
res.writeHead(500, { "Content-Type": "text/html" });
res.end(errorHtml());
}
}
}
}

View File

@ -9,35 +9,39 @@ function connect(sendContentJsonPort, initialErrorPage) {
eventSource = new EventSource("/stream");
window.reloadOnOk = initialErrorPage;
if (initialErrorPage) {
elmJsFetch().then(thenApplyHmr);
handleEvent(sendContentJsonPort, { data: "content.json" });
}
eventSource.onmessage = async function (evt) {
showCompiling("");
if (evt.data === "content.json") {
const elmJsRequest = elmJsFetch();
const fetchContentJson = fetchContentJsonForCurrentPage();
updateAppContentJson = updateContentJsonWith(
fetchContentJson,
sendContentJsonPort
);
try {
await fetchContentJson;
const elmJsResponse = await elmJsRequest;
thenApplyHmr(elmJsResponse);
} catch (errorJson) {
console.log({ errorJson });
showError({
type: "compile-errors",
errors: errorJson,
});
}
} else {
elmJsFetch().then(thenApplyHmr);
}
handleEvent(sendContentJsonPort, evt);
};
}
async function handleEvent(sendContentJsonPort, evt) {
showCompiling("");
if (evt.data === "content.json") {
const elmJsRequest = elmJsFetch();
const fetchContentJson = fetchContentJsonForCurrentPage();
updateAppContentJson = updateContentJsonWith(
fetchContentJson,
sendContentJsonPort
);
try {
await fetchContentJson;
const elmJsResponse = await elmJsRequest;
thenApplyHmr(elmJsResponse);
} catch (errorJson) {
console.log({ errorJson });
showError({
type: "compile-errors",
errors: errorJson,
});
}
} else {
elmJsFetch().then(thenApplyHmr);
}
}
/**
*
* @param {*} fetchContentJsonPromise