menu

Next.js

A place to chat about Next.js and answer questions. For all other ZEIT products, check this out.

Channels
Team

Next static export crashing graphql server (I think)

June 24, 2019 at 1:09pm

Next static export crashing graphql server (I think)

June 24, 2019 at 1:09pm
I am pushing the code that I've been developing locally to the production server for my product in anticipation of launch, and when I run 'npm run export` to generate my static site, the host / graphql server seems to crash. The host / graphql server is a Wordpress site with well over 10,000 articles. This never happened in development.
I think what is happening is that since the application is being exported on the same server as the Wordpress site is that the requests and responses are coming so far that the Wordpress server is overwhelmed. The result is that I start getting the following:
Error while running `getMarkupFromTree` { Error: Network error: Unexpected token < in JSON at position 0
at new ApolloError (/var/www/next/node_modules/apollo-boost/node_modules/apollo-client/bundle.umd.js:92:26)
at /var/www/next/node_modules/apollo-boost/node_modules/apollo-client/bundle.umd.js:1581:34
at /var/www/next/node_modules/apollo-boost/node_modules/apollo-client/bundle.umd.js:2001:15
at Set.forEach (<anonymous>)
at /var/www/next/node_modules/apollo-boost/node_modules/apollo-client/bundle.umd.js:1999:26
at Map.forEach (<anonymous>)
at QueryManager.broadcastQueries (/var/www/next/node_modules/apollo-boost/node_modules/apollo-client/bundle.umd.js:1997:20)
at /var/www/next/node_modules/apollo-boost/node_modules/apollo-client/bundle.umd.js:1476:29
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:228:7)
graphQLErrors: [],
networkError:
{ ServerParseError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at /var/www/next/node_modules/apollo-link-http-common/lib/index.js:35:25
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:228:7)
name: 'ServerParseError',
response:
Response {
size: 0,
timeout: 0,
[Symbol(Body internals)]: [Object],
[Symbol(Response internals)]: [Object] },
statusCode: 502,
bodyText: '<html>\r\n<head><title>502 Bad Gateway</title></head>\r\n<body bgcolor="white">\r\n<center><h1>502 Bad Gateway</h1></center>\r\n<hr><center>nginx/1.10.3 (Ubuntu)</center>\r\n</body>\r\n</html>\r\n' },
message: 'Network error: Unexpected token < in JSON at position 0',
extraInfo: undefined }
Does anyone have experience dealing with something similar?
It seems to me there needs to be some sort of rate limiting put in place, but since this is the Next.js export process, I can't really jump in the middle of the rendering to add some sort of delay.

June 24, 2019 at 2:35pm
Would love to know if there's a solution to this also. What I found is when the export process begins it creates multiple threads (I imagine to make the export faster) that calls the API in quick succession and overwhelms it. I haven't been able to work out how to limit these threads down but I think that might help prevent the errors?
like-fill
1
  • reply
  • like

June 26, 2019 at 9:08am
Would love to get some feedback here, I can't imagine other large sites being able to avoid this. You're basically DDOSing yourself when you export if you have a large site.
  • reply
  • like
So if you do 'next export -h' it gives you some help:
Usage
$ next export [options] <dir>
<dir> represents where the compiled dist folder should go.
If no directory is provided, the 'out' folder will be created in the current directory.
Options
-h - list this help
-o - set the output dir (defaults to 'out')
-s - do not print any messages to console
shows you can set the concurrency and threads via options. Not sure how you set the options yet though.
Edited
like-fill
1
  • reply
  • like
You do 'next export --threads 1 --concurrency 1' to limit them down.
  • reply
  • like

June 27, 2019 at 8:02am
Excellent find, thanks . Will give this a test and report back here.
  • reply
  • like
Yep, works beautifully, this is the answer, thanks again!
like-fill
1
  • reply
  • like