Skip to content

Commit fc43063

Browse files
committed
Document the self.alt pseudo-host for self-requests
Signed-off-by: itowlson <ivan.towlson@fermyon.com>
1 parent e36b3e6 commit fc43063

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

content/spin/v3/http-outbound.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ url = "https://github.com/fermyon/developer/blob/main/content/spin/v3/http-outbo
77

88
---
99
- [Using HTTP From Applications](#using-http-from-applications)
10+
- [Restrictions](#restrictions)
1011
- [Granting HTTP Permissions to Components](#granting-http-permissions-to-components)
1112
- [Configuration-Based Permissions](#configuration-based-permissions)
1213
- [Making HTTP Requests Within an Application](#making-http-requests-within-an-application)
@@ -232,10 +233,14 @@ However, the wildcard implies that the component requires _all other_ components
232233

233234
To make an HTTP request to another route with your application, you can pass just the route as the URL. For example, if you make an outbound HTTP request to `/api/customers/`, Spin prepends the route with whatever host the application is running on. It also replaces the URL scheme (`http` or `https`) with the scheme of the current HTTP request. For example, if the application is running in the cloud, Spin changes `/api` to `https://.../api`.
234235

236+
> You can also use the special host `self.alt` to perform self-requests by route. This is important for the JavaScript `fetch` wrapper, which handles relative requests in a way that doesn't work with `allowed_outbound_hosts`. For example, you would write `fetch('http://self.alt/api')`.
237+
235238
In this way of doing self-requests, the request undergoes normal HTTP processing once Spin has prepended the host. For example, in a cloud deployment, the request passes through the network, and potentially back in through a load balancer or other gateway. The benefit of this is that it allows load to be distributed across the environment, but it may count against your use of bandwidth.
236239

237-
You must still grant permission by including `self` in `allowed_outbound_hosts`:
240+
You must still grant permission by including `self` or `self.alt` in `allowed_outbound_hosts`:
238241

239242
```toml
240-
allowed_outbound_hosts = ["http://self", "https://self"]
243+
allowed_outbound_hosts = ["http://self", "https://self.alt"]
241244
```
245+
246+
> It doesn't matter which you use - either 'allow' form enables both relative and `self.alt` URLs.

content/spin/v3/javascript-components.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ url = "https://github.com/fermyon/developer/blob/main/content/spin/v3/javascript
1111
- [Building and Running the Template](#building-and-running-the-template)
1212
- [HTTP Components](#http-components)
1313
- [Sending Outbound HTTP Requests](#sending-outbound-http-requests)
14+
- [Intra-Application Requests in JavaScript](#intra-application-requests-in-javascript)
1415
- [Storing Data in Redis From JS/TS Components](#storing-data-in-redis-from-jsts-components)
1516
- [Routing in a Component](#routing-in-a-component)
1617
- [Storing Data in the Spin Key-Value Store](#storing-data-in-the-spin-key-value-store)
@@ -155,7 +156,7 @@ The important things to note in the implementation above:
155156

156157
## Sending Outbound HTTP Requests
157158

158-
If allowed, Spin components can send outbound HTTP requests.
159+
If allowed, Spin components can send outbound HTTP requests using the `fetch` function.
159160
Let's see an example of a component that makes a request to [an API that returns random animal facts](https://random-data-api.fermyon.app/animals/json)
160161

161162
```javascript
@@ -233,6 +234,19 @@ This can be the basis for building components that communicate with external
233234
databases or storage accounts, or even more specialized components like HTTP
234235
proxies or URL shorteners.
235236

237+
### Intra-Application Requests in JavaScript
238+
239+
JavaScript's `fetch` function handles relative URLs in a way that doesn't work well with Spin's fine-grained outbound HTTP permissions.
240+
Therefore, when [making a request to another route within the same application](./http-outbound#intra-application-http-requests-by-route),
241+
you must use the special pseudo-host `self.alt` rather than a relative route. For example:
242+
243+
```javascript
244+
await fetch('/api'); // Avoid!
245+
await fetch('http://self.alt/api'); // Prefer!
246+
```
247+
248+
You must [add `http://self` or `http://self.alt` to the component's `allowed_outbound_hosts`](./http-outbound#intra-application-http-requests-by-route).
249+
236250
---
237251

238252
## Storing Data in Redis From JS/TS Components

0 commit comments

Comments
 (0)