stereobooster / react-snap

👻 Zero-configuration framework-agnostic static prerendering for SPAs
MIT License
5.04k stars 391 forks source link

Error running react-snap: Error: EEXIST: file already exists #394

Open mchkl opened 4 years ago

mchkl commented 4 years ago

Hi, I'm facing a problem when trying to run npm run postbuild after npm run build. It prerenders the routes correctly at first and then creates the 404.html file and that's when I get the following error:

warning: page not found /wp-content/themes/greenmotive/404.html/
🔥  error at /wp-content/themes/greenmotive/404.html/ { Error: EEXIST: file already exists, mkdir '/var/www/html/wordpress/wp-content/themes/greenmotive/react-src/build/404.html'
    at Object.mkdirSync (fs.js:753:3)
    at Function.sync (/var/www/html/wordpress/wp-content/themes/greenmotive/react-src/node_modules/mkdirp/index.js:71:13)
    at saveAsHtml (/var/www/html/wordpress/wp-content/themes/greenmotive/react-src/node_modules/react-snap/index.js:565:12)
    at process._tickCallback (internal/process/next_tick.js:68:7)
  errno: -17,
  syscall: 'mkdir',
  code: 'EEXIST',
  path:
   '/var/www/html/wordpress/wp-content/themes/greenmotive/react-src/build/404.html'

I've already tried to create a file and a folder named 404.html in themes/greenmotive as well as delete 202.html and 404.html from the build folder and re-running npm run postbuild. Didn't work. Still getting the same error.

Also I tried to remove the following lines from the puppeteer_utils.js from react-snap in node_modules:

if (enqued == 2 && options.crawl) {
    addToQueue(`${basePath}${publicPath}/404.html`)
}

After doing this I stopped getting the error but the prerender would run in loop infinitely.

I wonder if anyone had a similar problem and found a solution. I would really appreciate any help solving this problem.

Thank you in advance!

stereobooster commented 4 years ago

Reproducible example?

mchkl commented 4 years ago

I made a repo with the reproducible error here: Github repo

stereobooster commented 4 years ago

I see following error

$ react-snap
events.js:177
      throw er; // Unhandled 'error' event
      ^

Error: ENOENT: no such file or directory, open 'GreenmotiveTest/build/index.html'
Emitted 'error' event at:
    at internal/fs/streams.js:123:12
    at FSReqCallback.oncomplete (fs.js:137:23) {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: 'GreenmotiveTest/build/index.html'

Indeed there is no index.thml only index.php 🤷‍♀

mchkl commented 4 years ago

Sorry, I put the explanation into the README but forgot to mention it. So actually I'm using wptheme-scripts in this project to make it work with wordpress and in order to make a CRA build you have to execute npm run crabuild which will produce index.html.

Update

crabuild is using react-scripts build

stereobooster commented 4 years ago

the problem is it tries to crawl 404 twice:

✅  crawled 3 out of 9 (/react-src/404.html)
⚠️  warning: page not found /react-src/404.html/

first one is /react-src/404.html second one with slash in the end

Probably

<Link to="/">

in 404 component gets resolved to 404.html/

mchkl commented 4 years ago

Hmm.. just as an experiment I deleted the import of the 404 component, the component itself and the Route component that was rendering it. Still the same error


Update

Also just tried to create 404 component again, remove any links inside, add 404 via Helmet component. No effect.

converge commented 4 years ago

do you have postbuild in your package.json and you're only running npm run build and not npm run build; npm run postbuild?

mchkl commented 4 years ago

Now my scripts look like this:

    "scripts": {
        "crastart": "react-scripts start",
        "crabuild": "react-scripts build",
        "craeject": "react-scripts eject",
        "cratest": "react-scripts test",
        "build": "wptheme-scripts build",
        "start": "wptheme-scripts start",
        "wpbuild": "wptheme-scripts wpbuild",
        "wpstart": "wptheme-scripts wpstart",
        "postbuild": "react-snap"
    },

If I change "build" from wptheme-scripts to react-scripts I can execute npm run build and the postbuild runs automatically following the build. But the error persists.