# @midwayjs/static-file

> midway static file component

Latest version **4.2.3** (published 2026-09-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install @midwayjs/static-file
pnpm add @midwayjs/static-file
yarn add @midwayjs/static-file
bun add @midwayjs/static-file
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 4.2.3 |
| Published | 2026-09-06 |
| First published | 2022-01-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=20 |
| Dependencies | 3 |
| Unpacked size | 16 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 7742 |
| Author | czy88840616 |
| Maintainers | czy88840616, legendecas, echosoar, lellansin, lxxyx, stone-jin, mariodu |
| Keywords | midway, koa-static, assets, static |

## Links

- npm: https://www.npmjs.com/package/@midwayjs/static-file
- Repository: https://github.com/midwayjs/midway
- Homepage: https://github.com/midwayjs/midway#readme
- Issues: https://github.com/midwayjs/midway/issues
- npm.io page: https://npm.io/package/@midwayjs/static-file

## Dependencies (3)

- [ylru](https://npm.io/package/ylru.md) 1.4.0
- [koa-range](https://npm.io/package/koa-range.md) 0.3.0
- [koa-static-cache](https://npm.io/package/koa-static-cache.md) 5.1.4

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 4.2.3 (latest) — 2026-09-06
- 4.0.1-beta.1 (beta) — 2026-04-02
- 4.0.0-alpha.1 (alpha) — 2024-12-15
- 4.2.2 — 2026-08-26
- 4.2.1 — 2026-06-14
- 4.1.0 — 2026-05-03
- 3.20.24 — 2026-04-25
- 4.0.3 — 2026-04-12
- 3.20.23 — 2026-04-09
- 4.0.2 — 2026-04-06
- 4.0.1 — 2026-04-05
- 4.0.0 — 2026-03-26
- 4.0.0-beta.17 — 2026-03-22
- 4.0.0-beta.16 — 2026-03-08
- 4.0.0-beta.15 — 2026-02-24
- … 169 more at https://npm.io/package/@midwayjs/static-file/versions

## README

# Midway Static File Component

Static server plugin for egg, base on [koa-static-cache](https://github.com/koajs/static-cache).

## Install

```bash
$ npm i @midwayjs/static-file --save
```

available in koa/egg/faas.

## Usage

import info component in `configuration.ts` file

```ts
import * as koa from '@midwayjs/koa';
import * as staticFile from '@midwayjs/static-file';
import { join } from 'path';

@Configuration({
  imports: [
    koa,
    staticFile,
  ],
  importConfigs: [
    join(__dirname, './config')
  ]
})
export class ContainerConfiguration {
}

```

support all configurations in [koa-static-cache](https://github.com/koajs/static-cache). and with default configurations below:

- dynamic: `true`
- preload: `false`
- maxAge: `31536000` in prod env, `0` in other envs
- buffer: `true` in prod env, `false` in other envs

provides one more option:

- maxFiles: the maximum value of cache items, only effective when dynamic is true, default is `1000`.

**All static files in `$appDir/public` can be visited with prefix `/public`, and all the files are lazy loaded.**

- In non-production environment, assets won't be cached, your modification can take effect immediately.
- In production environment, `@midwayjs/static-file` will cache the assets after visited, you need to restart the process to update the assets.
- Dir default is `$appDir/public` but you can also define **multiple directory** by default, static server will use all these directories.

```js
// {app_root}/src/config/config.default.ts
export const staticFile = {
  dirs: {
    default: {
      prefix: '/public',
      dir: 'xxx'
    },
    antoherDir: {
      prefix: '/',
      dir: 'xxx'
    }
  }
};
```

if you want to override default prefix, just set `dirs.default`.

```js
// {app_root}/src/config/config.default.ts
export const staticFile = {
  dirs: {
    default: {
      prefix: '/',
    },
  }
};
```


## License

[MIT](https://github.com/midwayjs/midway/blob/master/LICENSE)

---
_Source: https://npm.io/package/@midwayjs/static-file · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
