Skip to content

Commit e0cdff0

Browse files
update static plugin docs (#194)
1 parent f632cc4 commit e0cdff0

File tree

1 file changed

+146
-15
lines changed

1 file changed

+146
-15
lines changed

docs/technical-details/reference/components/built-in-extensions.md

Lines changed: 146 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Harper provides extended features using built-in extensions. They do **not** nee
55
For more information read the [Components, Applications, and Extensions](../../../developers/applications/) documentation section.
66

77
- [Built-In Extensions](#built-in-extensions)
8+
- [dataLoader](#dataloader)
89
- [fastifyRoutes](#fastifyroutes)
910
- [graphql](#graphql)
1011
- [graphqlSchema](#graphqlschema)
@@ -13,6 +14,13 @@ For more information read the [Components, Applications, and Extensions](../../.
1314
- [rest](#rest)
1415
- [roles](#roles)
1516
- [static](#static)
17+
- [Options](#options)
18+
- [Examples](#examples)
19+
- [Basic Static File Serving](#basic-static-file-serving)
20+
- [Enable automatic `index.html` serving](#enable-automatic-indexhtml-serving)
21+
- [Enable automatic `.html` extension matching](#enable-automatic-html-extension-matching)
22+
- [Provide a custom `404 Not Found` page](#provide-a-custom-404-not-found-page)
23+
- [Fully customize not found response](#fully-customize-not-found-response)
1624

1725
## dataLoader
1826

@@ -150,35 +158,158 @@ roles:
150158

151159
## static
152160

153-
Specify files to serve statically from the Harper HTTP endpoint.
161+
Serve static files via HTTP.
154162

155-
Use the [Resource Extension](./extensions.md#resource-extension) configuration options [`files` and `urlPath`](./extensions.md#resource-extension-configuration) to specify the files to be served.
163+
The `static` plugin serves static files based on the `files` and `urlPath` [plugin configuration options](./plugins.md#configuration). It supports additional features via other options (detailed below), but the core to the plugin is to statically serve the files matched by the `files` pattern. The plugin will serve files relative to the longest non-ambiguous path within the pattern. The `urlPath` can be specified to customize the URL path that the files are served from, otherwise they match the file pattern. For example, given an application directory structure:
156164

157-
As specified by Harper's Resource Extension docs, the `files` option can be any glob pattern or a glob options object. This extension will serve all files matching the pattern, so make sure to be specific.
165+
```
166+
my-app/
167+
├─ site/
168+
│ ├─ index.html
169+
│ ├─ about.html
170+
│ ├─ blog/
171+
│ ├─ post-1.html
172+
│ ├─ post-2.html
173+
├─ config.yaml
174+
```
158175
159-
To serve the entire `web` directory, specify `files: 'web/**'`.
176+
The `static` plugin can be configured to serve the `site/` directory by specifying:
160177
161-
To serve only the html files within `web`, specify `files: 'web/*.html'` or `files: 'web/**/*.html'`.
178+
```yaml
179+
static:
180+
files: 'site/**'
181+
```
162182

163-
The `urlPath` option is the base URL path entries will be resolved to. For example, a `urlPath: 'static'` will serve all files resolved from `files` to the URL path `localhost/static/`.
183+
Then you could access the files relative to the `site` directory, thus `GET localhost:9926/index.html` would return the contents of `site/index.html`, and `GET localhost:9926/blog/post-1.html` would return the contents of `site/blog/post-1.html`.
164184

165-
Given the `config.yaml`:
185+
You can use the `urlPath` option to serve the files from a different URL path, for example:
166186

167187
```yaml
168188
static:
169-
files: 'web/*.html'
170-
urlPath: 'static'
189+
files: 'site/**'
190+
urlPath: 'app'
171191
```
172192
173-
And the file directory structure:
193+
Now, `GET localhost:9926/app/index.html` would return the contents of `site/index.html`, and `GET localhost:9926/app/blog/post-1.html` would return the contents of `site/blog/post-1.html`.
194+
195+
Moreover, if the `site/` directory was nested another level, such as:
174196

175197
```
176-
component/
177-
├─ web/
178-
│ ├─ index.html
179-
│ ├─ blog.html
198+
my-app/
199+
├─ site/
200+
│ ├─ pages/
201+
│ ├─ index.html
202+
│ ├─ about.html
203+
│ ├─ blog/
204+
│ ├─ post-1.html
205+
│ ├─ post-2.html
206+
│ ├─ cache-info/
207+
│ ├─ index.json
208+
│ ├─ about.json
209+
│ ├─ ...
180210
├─ config.yaml
211+
```
212+
213+
Now a pattern such as `site/pages/**` will match all files within the `pages` directory (including subdirectories) so a request to `GET localhost:9926/index.html` will return the contents of `site/pages/index.html`, and `GET localhost:9926/blog/post-1.html` will return the contents of `site/pages/blog/post-1.html`.
214+
215+
Because this plugin is implemented using the new [Plugin API](./plugins.md), it automatically updates to application changes. From updating the `config.yaml` to adding, removing, or modifying files, everything is handled automatically and Harper should **not** require a restart.
216+
217+
### Options
218+
219+
In addition to the general Plugin configuration options (`files`, `urlPath`, and `timeout`), this plugin supports the following configuration options:
220+
221+
- **extensions** - `string[]` - _optional_ - An array of file extensions to try and serve when an exact path is not found. For example, `['html']` and the path `/site/page-1` will match `/site/page-1.html`.
222+
- **fallthrough** - `boolean` - _optional_ - If `true`, the plugin will fall through to the next handler if the requested file is not found. Make sure to disable this option if you want to customize the 404 Not Found response with the `notFound` option. Defaults to `true`.
223+
- **index** - `boolean` - _optional_ - If `true`, the plugin will serve an `index.html` file if it exists in the directory specified by the `files` pattern. Defaults to `false`.
224+
- **notFound** - `string | { file: string; statusCode: number }` - _optional_ - Specify a custom file to be returned for 404 Not Found responses. If you want to specify a different statusCode when a given path cannot be found, use the object form and specify the `file` and `statusCode` properties (this is particularly useful for SPAs).
225+
226+
### Examples
227+
228+
The `static` plugin can be configured in various ways to provide different behaviors. Here are some common examples:
229+
230+
#### Basic Static File Serving
231+
232+
Serve all files contained within the `static/` directory as is.
233+
234+
```yaml
235+
static:
236+
files: 'static/**'
237+
```
238+
239+
Requests must match the file names exactly (relative to the `static/` directory).
240+
241+
#### Enable automatic `index.html` serving
242+
243+
Serve all files contained within the `static/` directory, and automatically serve an `index.html` file if it exists in the directory.
244+
245+
```yaml
246+
static:
247+
files: 'static/**'
248+
index: true
249+
```
250+
251+
Now given a directory structure like:
181252

182253
```
254+
my-app/
255+
├─ static/
256+
│ ├─ index.html
257+
│ ├─ blog/
258+
│ ├─ index.html
259+
│ ├─ post-1.html
260+
```
261+
262+
Requests would map like:
263+
264+
```
265+
GET / -> static/index.html
266+
GET /blog -> static/blog/index.html
267+
GET /blog/post-1.html -> static/blog/post-1.html
268+
```
269+
270+
#### Enable automatic `.html` extension matching
271+
272+
Expanding on the previous example, if you specify the `extensions` option, the plugin will automatically try to match the requested path with the specified extensions.
273+
274+
```yaml
275+
static:
276+
files: 'static/**'
277+
index: true
278+
extensions: ['html']
279+
```
280+
281+
Now with the same directory structure, requests would map like:
282+
283+
```
284+
GET / -> static/index.html
285+
GET /blog -> static/blog/index.html
286+
GET /blog/post-1 -> static/blog/post-1.html
287+
```
288+
289+
#### Provide a custom `404 Not Found` page
290+
291+
Sometimes when a `404 Not Found` response is not sufficient, and you want to provide a custom page or resource, you can use the `notFound` option to specify a custom file to be returned when a requested path is not found.
292+
293+
```yaml
294+
static:
295+
files: 'static/**'
296+
notFound: 'static/404.html'
297+
```
298+
299+
Now if a request is made to a path that does not exist, such as `/non-existent`, the plugin will return the contents of `static/404.html` with a `404` status code.
300+
301+
#### Fully customize not found response
302+
303+
Most common in SPAs relying on client-side routing, you may want to override the default `404` status code when a path is not found.
304+
305+
You can do this by specifying the `notFound` option as an object with a `file` and `statusCode` property.
306+
307+
```yaml
308+
static:
309+
files: 'static/**'
310+
notFound:
311+
file: 'static/index.html'
312+
statusCode: 200
313+
```
183314

184-
The HTML files will be available at `localhost/static/index.html` and `localhost/static/blog.html` respectively.
315+
Now if a request is made to a path that does not exist, such as `/non-existent`, the plugin will return the contents of `static/index.html` with a `200` status code. This is particularly useful for SPAs where you want to serve the main application file regardless of the requested path.

0 commit comments

Comments
 (0)