{"action":"created","issue":{"url":"https://api.github.com/repos/chrisvfritz/prerender-spa-plugin/issues/21","repository_url":"https://api.github.com/repos/chrisvfritz/prerender-spa-plugin","labels_url":"https://api.github.com/repos/chrisvfritz/prerender-spa-plugin/issues/21/labels{/name}","comments_url":"https://api.github.com/repos/chrisvfritz/prerender-spa-plugin/issues/21/comments","events_url":"https://api.github.com/repos/chrisvfritz/prerender-spa-plugin/issues/21/events","html_url":"https://github.com/chrisvfritz/prerender-spa-plugin/pull/21","id":196404074,"number":21,"title":"Properly handle the end of plugin compilation","user":{"login":"molefrog","id":671276,"avatar_url":"https://avatars.githubusercontent.com/u/671276?v=3","gravatar_id":"","url":"https://api.github.com/users/molefrog","html_url":"https://github.com/molefrog","followers_url":"https://api.github.com/users/molefrog/followers","following_url":"https://api.github.com/users/molefrog/following{/other_user}","gists_url":"https://api.github.com/users/molefrog/gists{/gist_id}","starred_url":"https://api.github.com/users/molefrog/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/molefrog/subscriptions","organizations_url":"https://api.github.com/users/molefrog/orgs","repos_url":"https://api.github.com/users/molefrog/repos","events_url":"https://api.github.com/users/molefrog/events{/privacy}","received_events_url":"https://api.github.com/users/molefrog/received_events","type":"User","site_admin":false},"labels":[{"id":347724922,"url":"https://api.github.com/repos/chrisvfritz/prerender-spa-plugin/labels/enhancement","name":"enhancement","color":"84b6eb","default":true}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-12-19T12:37:27Z","updated_at":"2016-12-24T05:19:48Z","closed_at":"2016-12-24T05:19:48Z","pull_request":{"url":"https://api.github.com/repos/chrisvfritz/prerender-spa-plugin/pulls/21","html_url":"https://github.com/chrisvfritz/prerender-spa-plugin/pull/21","diff_url":"https://github.com/chrisvfritz/prerender-spa-plugin/pull/21.diff","patch_url":"https://github.com/chrisvfritz/prerender-spa-plugin/pull/21.patch"},"body":"Hey, we've been using the plugin in a several projects and I must admit it really helped us to boost our workflow! 👍🏻\r\n\r\nHowever, I run into a weird issue when trying to run Webpack compilation using Node.js API. We're building a static site generator service and I needed to be able to build websites in a queue. The service is for internal-use only that's why I basically wait until the previous built is done and then schedule another one. And I've noticed that sometimes static files are not ready yet by the end of compilation. After some debugging it turned out that Phantom.js is still running even after plugin's callback has been called. \r\n\r\nSo I took the example project to verify the bug:\r\n```\r\n# Current implementation\r\n[Webpack Node.js] start webpack compilation...\r\n[index.js] calling done()\r\n[Webpack Node.js] compile callback called\r\n[index.js] /: compileToHTML callback\r\n[index.js] /about: compileToHTML callback\r\n[index.js] /about: FS.writeFile callback\r\n[index.js] /: FS.writeFile callback\r\n```\r\n\r\nIndeed, if you look at `index.js` file here is what happens:\r\n```js\r\nSimpleHtmlPrecompiler.prototype.apply = function (compiler) {\r\n  var self = this\r\n  compiler.plugin('emit', function (compilation, done) {\r\n    self.paths.forEach(function (outputPath) {\r\n      compileToHTML(self.staticDir, outputPath, self.options, function (prerenderedHTML) {\r\n        var folder = Path.join(self.staticDir, outputPath)\r\n        mkdirp(folder, function (error) {\r\n          if (error) throw error\r\n          FS.writeFile(\r\n            Path.join(folder, 'index.html'),\r\n            prerenderedHTML,\r\n            function (error) {\r\n              if (error) throw error\r\n            }\r\n          )\r\n        })\r\n      })\r\n    })\r\n    done()\r\n  })\r\n}\r\n```\r\nThe `done()` is called right away, but there is a lot of things happening like calling phantom, file write etc. I have modified the code so that it waits until all paths are compiled. I use a modularized version of [async's](http://caolan.github.io/async/docs.html#each) `each` function and it shouldn't add a dependency overhead to the project.\r\n\r\nHere is how it works after the patch:\r\n```\r\n[Webpack Node.js] start webpack compilation...\r\n[index.js] /: compileToHTML callback\r\n[index.js] /about: compileToHTML callback\r\n[index.js] /about: FS.writeFile callback\r\n[index.js] calling done()\r\n[index.js] /: FS.writeFile callback\r\n[Webpack Node.js] compile callback called\r\n```\r\n\r\nI presume that most of the people don't use the plugin with Node.js API, instead they use `webpack` command and because of the nature of Node.js it doesn't end the process when there are incomplete IO operations. That's why everything seemed to work 😀\r\n\r\nHope you like the idea. Please check the spelling and grammar in comments, I'm not the native speaker.\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/chrisvfritz/prerender-spa-plugin/issues/comments/269069854","html_url":"https://github.com/chrisvfritz/prerender-spa-plugin/pull/21#issuecomment-269069854","issue_url":"https://api.github.com/repos/chrisvfritz/prerender-spa-plugin/issues/21","id":269069854,"user":{"login":"chrisvfritz","id":2327556,"avatar_url":"https://avatars.githubusercontent.com/u/2327556?v=3","gravatar_id":"","url":"https://api.github.com/users/chrisvfritz","html_url":"https://github.com/chrisvfritz","followers_url":"https://api.github.com/users/chrisvfritz/followers","following_url":"https://api.github.com/users/chrisvfritz/following{/other_user}","gists_url":"https://api.github.com/users/chrisvfritz/gists{/gist_id}","starred_url":"https://api.github.com/users/chrisvfritz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chrisvfritz/subscriptions","organizations_url":"https://api.github.com/users/chrisvfritz/orgs","repos_url":"https://api.github.com/users/chrisvfritz/repos","events_url":"https://api.github.com/users/chrisvfritz/events{/privacy}","received_events_url":"https://api.github.com/users/chrisvfritz/received_events","type":"User","site_admin":false},"created_at":"2016-12-24T05:19:48Z","updated_at":"2016-12-24T05:19:48Z","body":"I just released [version 2.0](https://github.com/chrisvfritz/prerender-spa-plugin/releases/tag/v2.0.0) and had to go with a slightly different solution, but it should achieve the same ends. Try it out and let me know how it goes! 🙂 "}}
{"actor":{"display_login":"chrisvfritz"}}