Skip to content

Commit 1a452d1

Browse files
authored
fix(launchedbrowser): wait for config to be available before launch puppeteer (#267)
closes 194
1 parent 8954b8d commit 1a452d1

5 files changed

Lines changed: 38 additions & 10 deletions

File tree

blog/page-1.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ publish date: 2019-11-26
55
slug: look at_my-urls Cool
66
slugs:
77
- 'page-1'
8-
- 'a slug'
98
description: This is the first demo page in this sample.
109
---
1110

scully.sampleBlog.config.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ exports.config = {
4747
property: 'id',
4848
},
4949
},
50+
'/user/:userId/friend/:friendCode': {
51+
type: 'ignored',
52+
// type:'json',
53+
userId: {
54+
url: 'https://jsonplaceholder.typicode.com/users',
55+
property: 'id',
56+
},
57+
friendCode: {
58+
url: 'https://jsonplaceholder.typicode.com/users?userId=${userId}',
59+
property: 'id',
60+
},
61+
},
5062
'/blog/:slug': {
5163
type: 'contentFolder',
5264
// postRenderers: ['toc'],

scully/renderPlugins/launchedBrowser.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,38 @@
11
import {Browser, launch, LaunchOptions} from 'puppeteer';
2-
import {Observable} from 'rxjs';
3-
import {shareReplay, take} from 'rxjs/operators';
4-
import {log} from '../utils/log';
2+
import {from, Observable} from 'rxjs';
3+
import {shareReplay, switchMap, take} from 'rxjs/operators';
54
import * as yargs from 'yargs';
6-
import {scullyConfig} from '../utils/config';
5+
import {loadConfig, scullyConfig} from '../utils/config';
6+
import {httpGetJson} from '../utils/httpGetJson';
7+
import {log} from '../utils/log';
8+
import {waitForIt} from './puppeteerRenderPlugin';
79

810
const {showBrowser} = yargs
911
.boolean('sb')
1012
.alias('sb', 'showBrowser')
1113
.describe('sb', 'Shows the puppeteer controlled browser').argv;
12-
13-
/** use shareReplay so the browser will stay in memory during the lifetime of the program */
14-
const launched = obsBrowser().pipe(shareReplay({refCount: false, bufferSize: 1}));
14+
/**
15+
* Returns an Observable with that will fire with the launched puppeteer in there.
16+
*/
17+
const launched = from(loadConfig).pipe(
18+
/** give the system a bit of breathing room, and prevent race */
19+
switchMap(() => from(waitForIt(500))),
20+
switchMap(() => obsBrowser()),
21+
/** use shareReplay so the browser will stay in memory during the lifetime of the program */
22+
shareReplay({refCount: false, bufferSize: 1})
23+
);
1524
export const launchedBrowser: () => Promise<Browser> = () => launched.pipe(take(1)).toPromise();
1625
let browser: Browser;
1726

27+
/**
28+
* Function that creates an observable with the puppeteer browser inside
29+
* @param options
30+
*/
1831
function obsBrowser(options: LaunchOptions = scullyConfig.puppeteerLaunchOptions || {}): Observable<Browser> {
1932
if (showBrowser) {
2033
options.headless = false;
2134
}
35+
options.args = options.args || [];
2236
// options.args = ['--no-sandbox', '--disable-setuid-sandbox'];
2337

2438
const {SCULLY_PUPPETEER_EXECUTABLE_PATH} = process.env;
@@ -56,8 +70,9 @@ function exitHandler(options, exitCode) {
5670
browser = undefined;
5771
}
5872
if (exitCode || exitCode === 0) {
59-
// console.log(exitCode)
73+
// console.log(exitCode);
6074
}
75+
// TODO: kill the server here. (but only if started from scully, not when started from another process)
6176
if (options.exit) {
6277
process.exit();
6378
}

scully/renderPlugins/puppeteerRenderPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export const puppeteerRender = async (route: HandledRoute): Promise<string> => {
8787
return pageHtml;
8888
};
8989

90-
function waitForIt(milliSeconds) {
90+
export function waitForIt(milliSeconds) {
9191
return new Promise(resolve => setTimeout(() => resolve(), milliSeconds));
9292
}
9393

scully/utils/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ const loadIt = async () => {
5454
await updateScullyConfig(compiledConfig);
5555
return scullyConfig;
5656
};
57+
58+
/** export the config as a promise, so you can wait for it when you need config during 'boot' */
5759
export const loadConfig = loadIt();
5860

5961
export const updateScullyConfig = async (config: Partial<ScullyConfig>) => {

0 commit comments

Comments
 (0)