@qelos/plugin-netlify-api 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/index.js +23 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -43,7 +43,7 @@ Optional: set the API URL (default `http://159.203.152.168`) and/or enable bypas
|
|
|
43
43
|
## What it does
|
|
44
44
|
|
|
45
45
|
- **Environment:** Sets `QELOS_API_IP` for the build (and for the proxy function at runtime).
|
|
46
|
-
- **Redirect:** Adds a rewrite
|
|
46
|
+
- **Redirect:** Adds a rewrite in `netlify.toml` and, **after the site build**, prepends the same rule to **`publish/_redirects`** when that file exists. Netlify applies `_redirects` **before** `netlify.toml`; frameworks (e.g. Nuxt `netlify-static`) often emit `/* → /404.html` first, which would otherwise match `/api/*` and never reach the proxy.
|
|
47
47
|
- **Function:** Copies `qelos-api-proxy.ts` from the package into your Netlify **functions directory** (default `netlify/functions`, or `[functions] directory` in `netlify.toml`) during `onPreBuild`, so the bundler sees a normal function file. Forwards requests using `QELOS_API_IP` / `API_HOST` (full URL or hostname).
|
|
48
48
|
|
|
49
49
|
No need to add redirects or commit the proxy yourself; the plugin writes it each build. Optional: add `netlify/functions/qelos-api-proxy.ts` to `.gitignore` if you run local Netlify builds and do not want the copy tracked.
|
package/index.js
CHANGED
|
@@ -3,6 +3,10 @@ const path = require('node:path');
|
|
|
3
3
|
|
|
4
4
|
const DEFAULT_API_URL = 'http://159.203.152.168';
|
|
5
5
|
|
|
6
|
+
/** Must appear before any `/*` rule in publish/_redirects — Netlify evaluates that file before netlify.toml. */
|
|
7
|
+
const REDIRECTS_FILE_LINE =
|
|
8
|
+
'/api/* /.netlify/functions/qelos-api-proxy 200!\n';
|
|
9
|
+
|
|
6
10
|
function resolveFunctionsDirectory(netlifyConfig) {
|
|
7
11
|
const dir = netlifyConfig.functions?.directory ?? 'netlify/functions';
|
|
8
12
|
return path.isAbsolute(dir) ? dir : path.join(process.cwd(), dir);
|
|
@@ -47,4 +51,23 @@ module.exports = {
|
|
|
47
51
|
force: true,
|
|
48
52
|
});
|
|
49
53
|
},
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Nuxt/static generators emit `dist/_redirects` with `/* → 404` first. Netlify runs
|
|
57
|
+
* `_redirects` rules before `netlify.toml`, so that catch-all wins unless `/api/*` is
|
|
58
|
+
* prepended here (after the framework build).
|
|
59
|
+
*/
|
|
60
|
+
async onPostBuild({ constants }) {
|
|
61
|
+
const publishDir = constants.PUBLISH_DIR;
|
|
62
|
+
if (!publishDir) return;
|
|
63
|
+
const redirectsPath = path.join(publishDir, '_redirects');
|
|
64
|
+
let existing = '';
|
|
65
|
+
try {
|
|
66
|
+
existing = await fs.readFile(redirectsPath, 'utf8');
|
|
67
|
+
} catch (e) {
|
|
68
|
+
if (e.code !== 'ENOENT') throw e;
|
|
69
|
+
}
|
|
70
|
+
if (existing.includes('qelos-api-proxy')) return;
|
|
71
|
+
await fs.writeFile(redirectsPath, REDIRECTS_FILE_LINE + existing, 'utf8');
|
|
72
|
+
},
|
|
50
73
|
};
|