@aryanbansal-launch/edge-utils 0.1.7 β 0.1.9
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/bin/launch-init.js +1 -1
- package/dist/launch/config.js +0 -3
- package/package.json +2 -2
- package/readme.md +76 -80
package/bin/launch-init.js
CHANGED
|
@@ -85,7 +85,7 @@ export default async function handler(request, context) {
|
|
|
85
85
|
`.trim();
|
|
86
86
|
|
|
87
87
|
async function init() {
|
|
88
|
-
console.log(`\n${colors.bright}${colors.cyan}π
|
|
88
|
+
console.log(`\n${colors.bright}${colors.cyan}π create-launch-edge: Contentstack Launch Initializer${colors.reset}\n`);
|
|
89
89
|
|
|
90
90
|
let actionsTaken = 0;
|
|
91
91
|
|
package/dist/launch/config.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aryanbansal-launch/edge-utils",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.9",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
},
|
|
14
14
|
"main": "dist/index.js",
|
|
15
15
|
"bin": {
|
|
16
|
-
"launch-
|
|
16
|
+
"create-launch-edge": "./bin/launch-init.js",
|
|
17
17
|
"launch-config": "./bin/launch-config.js"
|
|
18
18
|
},
|
|
19
19
|
"exports": {
|
package/readme.md
CHANGED
|
@@ -1,132 +1,128 @@
|
|
|
1
1
|
# π Edge Utils for Contentstack Launch
|
|
2
2
|
|
|
3
|
-
[](https://www.npmjs.com/package/@aryanbansal-launch/edge-utils)
|
|
3
|
+
[](https://www.npmjs.com/package/@aryanbansal-launch/edge-utils)
|
|
4
4
|
[](https://opensource.org/licenses/MIT)
|
|
5
5
|
|
|
6
|
-
A
|
|
7
|
-
|
|
8
|
-
---
|
|
9
|
-
|
|
10
|
-
## β¨ Features
|
|
11
|
-
|
|
12
|
-
- π‘οΈ **Security First**: Block AI crawlers and manage IP access with ease.
|
|
13
|
-
- π **Edge Auth**: Implement Basic Auth directly at the edge for specific hostnames.
|
|
14
|
-
- π **Geo-Aware**: Easily extract location data from request headers.
|
|
15
|
-
- βοΈ **Next.js Ready**: Built-in fixes for RSC header issues on Launch proxies.
|
|
16
|
-
- π **Cache Priming**: Easily manage cache warming URLs via CLI.
|
|
17
|
-
- π **Smart Routing**: Declarative redirects based on path and method.
|
|
18
|
-
- β‘ **Zero Dependencies**: Lightweight and optimized for edge runtime limits.
|
|
6
|
+
A comprehensive toolkit for [Contentstack Launch](https://www.contentstack.com/docs/developers/launch) (the high-performance frontend hosting service by [Contentstack](https://www.contentstack.com/)). This library provides production-ready utilities to simplify [Edge Functions](https://www.contentstack.com/docs/developers/launch/edge-functions) development, security, and performance optimization.
|
|
19
7
|
|
|
20
8
|
---
|
|
21
9
|
|
|
22
10
|
## β‘ Quick Start (Recommended)
|
|
23
11
|
|
|
24
|
-
|
|
12
|
+
Bootstrap your edge environment in seconds using our automated initializer. Run this command from your **project root**:
|
|
25
13
|
|
|
26
|
-
### 1. Install
|
|
27
14
|
```bash
|
|
15
|
+
# Install the package
|
|
28
16
|
npm install @aryanbansal-launch/edge-utils
|
|
29
|
-
```
|
|
30
17
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
```bash
|
|
34
|
-
npx launch-init
|
|
18
|
+
# Initialize Edge Functions
|
|
19
|
+
npx create-launch-edge
|
|
35
20
|
```
|
|
36
|
-
This will automatically create the `functions/` directory and a boilerplate `[proxy].edge.js` handler for you.
|
|
37
21
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
22
|
+
This command will automatically create the `functions/` directory and a production-ready `[proxy].edge.js` boilerplate handler.
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## β¨ Features & Deep Dive
|
|
27
|
+
|
|
28
|
+
### π‘οΈ Security & Access Control
|
|
29
|
+
- **[Block AI Crawlers](https://www.contentstack.com/docs/developers/launch/blocking-ai-crawlers)**: Automatically detects and rejects requests from known scrapers (GPTBot, ClaudeBot, etc.) to protect your content and server resources.
|
|
30
|
+
- **[Restricted Default Domains](https://www.contentstack.com/docs/developers/launch/blocking-default-launch-domains-from-google-search)**: By default, Launch provides a `*.contentstackapps.com` domain. This utility forces visitors to your custom domain, which is essential for SEO (preventing duplicate content) and professional branding.
|
|
31
|
+
- **[IP Access Control](https://www.contentstack.com/docs/developers/launch/ip-based-access-control-using-edge-functions)**: Create a lightweight firewall at the edge to whitelist internal teams or block malicious IPs before they hit your application logic.
|
|
32
|
+
|
|
33
|
+
### βοΈ Next.js Optimization
|
|
34
|
+
- **[RSC Header Fix](https://www.contentstack.com/docs/developers/launch/handling-nextjs-rsc-issues-on-launch)**: Next.js React Server Components (RSC) use a special `rsc` header. Sometimes, proxies or caches can incorrectly serve RSC data when a full page load is expected. This utility detects these edge cases and strips the header to ensure the correct response type is served.
|
|
35
|
+
|
|
36
|
+
### π Performance & Geo-Awareness
|
|
37
|
+
- **[Geo-Location Access](https://www.contentstack.com/docs/developers/launch/geolocation-headers-in-launch)**: Contentstack Launch injects geography data into request headers. This utility parses those headers into a clean object (`country`, `city`, `region`, etc.), enabling you to personalize content or restrict features based on user location.
|
|
38
|
+
- **[Cache Priming](https://www.contentstack.com/docs/developers/launch/cache-priming)**: Use the `launch-config` CLI to pre-load critical URLs into the edge cache, eliminating "cold start" latency for your first visitors after a deployment.
|
|
39
|
+
|
|
40
|
+
### π Smart Routing
|
|
41
|
+
- **Declarative Redirects**: Handle complex, logic-based redirects at runtime.
|
|
42
|
+
- **Runtime vs Config**:
|
|
43
|
+
- Use **`launch.json`** ([Static Redirects](https://www.contentstack.com/docs/developers/launch/edge-url-redirects)) for high-performance, simple path-to-path mapping.
|
|
44
|
+
- Use **`redirectIfMatch`** (this library) for dynamic redirects that require logic, such as checking cookies, headers, or geo-location before redirecting.
|
|
43
45
|
|
|
44
46
|
---
|
|
45
47
|
|
|
46
|
-
## π οΈ Usage Example
|
|
48
|
+
## π οΈ Detailed Usage Example
|
|
47
49
|
|
|
48
|
-
|
|
50
|
+
Your `functions/[proxy].edge.js` acts as a **middleware chain**. You can layer these utilities to create complex edge logic:
|
|
49
51
|
|
|
50
52
|
```javascript
|
|
51
53
|
import {
|
|
52
|
-
jsonResponse,
|
|
53
|
-
passThrough,
|
|
54
|
-
redirectIfMatch,
|
|
55
|
-
protectWithBasicAuth,
|
|
56
|
-
ipAccessControl,
|
|
57
|
-
blockAICrawlers,
|
|
58
54
|
blockDefaultDomains,
|
|
55
|
+
handleNextJS_RSC,
|
|
56
|
+
blockAICrawlers,
|
|
57
|
+
ipAccessControl,
|
|
58
|
+
redirectIfMatch,
|
|
59
59
|
getGeoHeaders,
|
|
60
|
-
|
|
60
|
+
passThrough
|
|
61
61
|
} from "@aryanbansal-launch/edge-utils";
|
|
62
62
|
|
|
63
63
|
export default async function handler(request, context) {
|
|
64
|
-
// 1. π‘οΈ
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
64
|
+
// 1. π‘οΈ Force Custom Domain (SEO Best Practice)
|
|
65
|
+
// Blocks access via *.contentstackapps.com
|
|
66
|
+
const domainCheck = blockDefaultDomains(request);
|
|
67
|
+
if (domainCheck) return domainCheck;
|
|
68
|
+
|
|
69
|
+
// 2. βοΈ Fix Next.js RSC Header issues
|
|
70
|
+
// Prevents "JSON-only" responses on page refreshes
|
|
71
|
+
const rscCheck = await handleNextJS_RSC(request, {
|
|
70
72
|
affectedPaths: ["/shop", "/about"]
|
|
71
73
|
});
|
|
72
|
-
if (
|
|
73
|
-
|
|
74
|
-
// 2. π‘οΈ Block AI bots immediately
|
|
75
|
-
const botResponse = blockAICrawlers(request);
|
|
76
|
-
if (botResponse) return botResponse;
|
|
74
|
+
if (rscCheck) return rscCheck;
|
|
77
75
|
|
|
78
|
-
// 3.
|
|
79
|
-
const
|
|
80
|
-
if (
|
|
76
|
+
// 3. π€ Block Aggressive Bots
|
|
77
|
+
const botCheck = blockAICrawlers(request);
|
|
78
|
+
if (botCheck) return botCheck;
|
|
81
79
|
|
|
82
|
-
// 4.
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
username: "admin",
|
|
86
|
-
password: "securepassword123"
|
|
87
|
-
});
|
|
88
|
-
if (authResponse && authResponse.status === 401) return authResponse;
|
|
80
|
+
// 4. π§± Firewall
|
|
81
|
+
const ipCheck = ipAccessControl(request, { allow: ["203.0.113.10"] });
|
|
82
|
+
if (ipCheck) return ipCheck;
|
|
89
83
|
|
|
90
|
-
// 5. π
|
|
91
|
-
const
|
|
92
|
-
path: "/legacy-
|
|
93
|
-
to: "/
|
|
84
|
+
// 5. π Logic-Based Redirects
|
|
85
|
+
const redirect = redirectIfMatch(request, {
|
|
86
|
+
path: "/legacy-page",
|
|
87
|
+
to: "/new-page",
|
|
94
88
|
status: 301
|
|
95
89
|
});
|
|
96
|
-
if (
|
|
90
|
+
if (redirect) return redirect;
|
|
97
91
|
|
|
98
|
-
// 6. π
|
|
92
|
+
// 6. π Personalization
|
|
99
93
|
const geo = getGeoHeaders(request);
|
|
100
|
-
|
|
94
|
+
if (geo.country === "UK") {
|
|
95
|
+
// Custom logic for UK visitors...
|
|
96
|
+
}
|
|
101
97
|
|
|
102
|
-
// 7. π Pass through to
|
|
98
|
+
// 7. π Pass through to Origin
|
|
103
99
|
return passThrough(request);
|
|
104
100
|
}
|
|
105
101
|
```
|
|
106
102
|
|
|
107
103
|
---
|
|
108
104
|
|
|
109
|
-
##
|
|
105
|
+
## βοΈ Configuration CLI
|
|
106
|
+
|
|
107
|
+
Manage your `launch.json` file interactively to handle bulk settings:
|
|
110
108
|
|
|
111
|
-
|
|
112
|
-
-
|
|
113
|
-
|
|
114
|
-
- **`ipAccessControl(request, { allow?, deny? })`**: Simple IP-based firewall.
|
|
109
|
+
```bash
|
|
110
|
+
npx launch-config
|
|
111
|
+
```
|
|
115
112
|
|
|
116
|
-
###
|
|
117
|
-
-
|
|
113
|
+
### Supported Settings:
|
|
114
|
+
- **Bulk Redirects**: Add multiple sources and destinations easily.
|
|
115
|
+
- **Rewrites**: Map internal paths to external APIs or micro-services.
|
|
116
|
+
- **Cache Priming**: Add a comma-separated list of URLs to warm up the CDN.
|
|
118
117
|
|
|
119
|
-
|
|
120
|
-
- **`redirectIfMatch(request, options)`**: Perform SEO-friendly redirects at the edge.
|
|
118
|
+
---
|
|
121
119
|
|
|
122
|
-
|
|
123
|
-
- **`getGeoHeaders(request)`**: Returns an object with `country`, `region`, `city`, `latitude`, `longitude`.
|
|
120
|
+
## π Platform Support
|
|
124
121
|
|
|
125
|
-
|
|
126
|
-
- **`handleNextJS_RSC(request, { affectedPaths })`**: Resolves RSC header issues on Contentstack Launch.
|
|
122
|
+
This library is exclusively optimized for **[Contentstack Launch](https://www.contentstack.com/docs/developers/launch)**. It assumes an environment where `Request`, `Response`, and standard Edge Global APIs are available.
|
|
127
123
|
|
|
128
124
|
---
|
|
129
125
|
|
|
130
|
-
##
|
|
126
|
+
## π License
|
|
131
127
|
|
|
132
|
-
|
|
128
|
+
Distributed under the MIT License. See `LICENSE` for more information.
|