Skip to content

Commit

Permalink
Merge pull request #61 from dillonkearns/prerender-trigger-after-view…
Browse files Browse the repository at this point in the history
…-function

Try prerender-trigger event only after body has been updated to make sure Elm has rendered.
  • Loading branch information
dillonkearns committed Jan 30, 2020
2 parents 29c79c5 + 358fad0 commit 2059dd0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion generator/src/add-files-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ module.exports = class AddFilesPlugin {
};
});

this.filesToGenerate.forEach(file => {
(this.filesToGenerate || []).forEach(file => {
// Couldn't find this documented in the webpack docs,
// but I found the example code for it here:
// https://github.com/jantimon/html-webpack-plugin/blob/35a154186501fba3ecddb819b6f632556d37a58f/index.js#L470-L478
Expand Down
10 changes: 6 additions & 4 deletions generator/src/develop.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,10 @@ function webpackOptions(
},
resolve: {
modules: [
path.resolve(process.cwd(), `./node_modules`),
path.resolve(process.cwd(), `./node_modules`),
// TODO: find a cleaner way to do this.
// This line just needs a way to point to the `node_modules` directory
// for the library bin (not the user's node_modules, which is the
// for the library bin (not the user's node_modules, which is the
// process.cwd prefixed node_modules above).
path.resolve(path.dirname(require.resolve('webpack')), '../../'),

Expand Down Expand Up @@ -306,7 +306,9 @@ function webpackOptions(
new PrerenderSPAPlugin({
staticDir: path.join(process.cwd(), "dist"),
routes: routes,
renderAfterDocumentEvent: "prerender-trigger"
renderer: new PrerenderSPAPlugin.PuppeteerRenderer({
renderAfterDocumentEvent: "prerender-trigger",
})
})
],
module: {
Expand Down Expand Up @@ -334,7 +336,7 @@ function webpackOptions(
new webpack.NamedModulesPlugin(),
// Prevents compilation errors causing the hot loader to lose state
new webpack.NoEmitOnErrorsPlugin(),
new webpack.HotModuleReplacementPlugin()
new webpack.HotModuleReplacementPlugin()
],
module: {
rules: [
Expand Down
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,14 @@ function loadContentAndInitializeApp(/** @type { init: any } */ mainElmModule)
fromElm.head.forEach(headTag => {
appendTag(headTag);
});

new MutationObserver(function() {
document.dispatchEvent(new Event("prerender-trigger"));
}).observe(document.body, { attributes: true, childList: true, subtree: true});

} else {
setupLinkPrefetching();
}

document.dispatchEvent(new Event("prerender-trigger"));
});

return app
Expand Down

0 comments on commit 2059dd0

Please sign in to comment.