### Install @podium/podlet
Source: https://github.com/podium-lib/podlet/blob/main/README.md
Installs the @podium/podlet package using npm. This is the primary method for adding the library to your project.
```bash
npm install @podium/podlet
```
--------------------------------
### Podlet Middleware Setup
Source: https://github.com/podium-lib/podlet/blob/main/README.md
Provides a Connect-compatible middleware for Podlet operations. It must be mounted before defining routes and wraps the `.process()` method.
```javascript
const app = express();
app.use(podlet.middleware());
```
--------------------------------
### Basic Podlet Server with Express
Source: https://github.com/podium-lib/podlet/blob/main/README.md
Demonstrates how to create a simple podlet server using Express. It includes setting up the Podlet instance, mounting its middleware, and defining routes for manifest and content.
```javascript
import express from 'express';
import Podlet, { html } from '@podium/podlet';
// create a new podlet instance
const podlet = new Podlet({
name: 'myPodlet',
version: '1.3.1',
pathname: '/',
development: true,
});
// create a new express app instance
const app = express();
// mount podlet middleware in express app
app.use(podlet.middleware());
// create a route to serve the podlet's manifest file
app.get(podlet.manifest(), (req, res) => {
res.json(podlet);
});
// create a route to serve the podlet's content
app.get(podlet.content(), (req, res) => {
res.podiumSend(html`
hello world
`);
});
// start the app on port 7100
app.listen(7100);
```
--------------------------------
### Podlet Constructor and Options
Source: https://github.com/podium-lib/podlet/blob/main/README.md
Details the constructor for creating a new Podlet instance and the available configuration options. Each option is described with its type, default value, and whether it's required.
```APIDOC
Podlet(options)
Creates a new podlet instance.
Options:
- name (string, required): The name the podlet identifies itself by. Must comply with custom element naming rules if useShadowDOM is true.
- version (string, required): The current version of the podlet. Used by layouts to determine if a refresh is needed.
- pathname (string, required): Pathname for where a Podlet is mounted in an HTTP server. Must match the server mount point.
- manifest (string, optional, default: '/manifest.json'): The path for the manifest file.
- content (string, optional, default: '/'): The path for the podlet's content.
- fallback (string, optional): The path for the podlet's fallback content.
- logger (object, optional): A logger instance.
- development (boolean, optional, default: false): Enables development mode features.
- useShadowDOM (boolean, optional, default: false): Enables Shadow DOM encapsulation for the podlet.
Example (name):
const podlet = new Podlet({ name: 'myPodlet' });
Example (version):
const podlet = new Podlet({ version: '1.1.0' });
Example (pathname):
// Mounted at root
const podletRoot = new Podlet({ name: 'rootPodlet', version: '1.0.0', pathname: '/' });
// Mounted at /foo
const podletFoo = new Podlet({ name: 'fooPodlet', version: '1.0.0', pathname: '/foo' });
Description for 'name' option:
The name the podlet identifies itself by. This value can contain upper and lower case letters, numbers, the - character and the _ character. No spaces. When shadow DOM is used, either via the `useShadowDOM` constructor option or via the `wrapWithShadowDOM` method, the name must comply with custom element naming rules. See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/CustomElementRegistry/define#valid_custom_element_names) for more information.
Description for 'version' option:
The current version of the podlet. It is important that this value be updated when a new version of the podlet is deployed since the page (layout) that the podlet is displayed in uses this value to know whether to refresh the podlet's manifest and fallback content or not.
Description for 'pathname' option:
Pathname for where a Podlet is mounted in an HTTP server. It is important that this value matches where the entry point of a route is in an HTTP server since this value is used to define where the manifest is for the podlet. If the podlet is mounted at the "root", set `pathname` to `/`. If the podlet is to be mounted at `/foo`, set pathname to `/foo` and mount middleware and routes at or under `/foo`.
```
--------------------------------
### Enable Podlet Development Mode
Source: https://github.com/podium-lib/podlet/blob/main/README.md
Configures a Podlet instance to enable development mode, typically based on the environment. This mode provides default contexts and encapsulating HTML for easier local testing.
```javascript
const podlet = new Podlet({
development: process.env.NODE_ENV !== 'production'
});
```
--------------------------------
### Configure Podlet CSS Assets
Source: https://github.com/podium-lib/podlet/blob/main/README.md
Sets the options for a podlet's CSS assets. This information is serialized into a manifest and can be sent as an HTTP 103 early hint to aid layout clients in fetching assets before the main body.
```APIDOC
Podlet.css(options)
Sets the options for a podlet's CSS assets. Options can be an object or an array of objects.
Parameters:
options: (Object | Array