Installation
Quick Start
TypeScript Support
This package is written in TypeScript and provides comprehensive type definitions:Expected Remote Response Format
Your remote URL should return JSON in this format:Integration Methods
1. Template Integration (Recommended)
Use with any template engine (EJS, Handlebars, Pug, etc.):EJS Example
Handlebars Example
Pug Example
2. Auto Injection
Automatically inject scripts into HTML responses:3. Manual Injection
Programmatically inject scripts into HTML strings:4. Direct Access
Access script data directly for custom implementations:Configuration Options
Option | Type | Default | Description |
---|---|---|---|
remoteUrl | string | Required | URL to fetch script configuration from |
cacheInterval | number | 300 | Cache duration in seconds |
injectIntoHtml | boolean | true | Auto-inject scripts into HTML responses |
injectPosition | string | '</head>' | Where to inject scripts in HTML |
scriptAttributes | ScriptAttributes | {async: true} | Additional attributes for script tags |
onError | function | undefined | Custom error handler function |
shouldInject | function | () => true | Conditional injection logic |
Advanced Configuration
Route-Specific Configuration
Apply ServerTag to specific routes with different configurations:API Reference
serverTagMiddleware(options)
Main middleware function that adds ServerTag functionality to Express apps.
Parameters:
options
(ServerTagMiddlewareOptions): Configuration options
injectScripts(html, scripts, attributes, position)
Helper function to manually inject scripts into HTML strings.
Parameters:
html
(string): HTML string to modifyscripts
(string[]): Array of script URLsattributes
(ScriptAttributes): Script tag attributesposition
(string): Where to inject (default:'</head>'
)