nginx middleware for pre-rendering JavaScript single page apps with Headless-Render-API.com (formerly named prerender.cloud from 2016 - 2022)
- The default.conf file is intended as a drop in config for a single standard, single nginx static host serving a single page application (redirects 404s to index.html)
- You can also copy and paste parts or all of the config into your own nginx configuration
If you're using the standard nginx docker image from https://hub.docker.com/_/nginx/
...and assuming your index.html and css and js are in ./build
...and assuming you've copied ./default.conf
into your current directory
docker pull nginx
docker run -p8080:80 -v $(pwd)/build:/usr/share/nginx/html:ro -v $(pwd)/default.conf:/etc/nginx/conf.d/default.conf:ro nginx
- Uncomment the
proxy_set_header X-Prerender-Token
line - replace
YOUR_SECRET_API_TOKEN_HERE
with your actual token you got after signing up at Headless-Render-API.com
- when service.headless-render-api.com service returns
- 400 client error (bad request)
- e.g. try to prerender a localhost URL as opposed to a publicly accessible URL
- the client itself returns the 400 error (the web page will not be accessible)
- 429 client error (rate limited)
- the original server payload (not prerendered) is returned, so the request is not interrupted due to unpaid bills or free accounts
- only happens while on the free tier (paid subscriptions are not rate limited)
- no messages will be written to console, comment out
proxy_intercept_errors
to see the errors over HTTP (but don't do this in production)
- 5xx (server error)
- the original server payload (not prerendered) is returned, so the request is not interrupted due to server error
- no messages will be written to console, comment out
proxy_intercept_errors
to see the errors served over HTTP (but don't do this in production)
- 400 client error (bad request)
This nginx config will cache responses from service.headless-render-api.com for 5 minutes, any request after that will download the latest copy from service.headless-render-api.com. If a request happens while that previous request reached out to service.headless-render-api.com, that (2nd and anything beyond) will use a stale cache copy. Unfortunately nginx does not support returning stale copies for that first request that triggers a download of the latest copy. See http://serverfault.com/questions/576402/nginx-serving-stale-cache-response-while-updating for more details