Skip to content

Commit

Permalink
Handle server restart from Vite plugins (#5849)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Jan 14, 2023
1 parent e818cc0 commit 8c100a6
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .changeset/poor-ladybugs-thank.md
@@ -0,0 +1,5 @@
---
'astro': patch
---

Handle server restart from Vite plugins
64 changes: 36 additions & 28 deletions packages/astro/src/core/dev/restart.ts
Expand Up @@ -142,45 +142,53 @@ export async function createContainerWithAutomaticRestart({
},
};

function handleServerRestart(logMsg: string) {
async function handleServerRestart(logMsg: string) {
// eslint-disable-next-line @typescript-eslint/no-shadow
const container = restart.container;
return async function (changedFile: string) {
if (shouldRestartContainer(container, changedFile)) {
const { container: newContainer, error } = await restartContainer({
beforeRestart,
container,
flags,
logMsg,
async handleConfigError(err) {
// Send an error message to the client if one is connected.
await handleConfigError(err);
container.viteServer.ws.send({
type: 'error',
err: {
message: err.message,
stack: err.stack || '',
},
});
const { container: newContainer, error } = await restartContainer({
beforeRestart,
container,
flags,
logMsg,
async handleConfigError(err) {
// Send an error message to the client if one is connected.
await handleConfigError(err);
container.viteServer.ws.send({
type: 'error',
err: {
message: err.message,
stack: err.stack || '',
},
});
restart.container = newContainer;
// Add new watches because this is a new container with a new Vite server
addWatches();
resolveRestart(error);
restartComplete = new Promise<Error | null>((resolve) => {
resolveRestart = resolve;
});
},
});
restart.container = newContainer;
// Add new watches because this is a new container with a new Vite server
addWatches();
resolveRestart(error);
restartComplete = new Promise<Error | null>((resolve) => {
resolveRestart = resolve;
});
}

function handleChangeRestart(logMsg: string) {
return async function (changedFile: string) {
if (shouldRestartContainer(restart.container, changedFile)) {
handleServerRestart(logMsg);
}
};
}

// Set up watches
function addWatches() {
const watcher = restart.container.viteServer.watcher;
watcher.on('change', handleServerRestart('Configuration updated. Restarting...'));
watcher.on('unlink', handleServerRestart('Configuration removed. Restarting...'));
watcher.on('add', handleServerRestart('Configuration added. Restarting...'));
watcher.on('change', handleChangeRestart('Configuration updated. Restarting...'));
watcher.on('unlink', handleChangeRestart('Configuration removed. Restarting...'));
watcher.on('add', handleChangeRestart('Configuration added. Restarting...'));

// Restart the Astro dev server instead of Vite's when the API is called by plugins.
// Ignore the `forceOptimize` parameter for now.
restart.container.viteServer.restart = () => handleServerRestart('Restarting...');
}
addWatches();
return restart;
Expand Down
31 changes: 31 additions & 0 deletions packages/astro/test/units/dev/restart.test.js
Expand Up @@ -180,4 +180,35 @@ describe('dev container restarts', () => {
await restart.container.close();
}
});

it('Is able to restart on viteServer.restart API call', async () => {
const fs = createFs(
{
'/src/pages/index.astro': ``,
},
root
);

const { astroConfig } = await openConfig({
cwd: root,
flags: {},
cmd: 'dev',
logging: defaultLogging,
});
const settings = createSettings(astroConfig, fileURLToPath(root));

let restart = await createContainerWithAutomaticRestart({
params: { fs, root, settings },
});
await startContainer(restart.container);
expect(isStarted(restart.container)).to.equal(true);

try {
let restartComplete = restart.restarted();
await restart.container.viteServer.restart();
await restartComplete;
} finally {
await restart.container.close();
}
});
});

0 comments on commit 8c100a6

Please sign in to comment.