Skip to content

Commit

Permalink
Update Vite ws property to hot (#10138)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Feb 16, 2024
1 parent 5ff288f commit 41a6baa
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 19 deletions.
10 changes: 5 additions & 5 deletions packages/astro/src/content/types-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export async function createContentTypesGenerator({
}
const collectionInfo = collectionEntryMap[collectionKey];
if (collectionInfo.type === 'content') {
viteServer.ws.send({
viteServer.hot.send({
type: 'error',
err: new AstroError({
...AstroErrorData.MixedContentDataCollectionError,
Expand Down Expand Up @@ -212,7 +212,7 @@ export async function createContentTypesGenerator({
}
const collectionInfo = collectionEntryMap[collectionKey];
if (collectionInfo.type === 'data') {
viteServer.ws.send({
viteServer.hot.send({
type: 'error',
err: new AstroError({
...AstroErrorData.MixedContentDataCollectionError,
Expand Down Expand Up @@ -359,7 +359,7 @@ async function writeContentFiles({
typeTemplateContent: string;
contentEntryTypes: Pick<ContentEntryType, 'contentModuleTypes'>[];
contentConfig?: ContentConfig;
viteServer: Pick<ViteDevServer, 'ws'>;
viteServer: Pick<ViteDevServer, 'hot'>;
}) {
let contentTypesStr = '';
let dataTypesStr = '';
Expand All @@ -374,7 +374,7 @@ async function writeContentFiles({
collection.type !== 'unknown' &&
collection.type !== collectionConfig.type
) {
viteServer.ws.send({
viteServer.hot.send({
type: 'error',
err: new AstroError({
...AstroErrorData.ContentCollectionTypeMismatchError,
Expand All @@ -387,7 +387,7 @@ async function writeContentFiles({
collection.type === 'data'
? "Try adding `type: 'data'` to your collection config."
: undefined,
location: { file: '' /** required for error overlay `ws` messages */ },
location: { file: '' /** required for error overlay `hot` messages */ },
}) as any,
});
return;
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/dev/restart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export async function restartContainer(container: Container): Promise<Container
);
}
// Inform connected clients of the config error
container.viteServer.ws.send({
container.viteServer.hot.send({
type: 'error',
err: {
message: error.message,
Expand Down
8 changes: 4 additions & 4 deletions packages/astro/src/core/module-loader/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export function createViteLoader(viteServer: vite.ViteDevServer): ModuleLoader {
}
});

const _wsSend = viteServer.ws.send;
viteServer.ws.send = function (...args: any) {
const _wsSend = viteServer.hot.send;
viteServer.hot.send = function (...args: any) {
// If the tsconfig changed, Vite will trigger a reload as it invalidates the module.
// However in Astro, the whole server is restarted when the tsconfig changes. If we
// do a restart and reload at the same time, the browser will refetch and the server
Expand Down Expand Up @@ -75,13 +75,13 @@ export function createViteLoader(viteServer: vite.ViteDevServer): ModuleLoader {
return viteServer.ssrFixStacktrace(err);
},
clientReload() {
viteServer.ws.send({
viteServer.hot.send({
type: 'full-reload',
path: '*',
});
},
webSocketSend(msg) {
return viteServer.ws.send(msg);
return viteServer.hot.send(msg);
},
isHttps() {
return !!viteServer.config.server.https;
Expand Down
10 changes: 5 additions & 5 deletions packages/astro/src/core/sync/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ export async function syncInternal(
)
);

// Patch `ws.send` to bubble up error events
// `ws.on('error')` does not fire for some reason
const wsSend = tempViteServer.ws.send;
tempViteServer.ws.send = (payload: HMRPayload) => {
// Patch `hot.send` to bubble up error events
// `hot.on('error')` does not fire for some reason
const hotSend = tempViteServer.hot.send;
tempViteServer.hot.send = (payload: HMRPayload) => {
if (payload.type === 'error') {
throw payload.err;
}
return wsSend(payload);
return hotSend(payload);
};

try {
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/vite-plugin-astro/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export async function handleHotUpdate(
ctx.server.moduleGraph.invalidateModule(mod);
}
}
ctx.server.ws.send({ type: 'full-reload', path: '*' });
ctx.server.hot.send({ type: 'full-reload', path: '*' });
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@ export default function astroDevToolbar({ settings, logger }: AstroPluginOptions
}
},
configureServer(server) {
server.ws.on('astro:devtoolbar:error:load', (args) => {
server.hot.on('astro:devtoolbar:error:load', (args) => {
logger.error(
'toolbar',
`Failed to load dev toolbar app from ${args.entrypoint}: ${args.error}`
);
});

server.ws.on('astro:devtoolbar:error:init', (args) => {
server.hot.on('astro:devtoolbar:error:init', (args) => {
logger.error(
'toolbar',
`Failed to initialize dev toolbar app ${args.app.name} (${args.app.id}):\n${args.error}`
);
});

server.ws.on('astro:devtoolbar:app:toggled', (args) => {
server.hot.on('astro:devtoolbar:app:toggled', (args) => {
// Debounce telemetry to avoid recording events when the user is rapidly toggling apps for debugging
clearTimeout(telemetryTimeout);
telemetryTimeout = setTimeout(() => {
Expand Down

0 comments on commit 41a6baa

Please sign in to comment.