Reactive page title
Change document.title on the fly in Meteor.js apps via flow-router-extra API.
ToC
Features
- 👨🔬 100% tests coverage
- ✨ Use prefixes for groups, including nesting
- 🎛 Per route, per group, and default (all routes)
titletag
Various ways to set title, ordered by priority:
FlowRouter.route()[overrides all below]FlowRouter.group()FlowRouter.globals- Head template
<title>Text</title>tag [superseded by any above]
Installation
meteor add ostrio:flow-router-title
Compatibility
- Meteor
>=1.4, including latest Meteor3.4; - Requires
ostrio:flow-router-extra@3.14.0+.
Demos
ES6 Import
1import { FlowRouterTitle } from 'meteor/ostrio:flow-router-title';
AGENTS.md
The repo ships AGENTS.md: a compact implementation map for ostrio:flow-router-title. It complements API in this document.
SKILLS.md
- The main
ostrio:flow-router-extrapackage ships a bundled skill at.agents/skills/meteor-flow-router/SKILL.md(coversostrio:flow-router-extra,ostrio:flow-router-meta,ostrio:flow-router-title). Install into your project with the Skills CLI (npx skills):
# From a Meteor app repo (install into that app’s .agents/skills for Cursor, etc.) npx skills add veliovgroup/flow-router --skill meteor-flow-router # Only list skills discovered in the Flow Router repo (no install) npx skills add veliovgroup/flow-router --list # User-global Cursor skills dir (~/.cursor/skills) npx skills add veliovgroup/flow-router --skill meteor-flow-router --agent cursor --global --yes
TypeScript
- Package ships
index.d.tsas a Meteor asset. Apps usingzodern:typesresolve typings formeteor/ostrio:flow-router-titleautomatically. - Import only from client code (same as this package’s
mainModule). - Constructor expects
FlowRouterfrommeteor/ostrio:flow-router-extra(Routertype). Route / group options usetitle,titlePrefixas in the API section.
1import { FlowRouter } from 'meteor/ostrio:flow-router-extra'; 2import { FlowRouterTitle } from 'meteor/ostrio:flow-router-title'; 3 4const titles = new FlowRouterTitle(FlowRouter); 5titles.set('My title');
Related Packages
flow-router-title performs the best when used with the next packages:
- flow-router-meta - Per route
metatags,script, andlink(e.g. CSS) — set per-route meta-tags, stylesheets, and scripts - flow-router-extra - Carefully extended FlowRouter
API
new FlowRouterTitle(FlowRouter)— The mainFlowRouterTitleconstructor that acceptsFlowRouteras the only argumentFlowRouterTitle#set(title: string)— The only method that changesdocument.titleduring runtime
After new FlowRouterTitle(FlowRouter) instance is initiated it extends FlowRouter.router() and FlowRouter.group() methods and FlowRouter.globals with support of:
title: string— String propertytitle: function(params, qs, data) => string— Method returning stringtitlePrefix: string— String property for page's title prefixtitlePrefix: function(params, qs, data) => string— Method returning string for page's title prefix
Usage
Initialize FlowRouterTitle class by passing FlowRouter object. Right after creating all routes:
1import { FlowRouter } from 'meteor/ostrio:flow-router-extra'; 2import { FlowRouterTitle } from 'meteor/ostrio:flow-router-title'; 3 4FlowRouter.route('/', { 5 action() { /* ... */ }, 6 title: 'Title' 7 /* ... */ 8}); 9 10const titleHandler = new FlowRouterTitle(FlowRouter);
404 / notFound compatibility
ostrio:flow-router-title supports both 404 styles in ostrio:flow-router-extra:
- Recommended: catch-all route via
FlowRouter.route('*', { title, action }) - Legacy/deprecated API:
FlowRouter.notFound = { title, action }
When both are present, latest registration wins, same as regular route registration order.
Set title via Route
Set title property in route's or group's configuration:
1// Set default document.title value in 2// case router has no title property 3FlowRouter.globals.push({ 4 title: 'Default title' 5}); 6 7FlowRouter.route('/me/account', { 8 name: 'account', 9 title: 'My Account' 10});
.set() method
Set document.title during runtime (without route(s)):
1FlowRouter.route('/', {/* ... */}); 2 3const titleHandler = new FlowRouterTitle(FlowRouter); 4titleHandler.set('My Awesome Title String'); // <- Returns `true` 5 6// `.set()` method accepts {string} only 7// Invalid values won't throw an exception 8// instead it will simply return `false`
Function context
Use function context (with data hook):
1FlowRouter.route('/post/:_id', { 2 name: 'post', 3 waitOn(params) { 4 return [Meteor.subscribe('post', params._id)]; 5 }, 6 async data(params) { 7 return await Collections.Posts.findOneAsync(params._id); 8 }, 9 title(params, query, data) { 10 if (data) { 11 return data.title; 12 } 13 return '404: Page not found'; 14 } 15});
Group context
Use group context:
1const account = FlowRouter.group({ 2 prefix: '/account', 3 title: 'Account', 4 titlePrefix: 'Account > ' 5}); 6 7account.route('/', { 8 name: 'accountIndex' // Title will be `Account` 9}); 10 11account.route('/settings', { 12 name: 'AccountSettings', 13 title: 'My Settings' // Title will be `Account > My Settings` 14});
Reactive data sources
To change title reactively, just pass it as function:
1FlowRouter.route('/me/account', { 2 name: 'account', 3 title() { 4 // In this example we used `ostrio:i18n` package 5 return i18n.get('account.document.title'); 6 } 7}); 8 9// Use params from route 10FlowRouter.route('/page/:_id/:slug', { 11 name: 'somePage', 12 title(params) { 13 return `Page ${params.slug}`; 14 } 15});
More examples
In all examples below title can be a Function or String:
1import { FlowRouter } from 'meteor/ostrio:flow-router-extra'; 2 3FlowRouter.globals.push({ 4 title() {/* ... */} // <-- Suitable for reactive data source 5}); 6 7FlowRouter.globals.push({ 8 title: 'Title text' 9}); 10 11FlowRouter.group({ 12 title() {/* ... */}, // <-- Suitable for reactive data source 13 titlePrefix() {/* ... */} // <-- Can accept reactive data source, but won't trigger re-computation 14}); 15 16FlowRouter.group({ 17 title: 'Title text', 18 titlePrefix: 'Title prefix text' 19}); 20 21FlowRouter.route('/path', { 22 title() {/* ... */} // <-- Reactive 23}); 24 25FlowRouter.route('/path', { 26 title: 'Title text' 27});
Running Tests
- Clone this package
- In Terminal (Console) go to directory where package is cloned
- Then run:
Meteor/Tinytest
# Default meteor test-packages ./ # With custom port meteor test-packages ./ --port 8888 # With local MongoDB and custom port MONGO_URL="mongodb://127.0.0.1:27017/flow-router-title-tests" meteor test-packages ./ --port 8888
Support this project
- Upload and share files using ☄️ meteor-files.com — Continue interrupted file uploads without losing any progress. There is nothing that will stop Meteor from delivering your file to the desired destination
- Use ▲ ostr.io for Server Monitoring, Web Analytics, WebSec, Web-CRON and SEO Pre-rendering of a website
- Star on GitHub
- Star on Atmosphere
- Sponsor via GitHub
- Support via PayPal