summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Bjorn Lu <bjornlu.dev@gmail.com> 2023-01-14 16:51:01 +0800
committerGravatar GitHub <noreply@github.com> 2023-01-14 16:51:01 +0800
commit8c100a6fe6cc652c3799d1622e12c2c969f30510 (patch)
tree07192c8a6ea569096112c1854550db7431b9152d
parente818cc0466a942919ea3c41585e231c8c80cb3d0 (diff)
downloadastro-8c100a6fe6cc652c3799d1622e12c2c969f30510.tar.gz
astro-8c100a6fe6cc652c3799d1622e12c2c969f30510.tar.zst
astro-8c100a6fe6cc652c3799d1622e12c2c969f30510.zip
Handle server restart from Vite plugins (#5849)
-rw-r--r--.changeset/poor-ladybugs-thank.md5
-rw-r--r--packages/astro/src/core/dev/restart.ts64
-rw-r--r--packages/astro/test/units/dev/restart.test.js31
3 files changed, 72 insertions, 28 deletions
diff --git a/.changeset/poor-ladybugs-thank.md b/.changeset/poor-ladybugs-thank.md
new file mode 100644
index 000000000..109ce43a9
--- /dev/null
+++ b/.changeset/poor-ladybugs-thank.md
@@ -0,0 +1,5 @@
+---
+'astro': patch
+---
+
+Handle server restart from Vite plugins
diff --git a/packages/astro/src/core/dev/restart.ts b/packages/astro/src/core/dev/restart.ts
index 9ded41325..9c71b7aa3 100644
--- a/packages/astro/src/core/dev/restart.ts
+++ b/packages/astro/src/core/dev/restart.ts
@@ -142,35 +142,39 @@ 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);
}
};
}
@@ -178,9 +182,13 @@ export async function createContainerWithAutomaticRestart({
// 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;
diff --git a/packages/astro/test/units/dev/restart.test.js b/packages/astro/test/units/dev/restart.test.js
index 740ee4976..5c8c4a37c 100644
--- a/packages/astro/test/units/dev/restart.test.js
+++ b/packages/astro/test/units/dev/restart.test.js
@@ -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();
+ }
+ });
});