Skip to content

Commit b279858

Browse files
feat(dev-server): add "ping" route (#5414)
* feat(dev-server): add ping route This commit adds a new `/ping` route to the dev server handler. This route will return a 200 response once the Stencil build has finished. * feat(dev-server): make "ping" route configurable This commit adds an option to the dev server config object as a part of the Stencil config to allow users to change the route for the "ping" response. The user supplied route is validated when validating the dev server config. If the user sets the ping route to `null`, the route will not be registered. * handle failed build * add tests * future-proof ping route default test * add ping route prefix test Co-authored-by: Ryan Waskiewicz <ryanwaskiewicz@gmail.com> --------- Co-authored-by: Ryan Waskiewicz <ryanwaskiewicz@gmail.com>
1 parent c879800 commit b279858

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed

src/compiler/config/test/validate-dev-server.spec.ts

+28
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,32 @@ describe('validateDevServer', () => {
276276
const { config } = validateConfig(inputConfig, mockLoadConfigInit());
277277
expect(config.devServer.prerenderConfig).toBe(wwwOutputTarget.prerenderConfig);
278278
});
279+
280+
describe('pingRoute', () => {
281+
it('should default to /ping', () => {
282+
// Ensure the pingRoute is not set in the inputConfig so we know we're testing the
283+
// default value added during validation
284+
delete inputConfig.devServer.pingRoute;
285+
const { config } = validateConfig(inputConfig, mockLoadConfigInit());
286+
expect(config.devServer.pingRoute).toBe('/ping');
287+
});
288+
289+
it('should set user defined pingRoute', () => {
290+
inputConfig.devServer = { ...inputDevServerConfig, pingRoute: '/my-ping' };
291+
const { config } = validateConfig(inputConfig, mockLoadConfigInit());
292+
expect(config.devServer.pingRoute).toBe('/my-ping');
293+
});
294+
295+
it('should prefix pingRoute with a "/"', () => {
296+
inputConfig.devServer = { ...inputDevServerConfig, pingRoute: 'my-ping' };
297+
const { config } = validateConfig(inputConfig, mockLoadConfigInit());
298+
expect(config.devServer.pingRoute).toBe('/my-ping');
299+
});
300+
301+
it('should clear ping route if set to null', () => {
302+
inputConfig.devServer = { ...inputDevServerConfig, pingRoute: null };
303+
const { config } = validateConfig(inputConfig, mockLoadConfigInit());
304+
expect(config.devServer.pingRoute).toBe(null);
305+
});
306+
});
279307
});

src/compiler/config/validate-dev-server.ts

+10
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ export const validateDevServer = (config: d.ValidatedConfig, diagnostics: d.Diag
2929

3030
devServer.address = devServer.address.split('/')[0];
3131

32+
// Validate "ping" route option
33+
if (devServer.pingRoute !== null) {
34+
let pingRoute = isString(devServer.pingRoute) ? devServer.pingRoute : '/ping';
35+
if (!pingRoute.startsWith('/')) {
36+
pingRoute = `/${pingRoute}`;
37+
}
38+
39+
devServer.pingRoute = pingRoute;
40+
}
41+
3242
// split on `:` to get the domain and the (possibly present) port
3343
// separately. we've already sliced off the protocol (if present) above
3444
// so we can safely split on `:` here.

src/declarations/stencil-public-compiler.ts

+9
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,15 @@ export interface DevServerConfig extends StencilDevServerConfig {
651651
prerenderConfig?: string;
652652
protocol?: 'http' | 'https';
653653
srcIndexHtml?: string;
654+
655+
/**
656+
* Route to be used for the "ping" sub-route of the Stencil dev server.
657+
* This route will return a 200 status code once the Stencil build has finished.
658+
* Setting this to `null` will disable the ping route.
659+
*
660+
* Defaults to `/ping`
661+
*/
662+
pingRoute?: string | null;
654663
}
655664

656665
export interface HistoryApiFallback {

src/dev-server/request-handler.ts

+15
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,21 @@ export function createRequestHandler(devServerConfig: d.DevServerConfig, serverC
2626
return serverCtx.serve302(req, res);
2727
}
2828

29+
if (devServerConfig.pingRoute !== null && req.pathname === devServerConfig.pingRoute) {
30+
return serverCtx
31+
.getBuildResults()
32+
.then((result) => {
33+
if (!result.hasSuccessfulBuild) {
34+
return serverCtx.serve500(incomingReq, res, 'Build not successful', 'build error');
35+
}
36+
37+
res.writeHead(200, 'OK');
38+
res.write('OK');
39+
res.end();
40+
})
41+
.catch(() => serverCtx.serve500(incomingReq, res, 'Error getting build results', 'ping error'));
42+
}
43+
2944
if (isDevClient(req.pathname) && devServerConfig.websocket) {
3045
return serveDevClient(devServerConfig, serverCtx, req, res);
3146
}

src/dev-server/test/req-handler.spec.ts

+26
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,32 @@ describe('request-handler', () => {
439439
expect(h).toBe(`88mph<iframe></iframe>`);
440440
});
441441
});
442+
443+
describe('pingRoute', () => {
444+
it('should return a 200 for successful build', async () => {
445+
serverCtx.getBuildResults = () =>
446+
Promise.resolve({ hasSuccessfulBuild: true }) as Promise<d.CompilerBuildResults>;
447+
448+
const handler = createRequestHandler(devServerConfig, serverCtx);
449+
450+
req.url = '/ping';
451+
452+
await handler(req, res);
453+
expect(res.$statusCode).toBe(200);
454+
});
455+
456+
it('should return a 500 for unsuccessful build', async () => {
457+
serverCtx.getBuildResults = () =>
458+
Promise.resolve({ hasSuccessfulBuild: false }) as Promise<d.CompilerBuildResults>;
459+
460+
const handler = createRequestHandler(devServerConfig, serverCtx);
461+
462+
req.url = '/ping';
463+
464+
await handler(req, res);
465+
expect(res.$statusCode).toBe(500);
466+
});
467+
});
442468
});
443469

444470
interface TestServerResponse extends ServerResponse {

0 commit comments

Comments
 (0)