Skip to content

Commit

Permalink
Document the callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
ComLock committed Oct 8, 2024
1 parent 90c2a52 commit 0b0bd21
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 4 deletions.
25 changes: 25 additions & 0 deletions docs/api/defaultCacheControl.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
= defaultCacheControl

defaultCacheControl is a function that returns a cache control header value.

It's used as a callback and is the default value for the `cacheControl` option in the <<api/requestHandler,requestHandler>> function.

When yielded inside the requestHandler it gets passed the found resource, it's content type and path.

== Type signature

See <<types#CacheControlResolver,CacheControlResolver>>

== Parameters

[%header,cols="1%,1%,1%,98%a"]
[frame="none"]
[grid="none"]
|===
| Name | Type | Description
| contentType | string | The content type of the resource
| path | string | The path to the resource
| resource | Resource | The found resource
|===

See the <<../examples/singlePageReactWebapp#,SPA>> example.
9 changes: 9 additions & 0 deletions docs/api/mappedRelativePath.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
= mappedRelativePath

mappedRelativePath is a function that takes a `base` path string and returns a <<types#RelativePathResolver,RelativePathResolver>> function.

The returned function will resolve relative paths to resources "hosted" under the based `base` path.

It can be imported and used as a callback for the `relativePath` option of the <<api/requestHandler,requestHandler>> function.

See the <<../examples/mapped#,mapped>> example.
8 changes: 4 additions & 4 deletions docs/api/requestHandler.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ This function handles requests for static resources.
[caption=""]
|===
| Name | Type | Default | Description
| cacheControl | CacheControlResolver | defaultCacheControl | Function that returns the cache control header value
| contentType | ContentTypeResolver | getMimeType | Function that returns the content type header value
| cacheControl | <<types#CacheControlResolver,CacheControlResolver>> | defaultCacheControl | Function that returns the cache control header value
| contentType | <<types#ContentTypeResolver,ContentTypeResolver>> | getMimeType | Function that returns the content type header value
| etag | boolean | true | Whether to include ETag header
| index | string | 'index.html' | Default file to serve when requesting a directory
| notFound | NotFoundHandler | notFoundResponse | Function that returns the response object when the resource is not found
| relativePath | RelativePathResolver | getRelativeResourcePath | Function that returns the relative path to the resource
| notFound | <<types#NotFoundHandler,NotFoundHandler>> | notFoundResponse | Function that returns the response object when the resource is not found
| relativePath | <<types#RelativePathResolver,RelativePathResolver>> | getRelativeResourcePath | Function that returns the relative path to the resource
| root | string | '/static' | Root path to serve files from
| staticCompress | boolean | true | Whether to serve precompressed files (*.br, *.gz) if available and supported by the browser
| throwErrors | boolean | true | Whether to throw errors or return a response object
Expand Down
17 changes: 17 additions & 0 deletions docs/api/spaNotFoundHandler.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
= spaNotFoundHandler

spaNotFoundHandler is a function that always returns a Response with the `index.html` Resource.

It can be imported and used as a callback for the `notFound` option of the <<api/requestHandler,requestHandler>> function.

Single page applications (SPA) typically consists of some static assets (stylesheets, fonts, images, etc...) and a single entrypoint file (e.g. `index.html`) that is responsible for routing and rendering the application.

Whenever a request doesn't match a static asset, lib-static would by default return a 404 Response.

This is not desireable for a SPA, because the request might be to a client-side routed page, and not a missing static asset.

Such requests can happen when someone reloads or opens a bookmarked url to a client-side routed page.

In addition to routing to client-side "pages", the SPA should render a beautiful 404 page, when no route matches.

See the <<../examples/singlePageReactWebapp#,SPA>> example.
3 changes: 3 additions & 0 deletions docs/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ const libStatic = require('/lib/enonic/static');
== API

* <<api/requestHandler#, requestHandler>>
* <<api/defaultCacheControl#, defaultCacheControl>>
* <<api/spaNotFoundHandler#, spaNotFoundHandler>>
* <<api/mappedRelativePath#, mappedRelativePath>>
* <<api/constants#, constants>>
* <<api/types#, types>>

Expand Down
12 changes: 12 additions & 0 deletions docs/menu.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
"title": "requestHandler",
"document": "api/requestHandler"
},
{
"title": "defaultCacheControl",
"document": "api/defaultCacheControl"
},
{
"title": "spaNotFoundHandler",
"document": "api/spaNotFoundHandler"
},
{
"title": "mappedRelativePath",
"document": "api/mappedRelativePath"
},
{
"title": "constants",
"document": "api/constants"
Expand Down

0 comments on commit 0b0bd21

Please sign in to comment.