Skip to content
This repository was archived by the owner on May 10, 2021. It is now read-only.
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
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ To make the pre-render browser visible and you would be available to debug.

To see all the options available see this [documentation](https://github.com/Tribex/prerenderer#prerendererrenderer-puppeteer-options)

### Renderer server configuration

By default Prerender server start at first free port after 8000 as noted in [documentation](https://github.com/JoshTheDerf/prerenderer#server-options), but if your API require frontend to be run at specific port, you can pass server configuration as follows,

```json
{
"serverConfig": {
"port": 8100
}
}
```

This might be helpful, for example, when API allows CORS headers only for requests from specific origins and not `http://localhost:8000` as in default configuration.

## What is Prerendering?

To quote [prerender-spa-plugin](https://github.com/chrisvfritz/prerender-spa-plugin/blob/master/README.md):
Expand All @@ -84,5 +98,5 @@ To quote [prerender-spa-plugin](https://github.com/chrisvfritz/prerender-spa-plu

## Available Renderers

Currently only `@prerenderer/renderer-puppeteer` is supported, although `@prerenderer/renderer-jsdom`
Currently only `@prerenderer/renderer-puppeteer` is supported, although `@prerenderer/renderer-jsdom`
will probably be supported in the future
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@ module.exports = function prerender(bundler) {
const spinner = ora(chalk.grey('Prerendering')).start();
let routes = ['/']; // the default route
let rendererConfig = {};
let serverConfig = {};
const found = await cosmiconfig('prerender').search();
if (found) {
const { config } = found;
if (Array.isArray(config)) {
routes = config;
} else {
if (config.rendererConfig) ({ rendererConfig } = config);
if (config.serverConfig) ({ serverConfig } = config);
if (config.routes) ({ routes } = config);
}
}
const { outDir } = bundler.options;
const prerenderer = new Prerenderer({
staticDir: outDir,
server: serverConfig,
renderer: new Puppeteer(rendererConfig),
});
try {
Expand Down
9 changes: 9 additions & 0 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const childProcess = require('child_process');
const { promisify } = require('util');
const { promises: fs } = require('fs');
const del = require('del');
const detect = require('detect-port');
const exec = promisify(childProcess.exec);

function runBuild(filename) {
Expand Down Expand Up @@ -58,3 +59,11 @@ it('should run with an api call', async function () {
expect(htmlFile).toEqual(expect.stringContaining('date of publication'));
expect(htmlFile).toEqual(expect.stringContaining('Sample Slide Show'));
});

it('should work with custom server port', async function () {
const config = { serverConfig: { port: await detect(8110) } };
await fs.writeFile('.prerenderrc', JSON.stringify(config));
await runBuild('basic.html');
const htmlFile = await fs.readFile(pathJoin('dist', 'index.html'), 'utf-8');
expect(htmlFile).toEqual(expect.stringContaining(`${baseRoute}/`));
})
16 changes: 16 additions & 0 deletions tests/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"@abide-community/parcel-plugin-clean-out-dir": "^1.0.0",
"cross-env": "^5.2.0",
"del": "^5.0.0",
"detect-port": "^1.3.0",
"jest": "^24.8.0",
"parcel-bundler": "^1.12.3",
"parcel-plugin-prerender": "file:../"
Expand Down