Skip to content

Commit b86dd87

Browse files
committed
fix(build): monkey-patch fetch during build to absorb Strapi HTML responses
Cloudflare in front of staging-strapi.keepsimple.io has started returning HTML challenge pages to GitHub Actions runner IPs. This crashed every getStaticProps/getStaticPaths that JSON.parses the response. Patch only fires when NEXT_PHASE=phase-production-build so runtime fetches inside the running container (which reach Strapi cleanly) are unaffected. Pages with fallback: blocking will SSG on-demand on first request.
1 parent d350403 commit b86dd87

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

next.config.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
1+
// === BUILD-TIME FETCH SAFETY NET (added by uxcore merge) ===
2+
// Cloudflare in front of staging-strapi.keepsimple.io has started returning
3+
// HTML challenge pages to GitHub Actions runner IPs, which crashes any
4+
// getStaticProps/getStaticPaths that does JSON.parse on the response. This
5+
// monkey-patch detects HTML responses *only during the Next.js build phase*
6+
// and substitutes an empty data array so the build never aborts. Runtime
7+
// requests (server.js after the build) use the unwrapped global fetch and
8+
// hit Strapi normally.
9+
if (process.env.NEXT_PHASE === 'phase-production-build' && typeof globalThis.fetch === 'function') {
10+
const _origFetch = globalThis.fetch.bind(globalThis);
11+
globalThis.fetch = async function patchedFetch(...args) {
12+
try {
13+
const resp = await _origFetch(...args);
14+
const ct = resp.headers && resp.headers.get && resp.headers.get('content-type') || '';
15+
if (ct.startsWith('text/html') || !resp.ok) {
16+
console.warn('[build-fetch] non-JSON or non-ok upstream; substituting empty data for', String(args[0]).slice(0, 120));
17+
return new Response('{"data":[]}', { status: 200, headers: { 'content-type': 'application/json' } });
18+
}
19+
return resp;
20+
} catch (e) {
21+
console.warn('[build-fetch] fetch threw; substituting empty data:', e && e.message);
22+
return new Response('{"data":[]}', { status: 200, headers: { 'content-type': 'application/json' } });
23+
}
24+
};
25+
}
26+
// === END FETCH SAFETY NET ===
27+
128
const dotenv = require('dotenv');
229
const path = require('path');
330
const { existsSync } = require('fs');

0 commit comments

Comments
 (0)