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

Allow web socket based refresh in middleware mode #2482

Merged
merged 3 commits into from
Oct 18, 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/green-arachnid-crossing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@web/storybook-builder": patch
---

Pass Storybook server into middleware mode to enable live refresh
5 changes: 5 additions & 0 deletions .changeset/orange-octopus-murmurs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@web/dev-server-core": patch
---

Allow web socket based refresh in middleware mode, by accepting the source `server` as an option for the `middlewareMode` config property
7 changes: 6 additions & 1 deletion packages/dev-server-core/src/server/DevServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class DevServer {
this.logger,
this.config,
this.fileWatcher,
config.middlewareMode,
!!config.middlewareMode,
);
this.koaApp = app;
if (server) {
Expand All @@ -39,6 +39,11 @@ export class DevServer {
this.connections.delete(connection);
});
});
} else if (
typeof this.config.middlewareMode === 'object' &&
this.config.middlewareMode.server
) {
this.webSockets = new WebSocketsManager(this.config.middlewareMode.server);
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/dev-server-core/src/server/DevServerCoreConfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Middleware } from 'koa';
import { Plugin } from '../plugins/Plugin';
import { Server } from 'net';

export type MimeTypeMappings = Record<string, string>;

Expand All @@ -21,7 +22,7 @@ export interface DevServerCoreConfig {
/**
* Whether to run server or not and allow to use as a middleware connected to another server.
*/
middlewareMode?: boolean;
middlewareMode?: boolean | { server: Server };
basePath?: string;
/**
* The app's index.html file. When set, serves the index.html for non-file requests. Use this to enable SPA routing
Expand Down
6 changes: 4 additions & 2 deletions packages/storybook-builder/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const bail: WdsBuilder['bail'] = async () => {
await wdsServer?.stop();
};

export const start: WdsBuilder['start'] = async ({ startTime, options, router }) => {
export const start: WdsBuilder['start'] = async ({ startTime, options, router, server }) => {
const previewDirOrigin = join(getNodeModuleDir('@storybook/preview'), 'dist');
router.use('/sb-preview', express.static(previewDirOrigin, { immutable: true, maxAge: '5m' }));
router.use(`/${PREBUNDLED_MODULES_DIR}`, express.static(resolve(`./${PREBUNDLED_MODULES_DIR}`)));
Expand Down Expand Up @@ -103,7 +103,9 @@ export const start: WdsBuilder['start'] = async ({ startTime, options, router })
}

// setup middleware mode
wdsFinalConfig.middlewareMode = true;
wdsFinalConfig.middlewareMode = {
server,
};
wdsFinalConfig.port = undefined;
wdsFinalConfig.hostname = undefined;

Expand Down
Loading