serve-static.ts
This file
Path
adapters/cloudflare-workers/src/serve-static.ts
42 lines · 1.5 KiB · TypeScript
Last changed by
feat(adapters): add the seven adapters as workspace packages (#5463)Yusuke Wadabefore adoptionb877827
History of this fileTake it
feat(adapters): add the seven adapters as workspace packages (#5463)Yusuke Wadabefore adoptionb877827
lines 1–42why, one step back1import { serveStatic as baseServeStatic } from 'hono/serve-static'2import type { ServeStaticOptions as BaseServeStaticOptions } from 'hono/serve-static'3import type { Env, MiddlewareHandler } from 'hono/types'4import { getContentFromKVAsset } from './utils'56export type ServeStaticOptions<E extends Env = Env> = BaseServeStaticOptions<E> & {7 // namespace is KVNamespace8 namespace?: unknown9 manifest?: object | string10}1112/**13 * @deprecated14 * `serveStatic` in the Cloudflare Workers adapter is deprecated.15 * You can serve static files directly using Cloudflare Static Assets.16 * @see https://developers.cloudflare.com/workers/static-assets/17 * Cloudflare Static Assets is currently in open beta. If this doesn't work for you,18 * please consider using Cloudflare Pages. You can start to create the Cloudflare Pages19 * application with the `npm create hono@latest` command.20 */21export const serveStatic = <E extends Env = Env>(22 options: ServeStaticOptions<E> = {}23): MiddlewareHandler => {24 return async function serveStatic(c, next) {25 const getContent = async (path: string) => {26 return getContentFromKVAsset(path, {27 manifest: options.manifest,28 // eslint-disable-next-line @typescript-eslint/ban-ts-comment29 // @ts-ignore30 namespace: options.namespace31 ? options.namespace32 : c.env33 ? c.env.__STATIC_CONTENT34 : undefined,35 })36 }37 return baseServeStatic({38 ...options,39 getContent,40 })(c, next)41 }42}