Dynamically update meta tags and document title for SEO purposes in your AngularJS application.
- lightweight (< 1KB)
- uses original meta syntax
- supports prerender.io for SEO purposes
- supports Open Graph protocol meta elements
- supports schema.org protocol meta elements
- update your document title dynamically
- update your meta tags depending on the state your application is in
- no additional scripting required, works out-of-the-box!
First install the module using bower:
$ bower install angular-update-meta
then add the updateMeta
module to the dependencies of your AngularJS application module:
angular.module('yourApp', ['updateMeta']);
Suppose you have the following markup in your template:
<html>
<head>
<title>Website title</title>
<meta charset="utf-8" />
<meta http-equiv="Content-Language" content="en" />
<meta name="description" content="Application wide description" />
<meta property="og:title" content="The Rock" />
<meta itemprop="description" content="Application wide description for Schema.org (Google+ uses this)">
</head>
<body>
...
</body>
</html>
Now you can use the following markup in your view(s):
<update-title title="A new title"></update-title>
<update-meta charset="ISO-8859-1"></update-meta>
<update-meta http-equiv="Content-Language" content="es"></update-meta>
<update-meta name="description" content="A page specific description"></update-meta>
<update-meta property="og:title" content="Minions"></update-meta>
<update-meta itemprop="description" content="A page specific itemprop description"></update-meta>
So the head is updated to:
<html>
<head>
<title>A new title</title>
<meta charset="ISO-8859-1" />
<meta http-equiv="Content-Language" content="es" />
<meta name="description" content="A page specific description" />
<meta property="og:title" content="Minions" />
<meta itemprop="description" content="A page specific itemprop description">
</head>
<body>
...
</body>
</html>
Only existing meta elements are updated.
If the meta element does not exist yet, it is NOT added, so make sure they exist in your original head
element.
Whenever an update-meta
element is processed, the original meta
in the head is updated with the new value.
This allows you to dynamically set the meta
element values with values from within your markup and child states.
Dynamic AngularJS expressions are supported too:
<update-meta property="og:title" content="{{ title }}"></update-meta>
In the example above, the og:title
is automatically updated whenever title
changes.
Prerender.io will grab the updated values and store them in your page snapshots so they are optimized for SEO purposes.
This allows you to conveniently update the meta
elements for each individual page in your AngularJS single page application and store them correctly in your Prerender page snapshots.
You can preview the prerender output by using the _escaped_fragment_=
parameter as described in the prerender.io documentation.
To update the build in the dist
directory:
$ gulp
To run the unit tests (for both concatenated and minified version):
$ gulp test
- added support for itemprop (thank you @urbgimtam)
- added examples for itemprop
- added examples for use with ngRoute and ui-router
- fix bower dependencies
- added update-title directive
- updated documentation
- updated example
- added dynamic tag support
- updated documentation
- updated example
- fixed issue with
http-equiv
- updated documentation
- updated example
- added support for meta
property
elements to support the Open Graph protocol
- switch to native
element.querySelector()
method to fix #4
- added double quotes to support #2
- fix bower ignore files
- update bower ignore files
- add travis support
- update documentation
- refactor to support original markup
- add unit tests
- update documentation
- Initial version