Skip to content

Commit 1d005a0

Browse files
committed
feat(docs): add static asset mounting section for Express and H3 runtimes
1 parent c69ff0b commit 1d005a0

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

docs/guide/express-runtime.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,43 @@ await app.boot(3000);
8888
await app.shutdown();
8989
```
9090

91+
## Static Assets
92+
93+
Express mounts the `public` directory automatically during `app.boot(port)` through the runtime driver.
94+
95+
Default behavior:
96+
97+
- serves files from `path.join(process.cwd(), 'public')`
98+
- applies `Cache-Control: public, max-age=31536000, immutable`
99+
- adds permissive CORS headers for static responses
100+
101+
If you need different static asset behavior, override `mountPublicAssets` in `src/core/app.ts` when constructing `ExpressDriver`.
102+
103+
```ts
104+
import express from 'express';
105+
import path from 'path';
106+
import { ExpressDriver } from '@arkstack/driver-express';
107+
108+
this.driver = new ExpressDriver({
109+
bindRouter: async (runtime) => {
110+
runtime.use(await Router.bind());
111+
},
112+
mountPublicAssets: (runtime, publicPath) => {
113+
runtime.use(
114+
'/assets',
115+
express.static(path.resolve(publicPath), {
116+
maxAge: '7d',
117+
}),
118+
);
119+
},
120+
errorHandler: ErrorHandler,
121+
});
122+
```
123+
91124
## Notes
92125

93126
- `app.boot(port)` mounts public assets, binds router, applies middleware, registers error handling, starts the server, and attaches graceful shutdown.
127+
- Static asset mounting happens before configured middleware is applied.
94128
- For middleware layering and recommended usage, see [Middleware Guide](/guide/middleware).
95129
- Use the router contract (`getRouter`) for framework-agnostic behavior where possible.
96130
- Prefer `expressApp` only when you specifically need native Express APIs.

docs/guide/h3-runtime.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,38 @@ await app.boot(3000);
9090
await app.shutdown();
9191
```
9292

93+
## Static Assets
94+
95+
H3 mounts the `public` directory automatically during `app.boot(port)` through `H3Driver`.
96+
97+
Default behavior:
98+
99+
- serves files from `process.cwd()/public`
100+
- only handles requests that look like asset paths
101+
- blocks dotfiles and path traversal attempts
102+
- applies long-lived cache headers and permissive CORS headers
103+
104+
If you need to change that behavior, override `mountPublicAssets` in `src/core/app.ts` when constructing `H3Driver`.
105+
106+
```ts
107+
import { H3Driver } from '@arkstack/driver-h3';
108+
import { staticAssetHandler } from '@arkstack/driver-h3/middlewares';
109+
110+
this.driver = new H3Driver({
111+
createApp: () => new H3({ onError: ErrorHandler }),
112+
bindRouter: async (runtime) => {
113+
await Router.bind(runtime);
114+
},
115+
mountPublicAssets: (runtime, publicPath) => {
116+
runtime.use(staticAssetHandler(publicPath));
117+
},
118+
});
119+
```
120+
93121
## Notes
94122

95123
- `app.boot(port)` mounts public assets, binds router, applies middleware, starts the server, and attaches graceful shutdown.
124+
- Static asset mounting happens before configured middleware is applied.
96125
- For middleware layering and recommended usage, see [Middleware Guide](/guide/middleware).
97126
- Use the router contract (`getRouter`) for framework-agnostic behavior where possible.
98127
- Prefer `h3App` only when you specifically need native H3 APIs.

docs/guide/middleware.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,45 @@ Arkstack applies middleware in this order during `app.boot(port)`:
2727
5. `after` middleware runs.
2828
6. Runtime-specific startup continues (Express error handler registration, then server start).
2929

30+
## Static asset mounting
31+
32+
Static assets are mounted by the runtime driver before configured middleware runs.
33+
34+
Default locations:
35+
36+
- Express: `path.join(process.cwd(), 'public')`
37+
- H3: `process.cwd()/public`
38+
39+
Use this default behavior when your app only needs a conventional `public` directory. If you need a different mount path, cache policy, or asset strategy, override `mountPublicAssets` when constructing the driver in `src/core/app.ts`.
40+
41+
Express example:
42+
43+
```ts
44+
this.driver = new ExpressDriver({
45+
bindRouter: async (runtime) => {
46+
runtime.use(await Router.bind());
47+
},
48+
mountPublicAssets: (runtime, publicPath) => {
49+
runtime.use('/assets', express.static(publicPath));
50+
},
51+
errorHandler: ErrorHandler,
52+
});
53+
```
54+
55+
H3 example:
56+
57+
```ts
58+
this.driver = new H3Driver({
59+
createApp: () => new H3({ onError: ErrorHandler }),
60+
bindRouter: async (runtime) => {
61+
await Router.bind(runtime);
62+
},
63+
mountPublicAssets: (runtime, publicPath) => {
64+
runtime.use(staticAssetHandler(publicPath));
65+
},
66+
});
67+
```
68+
3069
Use each group with this intent:
3170

3271
- `global`: middleware that should always run for every request (body parsing, CORS, method override, baseline security headers).

0 commit comments

Comments
 (0)