Skip to content
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ The supported options are:
- `enabled`: defaults to `environment === 'production'` so that `prember` only runs during production builds.
- `indexFile`: defaults to `"index.html"`. This is the name we will give to each of the files we create during pre-rendering.
- `emptyFile`: defaults to `"_empty.html"`. This is where we will put a copy of your empty `index.html` as it was before any pre-rendering.
- `requestsPerFastboot`: defaults to `1000`. This tells prember how many requests to pass to a single fastboot instance before creating a new one. This can be useful for memory management.

## Using a custom URL discovery function

Expand Down
39 changes: 24 additions & 15 deletions lib/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const port = 7784;
class Prerender extends Plugin {
constructor(
builtAppTree,
{ urls, indexFile, emptyFile, fastbootOptions },
{ urls, indexFile, emptyFile, fastbootOptions, requestsPerFastboot },
ui,
plugins,
rootURL
Expand All @@ -31,11 +31,14 @@ class Prerender extends Plugin {
this.port = port;
this.host = `localhost:${port}`;
this.fastbootOptions = fastbootOptions || {};
this.requestsPerFastboot = requestsPerFastboot || 1000;
this.app = undefined;
this.visitCounter = 0;
}

async listUrls(app, protocol, host) {
async listUrls(protocol, host) {
let visit = async (url) => {
return this._visit(app, protocol, host, url);
return this._visit(protocol, host, url);
};

if (typeof this.urls === 'function') {
Expand Down Expand Up @@ -109,22 +112,16 @@ class Prerender extends Plugin {
JSON.stringify(pkg)
);

let app = new FastBoot(
Object.assign({}, this.fastbootOptions, {
distPath: this.inputPaths[0],
})
);

let expressServer = express()
.use(this.rootURL, express.static(this.inputPaths[0]))
.listen(this.port);

let hadFailures = false;

for (let url of await this.listUrls(app, this.protocol, this.host)) {
for (let url of await this.listUrls(this.protocol, this.host)) {
try {
hadFailures =
!(await this._prerender(app, this.protocol, this.host, url)) ||
!(await this._prerender(this.protocol, this.host, url)) ||
hadFailures;
} catch (err) {
hadFailures = true;
Expand All @@ -141,7 +138,19 @@ class Prerender extends Plugin {
}
}

async _visit(app, protocol, host, url) {
async _visit(protocol, host, url) {
// keep track of calls to visit so we know when we can recycle the fastboot instance
this.visitCounter++;

if (!this.app || this.visitCounter > this.requestsPerFastboot) {
this.visitCounter = 0;
this.app = new FastBoot(
Object.assign({}, this.fastbootOptions, {
distPath: this.inputPaths[0],
})
);
}

let opts = {
request: {
url,
Expand All @@ -154,11 +163,11 @@ class Prerender extends Plugin {
},
},
};
return await app.visit(url, opts);
return await this.app.visit(url, opts);
}

async _prerender(app, protocol, host, url) {
let page = await this._visit(app, protocol, host, url);
async _prerender(protocol, host, url) {
let page = await this._visit(protocol, host, url);
if (page.statusCode === 200) {
let html = await page.html();
await this._writeFile(url, html);
Expand Down