@curi/static
v2.0.2
GitHub RepoNPM PackageAbout
The @curi/static
package is for creating static assets for your server. Its exported functions should be used in a build script, not the source of an application.
Installation
API
staticFiles
The staticFiles
function is used to generate HTML files for a static website and save them to the disk. An HTML file will be created for each page that you provide.
Files will be created as index.html
in the directory for their path. For example, the page for the pathname /about
would create an /about/index.html
file.
staticFiles
returns a Promise that resolve with an array of results. Each entry in the results array contains the pathname
of the result, a success
boolean, and, if success
is false, the error
that occurred.
It can be useful to log the results to easily see what pages were successfully built and which had issues.
Arguments
The staticFiles
functions is passed an object of arguments.
pages
An array of page descriptors. A page descriptor is an object with a name
property defining which route to generate a page for. If the route (or any of its ancestors) has any params, they should be passed as an object with the params
property.
router
The router property is an object with two properties: routes
and options
.
routes
routes
is an array of route descriptors. This is the same array that you would use to create a router in client-side code.
options
The options
object is the options for a router. This is only necessary if you need to use side effects or other route options.
When you call staticFiles
, a router is created for each page. staticFiles
creates its own history
instances, and gets its routes from the routes
options, but the router may also need to be provided with other options.
fallback
Some hosts allow you to provide a fallback page for when a request doesn't match any of your generated HTML files. The fallback
option takes the pathname
it should generate page contents for and the filename
to save the file as.
output
The output
property is used to describe how output files are generated and where they are stored.
render
A function that takes the emitted response
, navigation
, and router
for a location and returns the page's HTML.
How the content is generated will depend on what type of application is being built. For a React application, this would call the renderToString
method from react-dom/server
. A Vue application would use vue-server-renderer
.
render
is expected to return the complete HTML for a page
If the response
should not be rendered, the render
function should throw an error.
dir
The folder where the generated HTML files should be saved.
pathnames
The pathnames
function is used to generate pathnames from an array of provided page descriptors. This can be useful for generating a sitemap.
Arguments
routes
The array of route descriptors that is passed to a router.
pages
An array of page descriptors. A page descriptor is an object with a name
property defining which route to generate a page for. If the route (or any of its ancestors) has any params, they should be passed as an object with the params
property.
routerOptions
The options for a router, predominantly useful for passing any route interactions the application may need while rendering.