Files
sweet/docs/latest/concepts/static-files.md
T
Christian Svensson a2de5154ec fix(docs): improve and increase consistency for code blocks (#3163)
Follow-up from #3162.

This PR does various tweaks, mostly related to code blocks.

- Adds missing titles to code blocks, and updates titles to be more
consistent
- Adds filenames to most ` ```tsx` blocks with e.g. `main.ts`, `dev.ts`
or a suitable example (e.g. `routes/greeting.tsx`) based on the code
- Adds filenames to ` ```diff` blocks if there's a relevant file, to
make the icon e.g. TS over Diff icon
  - Code blocks with Fresh code (e.g. interfaces) uses ` ```tsx fresh 🍋`
- Changes most ` ```sh` blocks with example terminal commands to ` ```sh
Terminal`
- Updates ` ```txt Project Structure` or ` ```diff Project Structure`
blocks to be more consistent
- Changes `sh` or `txt` to `txt-files` which improves highlighting and
makes icon consistent
- Renames `<project dir>` to `<project root>` to match builder `root`
option terminology
- Adds `<project root>` root folder to most/all overview folder
overviews
  - Makes the structure blocks more consistently displayed
- Tried to make some "bare" examples more complete

## Comparison

Here's a small curated set of before/after changes.

### Deployment

- `deno` highlighted in terminal commands
- `.gitignore` syntax highlighting works without using `sh` as language

<img width="1641" height="1009" alt="image"
src="https://github.com/user-attachments/assets/170cf833-1f6a-4b0d-8d2d-a081bc28a967"
/>

### File routing

Updated icon, comments same as before, but root and hierarchy have new
colors (`operator`/`function`).

<img width="1647" height="588" alt="image"
src="https://github.com/user-attachments/assets/f28db51b-4bee-45e4-be1c-e137a6d3ae3d"
/>
2025-08-01 09:07:42 +02:00

2.0 KiB

description
description
Fresh has built-in support for serving static files. This is useful for serving images, CSS, and other static assets.

Fresh automatically serves static assets placed in a static/ directory in the project root. These assets are served at the root of the webserver, with a higher priority than routes. This means that if a given request matches a file in the static/ folder, it is always served, even if there is a route that would also match the request.

Static asset responses automatically get a content-type header assigned based on the file extension of the file on disk. Assets are also automatically streamed from disk to the client to improve performance and efficiency for both user and server.

Fresh also adds an etag header to assets automatically and handles the If-None-Match header for incoming requests.

Caching

By default, no caching headers are added to assets. This can be disadvantageous in many scenarios, so Fresh makes it easy to serve assets with long cache lifetimes too.

The first approach to do this is manual. The client runtime exports an asset function that takes an absolute path to the static asset and returns a "locked" version of this path that contains a build ID for cache busting. When the asset is requested at this "locked" path, it will be served with a cache lifetime of one year.

import { asset } from "$fresh/runtime.ts";

export default function Page() {
  return (
    <p>
      <a href={asset("/brochure.pdf")}>View brochure</a>
    </p>
  );
}

Fresh also does this automatically for src and srcset attributes in <img> and <source> HTML tags. These will automatically use "locked" paths if Fresh deems it safe to do so. You can always opt out of this behaviour per tag, by adding the data-fresh-disable-lock attribute.

{/* Locked URL source */}
<img src="/user.png" />;

{/* Preserve URL source and disable lock */}
<img src="/user.png" data-fresh-disable-lock />;