Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentUrl option #12

Merged
merged 4 commits into from
Sep 4, 2018
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
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ Painless universal prerendering for Webpack. Works great with
- [How does it work?](#how-does-it-work)
- [Installation](#installation)
- [Usage](#usage)
- [DOM Prerendering](#dom-prerendering)
- [String Prerendering](#string-prerendering)
- [Injecting content into the HTML](#injecting-content-into-the-html)
- [Prerendering JavaScript Files](#prerendering-javascript-files)
- [DOM Prerendering](#dom-prerendering)
- [String Prerendering](#string-prerendering)
- [Injecting content into the HTML](#injecting-content-into-the-html)
- [Prerendering JavaScript Files](#prerendering-javascript-files)
- [Options](#options)
- [License](#license)

Expand Down Expand Up @@ -250,11 +250,12 @@ const prerenderedHtml = require('!prerender-loader?string!./app.js');

All options are ... optional.

| Option | Type | Default | Description |
|------------|---------|---------|-------------|
| `string` | boolean | false | Output a JS module exporting an HTML String instead of the HTML itself
| `disabled` | boolean | false | Bypass the loader entirely (but still respect `options.string`)
| `params` | object | null | Options to pass to your prerender function
| Option | Type | Default | Description |
| ------------- | ------- | ------------------ | ---------------------------------------------------------------------- |
| `string` | boolean | false | Output a JS module exporting an HTML String instead of the HTML itself |
| `disabled` | boolean | false | Bypass the loader entirely (but still respect `options.string`) |
| `documentUrl` | string | 'http://localhost' | Change the jsdom's URL (affects `window.location`, `document.URL`...) |
| `params` | object | null | Options to pass to your prerender function |


---
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ async function prerender (parentCompilation, request, options, inject, loader) {
// suppress console-proxied eval() errors, but keep console proxying
virtualConsole: new jsdom.VirtualConsole({ omitJSDOMErrors: false }).sendTo(console),

// `url` sets the value returned by `window.location`, `document.URL`...
// Useful for routers that depend on the current URL (such as react-router or reach-router)
url: options.documentUrl || 'http://localhost',

// don't track source locations for performance reasons
includeNodeLocations: false,

Expand Down
12 changes: 12 additions & 0 deletions test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,15 @@ exports[`webpack compilation smoke test (no prerendering) should compile 1`] = `
</html>
"
`;

exports[`webpack compilation smoke test (no prerendering) should propagate child compiler errors 1`] = `
"<!DOCTYPE html>
<html>
<head>
<title>Basic Demo</title>
</head>
<body>
<script type=\\"text/javascript\\" src=\\"bundle.js\\"></script></body>
</html>
"
`;
8 changes: 8 additions & 0 deletions test/fixtures/document-url/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE html>
<html>
<head>
<title>document with different window.location</title>
</head>
<body>
</body>
</html>
1 change: 1 addition & 0 deletions test/fixtures/document-url/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => `<div>${window.location}</div>`;
11 changes: 11 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,15 @@ describe('prerender-loader!x.html', () => {
expect(html).toMatchSnapshot();
});
});

const DOCUMENT_URL = 'http://localhost/page';

describe(`?documentUrl=${DOCUMENT_URL}`, () => {
it('should set the value returned by window.location', async () => {
const { document } = await compileToHtml('document-url', configure({ string: true, documentUrl: DOCUMENT_URL }));

// verify that our DOM-generated content has been prerendered into the static HTML:
expect(document.body.firstElementChild).toHaveProperty('outerHTML', `<div>${DOCUMENT_URL}</div>`);
});
});
});