Skip to content

Commit 3a0eaaf

Browse files
explain static assets proxying
1 parent a8c88c8 commit 3a0eaaf

File tree

1 file changed

+46
-1
lines changed
  • src/content/docs/bff/fundamentals/multi-frontend

1 file changed

+46
-1
lines changed

src/content/docs/bff/fundamentals/multi-frontend/index.mdx

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,59 @@ var frontend = new BffFrontend(BffFrontendName.Parse("frontend1"))
155155

156156
See the topic on [Token Management](/bff/fundamentals/tokens.md) for more information about the various token management options.
157157

158-
## Index HTML Proxying
158+
## Handling SPA static assets
159+
160+
The BFF can be configured to handle the static file assets that are typically used when developing SPA based apps.
161+
162+
### Proxying the Index.HTML only
159163

160164
When deploying a multi-frontend BFF, it makes most sense to have the frontend's configured with an `index.html` file that is retrieved from a Content Delivery Network (CDN).
161165

166+
This can be done in various ways. For example, if you use Vite, you can publish static assets with a 'base' configured. This will make sure that any static asset,
167+
(such as images, scripts, etc) are retrieved directly from the CDN. This gives the best performance.
168+
162169
```csharp
163170
var frontend = new BffFrontend(BffFrontendName.Parse("frontend1"))
164171
.WithCdnIndexHtml(new Uri("https://my_cdn/some_app/index.html"))
165172
```
166173

167174
When you do this, the BFF automatically wires up a catch-all route that serves the `index.html` for that specific frontend.
168175
See [Serve the index page from the BFF host](/bff/architecture/ui-hosting.md#serve-the-index-page-from-the-bff-host) for more information.
176+
177+
### Proxying all static assets
178+
179+
When developing a SPA based application, it's very common to use a development webserver such as Vite. While vite can publish static assets with a 'base',
180+
this doesn't work well during development.
181+
182+
The best development experience can be achieved by configuring the BFF to proxy all static assets from the development server:
183+
184+
185+
```csharp
186+
var frontend = new BffFrontend(BffFrontendName.Parse("frontend1"))
187+
.WithProxiedStaticAssets(new Uri("https://localhost:3000")); // https://localhost:3000 would be the URL of your development web server.
188+
```
189+
190+
While this can also be done in production, it will proxy all static assets through the BFF. This will increase the bandwidth consumed by the BFF
191+
and reduce the overall performance of your application.
192+
193+
194+
### Proxying assets based on environment
195+
196+
If you're using a local development server during development, but a CDN in production, you can also configure this as follows:
197+
198+
``` csharp
199+
200+
// In this example, the environment name from the application builder is used to determine
201+
// if we're running in production or not.
202+
var runningInProduction = () => builder.Environment.EnvironmentName == Environments.Production;
203+
204+
// Then, when configuring the frontend, you can switch when the static assets will be proxied.
205+
new BffFrontend(BffFrontendName.Parse("default-frontend"))
206+
.WithBffStaticAssets(new Uri("https://localhost:5010/static"), useCdnWhen: runningInProduction);
207+
208+
```
209+
210+
:::note
211+
This function is evaluated immediately when calling the method `.WithBffStaticAssets` is called. If you call this method during startup,
212+
then the condition is only evaluated at startup time. It's not evaluated at runtime for every request.
213+
:::

0 commit comments

Comments
 (0)