Skip to content

Commit

Permalink
Merge pull request #2482 from modernweb-dev/storybook
Browse files Browse the repository at this point in the history
Allow web socket based refresh in middleware mode
  • Loading branch information
bashmish authored Oct 18, 2023
2 parents ceed039 + 95715f9 commit 35b148d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
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

0 comments on commit 35b148d

Please sign in to comment.