Skip to content
Open
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
15 changes: 9 additions & 6 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, requestsPerFastboot },
{ urls, indexFile, emptyFile, flatOutput, fastbootOptions, requestsPerFastboot },
ui,
plugins,
rootURL
Expand All @@ -24,6 +24,7 @@ class Prerender extends Plugin {
this.urls = urls || [];
this.indexFile = indexFile || 'index.html';
this.emptyFile = emptyFile || '_empty.html';
this.flatOutput = flatOutput || false;
this.ui = ui;
this.plugins = plugins;
this.rootURL = rootURL;
Expand Down Expand Up @@ -189,11 +190,13 @@ class Prerender extends Plugin {
}

async _writeFile(url, html) {
let filename = path.join(
this.outputPath,
url.replace(this.rootURL, '/'),
this.indexFile
);
let relativePath = url.replace(this.rootURL, '/');
let filename;
if (this.flatOutput && relativePath !== '/') {
filename = path.join(this.outputPath, relativePath + '.html');
} else {
filename = path.join(this.outputPath, relativePath, this.indexFile);
}
await mkdirp(path.dirname(filename));
await writeFile(filename, html);
}
Expand Down
52 changes: 52 additions & 0 deletions node-tests/flat-output-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const { module: Qmodule, test } = require('qunit');
const fs = require('fs');
const path = require('path');
const os = require('os');

const Prerender = require('../lib/prerender');

function createPrerender(options = {}) {
let instance = Object.create(Prerender.prototype);
instance.rootURL = options.rootURL || '/';
instance.indexFile = options.indexFile || 'index.html';
instance.flatOutput = options.flatOutput || false;
Object.defineProperty(instance, 'outputPath', { value: options.outputPath });
return instance;
}

Qmodule('flatOutput', function (hooks) {
let tmpDir;

hooks.beforeEach(function () {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'prember-test-'));
});

hooks.afterEach(function () {
fs.rmSync(tmpDir, { recursive: true });
});

test('default output writes index.html in subdirectory', async function (assert) {
let prerender = createPrerender({ outputPath: tmpDir });
await prerender._writeFile('/foo/bar', '<html>test</html>');
assert.ok(fs.existsSync(path.join(tmpDir, 'foo', 'bar', 'index.html')));
});

test('flatOutput writes .html files instead of subdirectories', async function (assert) {
let prerender = createPrerender({ outputPath: tmpDir, flatOutput: true });
await prerender._writeFile('/foo/bar', '<html>test</html>');
assert.ok(fs.existsSync(path.join(tmpDir, 'foo', 'bar.html')));
assert.notOk(fs.existsSync(path.join(tmpDir, 'foo', 'bar', 'index.html')));
});

test('flatOutput still writes index.html for root URL', async function (assert) {
let prerender = createPrerender({ outputPath: tmpDir, flatOutput: true });
await prerender._writeFile('/', '<html>root</html>');
assert.ok(fs.existsSync(path.join(tmpDir, 'index.html')));
});

test('flatOutput works with custom rootURL', async function (assert) {
let prerender = createPrerender({ outputPath: tmpDir, flatOutput: true, rootURL: '/app/' });
await prerender._writeFile('/app/foo/bar', '<html>test</html>');
assert.ok(fs.existsSync(path.join(tmpDir, 'foo', 'bar.html')));
});
});