Typing path parameters
You can type path parameters on your Akte files when defining them. This will enforce correct path
value and type the params
object of the data
function.
How to use Akte to achieve what you want.
Files are the central part of Akte. You can define two kinds of files.
/about
or /sitemap.xml
/posts/:slug
or even catch-all routesIn any case, for files to be taken into account by your app, you need to register them in your akte.app.ts
file.
Define a single file using defineAkteFile
.
Define a collection of files using defineAkteFiles
.
Akte uses the path
property of your file definitions to handle routing and define which file to generate. Route patterns and matching algorithm are powered by radix3
.
Static routes are meant to be used with single files.
path value | Route matched | File generated |
---|---|---|
/ | / | /index.html |
/foo | /foo | /foo.html |
/bar/ | /bar/ | /bar/index.html |
/baz.json | /baz.json | /baz.json |
Dynamic routes are meant to be used with collection of files.
path value | Example route matched | Example file generated |
---|---|---|
/:id | /foo | /foo.html |
/:cat/:id | /baz/baz | /bar/baz.html |
/posts/:slug | /posts/qux | /posts/qux.html |
/api/:id.json | /api/quux.json | /api/quux.json |
You can type path parameters on your Akte files when defining them. This will enforce correct path
value and type the params
object of the data
function.
Catch-all routes are also available for collection of files.
path value | Example route matched | Example file generated |
---|---|---|
/** | /foo/bar/baz | /foo/bar/baz.html |
/pages/** | /pages/qux/quux | /pages/qux/quux.html |
/api/**.json | /api/corge/grault.json | /api/corge/grault.json |
Catch-all routes will match the root of the catch-all route, e.g. /**
also matches /
and /pages/**
also matches /pages
In some cases, you can end up with multiple files matching the same route (e.g. /
and /**
both match /
). When this happens, the priority is given to the file registered last within your Akte app configuration.
Akte works with two kinds of data:
Global data are defined on your Akte app. It's a function or an asynchronous function that returns them.
Bulk data defines all the data needed to render all files defined with defineAkteFiles
. It's a function or an asynchronous function that returns a map of path and file data.
While the bulkData
function is at least necessary to define which files to render, it also exists to optimize build time. Most APIs allow you to query content in bulk, by doing so, less requests are made and build can be faster.
bulkData
functions all receive the same context
argument, it contains:
globalData
: application global dataUse it as needed to compute your bulk data appropriately.
Data defines the data needed to render a file at a given path defined with defineAkteFile
. It's a function or an asynchronous function that returns the file data.
The data
function can optionally be provided for files defined with defineAkteFiles
(by default it is inferred from the files bulkData
function). This allows to optimize the file rendering process during single renders, e.g. rendering on serverless environment.
data
functions all receive the same context
argument, it contains:
path
: the path of the rendered fileparams
: path parameters for collection of filesglobalData
: application global dataUse it as needed to compute your bulk data appropriately.
Akte uses your file render
functions to render them as strings. These functions can be asynchronous, however, it's recommended to keep async behaviors within data
and bulkData
.
render
functions all receive the same context
argument, it contains:
path
: the path of the rendered fileglobalData
: application global datadata
: data for this file, type is inferred from data
or bulkData
Use it to render each of your files appropriately.
Akte templating is string-based. While this doesn't prevent you from pulling in a templating engine of your liking, ES6 template literals can get you quite far with à-la-JSX syntax.
You can get HTML highlighting, autocompletion, IntelliSense, and more for template literals by leveraging extensions like es6-string-html
(that's why you've been seeing those /* html */
comments)
Because rendering is all JavaScript based, you can create your own functions to make templating easier. For example, you can wrap your output within a layout like so.
Or make some "components" of your own.
Your Akte app can be imported and used programmatically. To do so, it exposes various methods you can use to achieve different goals, including serverless usage.
You can know if a path is handled by your Akte app using the lookup
method.
Pairing lookup
with the render
method allows you to render a path. The render
method resolves global data and the file's data, caches them, then runs your file render
function for the request path.
You can render all your app files with the renderAll
method.
You can write a map of rendered files with the writeAll
method.
All files will, by default, be written to the app build.outDir
directory which defaults to dist
. When calling writeAll
you can specify another directory.
For convenience, the buildAll
method is available. It renders and writes all files, then returns an array of files written.
Akte caches all globalData
, bulkData
, data
calls for performance. The clearCache
method allows you to clear these caches.
Akte reports on what it's doing using the debug
package. Track performances and debug your app by setting the DEBUG
environment variable before running Akte.