Define a collection of files using defineAkteFiles.
import { defineAkteFiles } from"akte";
constposts=defineAkteFiles().from({
path: "/posts/:slug",
data(context) { /* Optional, see data section */ },
bulkData() {
// Akte can't guess which paths you want to generate// for collection of files, so you need at least to// return a map of path to generate. More about// working with data below.return {
"/posts/foo": {},
"/posts/bar": {},
"/posts/baz": {},
};
},
render(context) {
return/* html */`<h1>${context.path}</h1>`;
},
});
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.
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.
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.
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.
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.
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.
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.
constwrapInLayout= (slot:string):string=> {
return/* html */`<!doctype html><html lang="en"> <head> <title>My App</title> </head> <body> <header>...</header> ${slot} <footer>...</footer> </body></html>`;
};
// Later in your rendering function...returnwrapInLayout(/* html */`...`);
Or make some "components" of your own.
constheader= (args: { displayLogo?:boolean; }):string=> {
return/* html */`<header> ${args.displayLogo?`<img src="/logo.svg" alt="logo" />`:""} <nav> <ul> <li><a href="/">Home</a></li> <li><a href="/posts">Blog</a></li> <li><a href="/about">About</a></li> </ul> </nav></header>`;
};
// Later in your rendering function...return/* html */`${header({ displayLogo: true })}...`;
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.
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.
import { app } from"./akte.app";
try {
constmatch=app.lookup("/foo");
constfile=awaitapp.render(match);
} catch (error) {
// Path is not handled...
}
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.
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.