Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle server restart from Vite plugins #5849

Merged
merged 1 commit into from
Jan 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/poor-ladybugs-thank.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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();
}
});
});