Skip to content

Commit dd34887

Browse files
updated information on default frontends
1 parent 3a0eaaf commit dd34887

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-5
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@ and removed from the [`Path`](https://learn.microsoft.com/en-us/dotnet/api/micro
112112
that happens in the application is relative to the path of the frontend. This also includes the OpenID callback paths.
113113
:::
114114

115+
### Implicit frontend disabled
116+
117+
When you don't add any frontends, then the BFF creates an 'implicit' default frontend. This allows the BFF to function correctly in 'single frontend' mode.
118+
As soon as you add a frontend, then this 'implicit' frontend is disabled.
119+
120+
This means that, if you want to use both explicitly matching frontends (on host headers or paths) and a default (fallback) frontend, that you should explicitly add
121+
this default frontend.
122+
115123
## Adding A Frontend During Startup
116124

117125
The simplest way to add frontends is during startup.

src/content/docs/bff/getting-started/single-frontend.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ Duende.BFF (Backend for Frontend) is a library that helps you build secure, mode
1212

1313
:::note
1414
Duende.BFF V4 introduced a new way of configuring the BFF, which automatically configures the BFF using recommended practices. If you're upgrading from V3, please refer to the [upgrade guide](/bff/upgrading/bff-v3-to-v4.md).
15+
16+
When in single frontend mode, an 'implicit' default frontend is automatically registered. This ensures all the management routes and openid connect handling routes are available for your frontend.
17+
When you call .AddFrontend() to add a new frontend, the system switches to Multi-frontend mode. If you wish to have a default frontend in multi-frontend mode, you'll need to explicitly add it. See [Multi-frontend support](bff/fundamentals/multi-frontend/) for more information on this topic.
18+
1519
:::
1620

1721
## Prerequisites

src/content/docs/bff/upgrading/bff-v3-to-v4.md

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,18 +118,65 @@ This enables you to configure your OpenID Connect options, including secrets, an
118118

119119
See the type `BffConfiguration` to see what settings can be configured.
120120

121-
#### Index HTML Retrieval
121+
## Handling SPA static assets
122122

123-
It's fairly common to deploy your application in such a way to have the BFF be the first entrypoint for your application. It should serve an index.html that will bootstrap your frontend. However, your static content should be loaded from a CDN.
123+
The BFF can be configured to handle the static file assets that are typically used when developing SPA based apps.
124124

125-
If you publish your frontend code to a cdn with absolute paths (for example by specifying a base path in your vite config), then all static content is loaded directly from the CDN.
125+
### Proxying the Index.HTML only
126+
127+
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).
128+
129+
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,
130+
(such as images, scripts, etc) are retrieved directly from the CDN. This gives the best performance.
131+
132+
```csharp
133+
var frontend = new BffFrontend(BffFrontendName.Parse("frontend1"))
134+
.WithCdnIndexHtml(new Uri("https://my_cdn/some_app/index.html"))
135+
```
136+
137+
When you do this, the BFF automatically wires up a catch-all route that serves the `index.html` for that specific frontend.
138+
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.
139+
140+
### Proxying all static assets
141+
142+
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',
143+
this doesn't work well during development.
144+
145+
The best development experience can be achieved by configuring the BFF to proxy all static assets from the development server:
126146

127-
You can configure the location of your `index.html` by specifying:
128147

129148
```csharp
130-
.WithIndexHtmlUrl(new Uri("https://localhost:5005/static/index.html"))
149+
var frontend = new BffFrontend(BffFrontendName.Parse("frontend1"))
150+
.WithProxiedStaticAssets(new Uri("https://localhost:3000")); // https://localhost:3000 would be the URL of your development web server.
131151
```
132152

153+
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
154+
and reduce the overall performance of your application.
155+
156+
157+
### Proxying assets based on environment
158+
159+
If you're using a local development server during development, but a CDN in production, you can also configure this as follows:
160+
161+
``` csharp
162+
163+
// In this example, the environment name from the application builder is used to determine
164+
// if we're running in production or not.
165+
var runningInProduction = () => builder.Environment.EnvironmentName == Environments.Production;
166+
167+
// Then, when configuring the frontend, you can switch when the static assets will be proxied.
168+
new BffFrontend(BffFrontendName.Parse("default-frontend"))
169+
.WithBffStaticAssets(new Uri("https://localhost:5010/static"), useCdnWhen: runningInProduction);
170+
171+
```
172+
173+
:::note
174+
This function is evaluated immediately when calling the method `.WithBffStaticAssets` is called. If you call this method during startup,
175+
then the condition is only evaluated at startup time. It's not evaluated at runtime for every request.
176+
:::
177+
178+
179+
133180
### Server Side Sessions Database Migrations
134181

135182
When using the server side sessions feature backed by the `Duende.BFF.EntityFramework` package, you will need to script [Entity Framework database migrations](/bff/fundamentals/session/server-side-sessions.mdx#entity-framework-migrations) and apply these changes to your database.

0 commit comments

Comments
 (0)