netlify-redirects.mjs
31 lines 1.7 KB
Raw
sha256:94ec65bd2b200240ac785a97cf14c5db066832bd608a24d6a9c151f17b918b02 feat(calendar): hosted bridge/gateway route parity and time… Human minor ⚠ breaking 21 hours ago
1 /**
2 * Writes public/_redirects so traffic goes to gateway or bridge function.
3 * Gateway site: leave USE_BRIDGE_FUNCTION unset. Bridge site: true (set in Netlify UI
4 * or in deploy/bridge/netlify.toml [build.environment]). Root netlify.toml must not
5 * declare a catch-all [[redirects]]—it would apply to every linked site in the monorepo.
6 */
7 import { mkdir, writeFile, readFile } from 'fs/promises';
8 import { dirname } from 'path';
9 import { fileURLToPath } from 'url';
10
11 const __dirname = dirname(fileURLToPath(import.meta.url));
12 // Base must end with / so `_redirects` is under public/ (otherwise URL resolution drops `public`).
13 const publicDir = new URL('../public/', import.meta.url);
14 const redirectsPath = new URL('_redirects', publicDir);
15
16 const useBridge = process.env.USE_BRIDGE_FUNCTION === 'true' || process.env.USE_BRIDGE_FUNCTION === '1';
17 console.log('[netlify-redirects] USE_BRIDGE_FUNCTION=%s → %s', process.env.USE_BRIDGE_FUNCTION ?? '(unset)', useBridge ? 'bridge' : 'gateway');
18
19 await mkdir(publicDir, { recursive: true });
20 // Both sites must use :splat so the function URL includes the visitor path (e.g. /api/v1/notes).
21 // Without :splat, every request hits the function as "/" and the canister returns 404 Not found.
22 const line = useBridge
23 ? '/* /.netlify/functions/bridge/:splat 200'
24 : '/* /.netlify/functions/gateway/:splat 200';
25 await writeFile(redirectsPath, line + '\n', 'utf8');
26 const content = await readFile(redirectsPath, 'utf8');
27 if (!content.includes(line.trim())) {
28 console.error('[netlify-redirects] Build assertion failed: public/_redirects does not contain expected line:', line.trim());
29 process.exit(1);
30 }
31 console.log('[netlify-redirects] Wrote public/_redirects: %s', line);
File History 1 commit
sha256:94ec65bd2b200240ac785a97cf14c5db066832bd608a24d6a9c151f17b918b02 feat(calendar): hosted bridge/gateway route parity and time… Human minor 21 hours ago