reuters-graphics / html-webpack-prerender-plugin

A plugin to prerender and inject JavaScript apps into the static markup generated by html-webpack-plugin at build time.
MIT License
14 stars 5 forks source link

Error when building - self is not defined #18

Open clauderobi opened 3 years ago

clauderobi commented 3 years ago

Hi, I am trying to prerender a simple React project. In fact I duplicated the example found in docs/read.md in an exisitng project and adjusted my webpack.config.js to point to the correct app.js file (different filename).

I am getting this; ERROR in Error: HtmlWebpackPrerenderPlugin ERROR: Error evaluating your app script. ReferenceError: self is not defined

My hunch is that it is a version compatibility issue but I am really not sure. In any case here are the different version of all modules I have ibn the sandbox: "devDependencies": { "@babel/core": "^7.14.6", "@babel/preset-env": "^7.14.5", "@babel/preset-react": "^7.14.5", "babel-loader": "^8.2.2", "clean-webpack-plugin": "^4.0.0-alpha.0", "css-loader": "^5.2.6", "file-loader": "^6.2.0", "html-loader": "^2.1.2", "html-react-parser": "^1.3.0", "html-webpack-plugin": "^5.3.1", "html-webpack-prerender-plugin": "^0.1.0", "markdown-loader": "^6.0.0", "node-sass": "^6.0.0", "react-hot-loader": "^4.13.0", "sass-loader": "^12.1.0", "style-loader": "^2.0.0", "url-loader": "^4.1.1", "webpack": "^5.39.1", "webpack-bundle-analyzer": "^4.4.2", "webpack-cli": "^4.7.2", "webpack-dev-server": "^3.11.2", "webpack-merge": "^5.8.0" }, "dependencies": { "react": "^17.0.2", "react-dom": "^17.0.2", "react-router-dom": "^5.2.0", "styled-components": "^5.3.0" }

Any feedback?

ArcadeRenegade commented 2 years ago

Same issue. I don't think this plugin supports Webpack 5 yet.

clauderobi commented 2 years ago

It is a bit of a pain but I took a completely different approach. On the client side, I simply pull the current DOM, make some adjustments to the headers (to be SEO friendly) and then push the HTML code to the dev server. I configured the server to store the HTML code in files in a dedicated directory. Obviously I am running this only when my app is in dev mode and make sure to visit all the subpages.

My deployment script picks up the files and adds them into the package it creates along with the normal build artefacts. Before that, the script run chrome in headless mode to produce updated pre rendered pages.

Works very well. The only caveat is that the dev server must be running when doing a deployment.