Master SEO and meta in @vue/cli projects

Mayank Chaudhari
4 min readJul 27, 2021
Inspired from a Vue Society Show Off

How to set htmlWebpack-Plugin.options.title in project created with @vue/cli? The question might seem bit odd but this is one of my most searched and liked questions on StackOverflow. So, here it is with some added spices!

Thought questions is looking for setting htmlWebpackPlugin.options.title, the ultimate effect is changing the title of the web-page.

1. Most convenient and trivial solution

The simplest way to do this is to modify the public/index.html and hard-code the title.

The default index.html scaffold by vue cli

This is the default public/index.html that is generated by vue cli. And in this, you just need to change

<title>
<%= htmlWebpackPlugin.options.title %>
</title>

to

<title>Title of your choice</title>

2. Change the name field in package.json

Another simple solution is to change the "name": "your-project-name". However, there are many restrictions on the name you can use in package.json. You can read more about this here. Basically, package.json must contain a name and that must be lowercase and one word, and may contain hyphens and underscores.

3. Using pages field in vue.config.js

vue.config.js is an optional file that you can add to provide additional configurations to Vue CLI and this file, if present, will be automatically loaded by Vue CLI. You need to create vue.config.js in the root folder - the folder containing you package.json file.

According to Vue documentation, you can use pages field to define entrypoint for multi-page app. However, you can also use this to define title for single page app as well. Create vue.config.js in the root directory and add pages field to your exports as follows:

Note that if you are already running development server, this change will be reflected only when you stop and restart the development server. In other words, these changes will not be hot reloaded.

4. Chaining Webpack

You can chain Webpack in vue.config.js as shown below

Note that similar to solution 3, this change will be reflected only when you stop and restart the development server, in case you are already running development server. In other words, these changes will not be hot reloaded.

5. Modify title in lifecycle hooks using JavaScript

The next solution in the list is to use JavaScript to modify the title. You can do this either in mounted lifecycle hook of your root component or if you want different title for different routes, you can do the same for components loaded by each route.

6. Use Vue Meta

Finally you can use Vue Meta to manage all metadata for your Vue app including title. First you need to add Vue Meta to your project and then use metaInfo field as shown below to configure metadata for your page or route.

Conclusion

The first 4 solutions are static ways of changing your title or in other words you can’t change your title at runtime using these ways. Also all of these are not hot reloaded. The last 2 options use JavaScript and can manipulate the title at runtime.

Finally, I have something exciting for you guys. Guess what?

I am excited to offer exciting discounts on my Vue JS courses. Hurry!

Originally published at https://stackoverflow.com on May 26, 2020.

--

--

Mayank Chaudhari

Technical Writer | Developer | Researcher | Freelancer | Open Source Contributor