Change document.title
on the fly in Meteor.js apps via flow-router-extra API.
- 👨🔬 100% tests coverage
- ✨ Use prefixes for groups, including nesting
- 🎛 Per route, per group, and default (all routes)
title
tag
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]
meteor add ostrio:flow-router-title
import { FlowRouterTitle } from 'meteor/ostrio:flow-router-title';
flow-router-title
performs the best when used with the next packages:
- flow-router-meta - Per route
meta
tags,script
, andlink
(e.g. CSS) — set per-route meta-tags, stylesheets, and scripts - flow-router-extra - Carefully extended FlowRouter
new FlowRouterTitle(FlowRouter)
— The mainFlowRouterTitle
constructor that acceptsFlowRouter
as the only argumentFlowRouterTitle#set(title: string)
— The only method that changesdocument.title
during 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
Initialize FlowRouterTitle
class by passing FlowRouter
object. Right after creating all routes:
import { FlowRouter } from 'meteor/ostrio:flow-router-extra';
import { FlowRouterTitle } from 'meteor/ostrio:flow-router-title';
FlowRouter.route('/', {
action() { /* ... */ },
title: 'Title'
/* ... */
});
const titleHandler = new FlowRouterTitle(FlowRouter);
Set title
property in route's or group's configuration:
// Set default document.title value in
// case router has no title property
FlowRouter.globals.push({
title: 'Default title'
});
FlowRouter.route('/me/account', {
name: 'account',
title: 'My Account'
});
Set document.title
during runtime (without route(s)):
FlowRouter.route('/', {/* ... */});
const titleHandler = new FlowRouterTitle(FlowRouter);
titleHandler.set('My Awesome Title String'); // <- Returns `true`
// `.set()` method accepts {string} only
// Invalid values won't throw an exception
// instead it will simply return `false`
Use function context (with data
hook):
FlowRouter.route('/post/:_id', {
name: 'post',
waitOn(params) {
return [Meteor.subscribe('post', params._id)];
},
async data(params) {
return await Collections.Posts.findOneAsync(params._id);
},
title(params, query, data) {
if (data) {
return data.title;
}
return '404: Page not found';
}
});
Use group context:
const account = FlowRouter.group({
prefix: '/account',
title: 'Account',
titlePrefix: 'Account > '
});
account.route('/', {
name: 'accountIndex' // Title will be `Account`
});
account.route('/settings', {
name: 'AccountSettings',
title: 'My Settings' // Title will be `Account > My Settings`
});
To change title
reactively, just pass it as function:
FlowRouter.route('/me/account', {
name: 'account',
title() {
// In this example we used `ostrio:i18n` package
return i18n.get('account.document.title');
}
});
// Use params from route
FlowRouter.route('/page/:_id/:slug', {
name: 'somePage',
title(params) {
return `Page ${params.slug}`;
}
});
In all examples below title
can be a Function or String:
import { FlowRouter } from 'meteor/ostrio:flow-router-extra';
FlowRouter.globals.push({
title() {/* ... */} // <-- Suitable for reactive data source
});
FlowRouter.globals.push({
title: 'Title text'
});
FlowRouter.group({
title() {/* ... */}, // <-- Suitable for reactive data source
titlePrefix() {/* ... */} // <-- Can accept reactive data source, but won't trigger re-computation
});
FlowRouter.group({
title: 'Title text',
titlePrefix: 'Title prefix text'
});
FlowRouter.route('/path', {
title() {/* ... */} // <-- Reactive
});
FlowRouter.route('/path', {
title: 'Title text'
});
- Clone this package
- In Terminal (Console) go to directory where package is cloned
- Then run:
# 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
- 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