Skip to main content

@rtorcato/cf-common

Interfaces​

ServeAssetsOptions​

Defined in: index.ts:8

Options for serveAssets.

Properties​

spa?​

optional spa?: string

Defined in: index.ts:15

Serve this path (usually '/', where index.html lives) for a page navigation that matched no asset, with status 200. Only GET/HEAD requests whose Accept header includes text/html fall back, so a missing /app.js still 404s.

notFound?​

optional notFound?: string

Defined in: index.ts:17

Serve this path's body with status 404 when nothing else matched.

Functions​

fetchAsset()​

fetchAsset(assets, request, path): Promise<Response>

Defined in: index.ts:29

Fetch the asset at path (resolved against the request URL) with the request's method and headers. Use it to map a route onto a different asset, e.g. /blog/<slug> onto /blog/post. A POST becomes a GET, since the binding only serves reads.

Example​

return fetchAsset(env.ASSETS, request, '/offline')

Parameters​

assets​

{ }

request​

Request

path​

string

Returns​

Promise<Response>


serveAssets()​

serveAssets(assets, request, options?): Promise<Response>

Defined in: index.ts:53

Serve request from the Static Assets binding. If no asset matched (404), fall back to spa for a page navigation, then to notFound as a 404 page; otherwise return the binding's response as is.

Paths are the URLs the assets are served at, so with the default html_handling that is '/' for index.html and '/404' for 404.html. If the whole site wants one rule, not_found_handling in wrangler config does this without code; this helper is for choosing per route.

Example​

if (url.pathname.startsWith('/app')) return serveAssets(env.ASSETS, request, { spa: '/app/' })
return serveAssets(env.ASSETS, request, { notFound: '/404' })

Parameters​

assets​

{ }

request​

Request

options?​

ServeAssetsOptions = {}

Returns​

Promise<Response>