Explain Codes LogoExplain Codes Logo

Where to find or how to set htmlWebpackPlugin.options.title in project created with vue cli 3?

html
vue-meta
webpack
javascript
Alex KataevbyAlex Kataev·Nov 22, 2024
TLDR

Simply set the title in the vue.config.js inside the pages configuration:

// vue.config.js module.exports = { pages: { index: { title: 'Your Page Title', }, }, };

Don't forget to include <title><%= htmlWebpackPlugin.options.title %></title> in your index.html file for this to take effect. Also, if your use-case demands, runtime title updates can be achieved using Vue Meta or JavaScript. More on these below!

Dynamic Title: Chameleon Mode

Vue Meta: Metadata Management Guru

Vue Meta is a Japanese sensei for maintaining your Vue app's metadata! Install it (npm install vue-meta), use it in your main.js and define the metadata (including title) in your Vue components. Check how easy-peasy it is:

// Who needs Chuck Norris if you've got Vue Meta? 😄 import Vue from 'vue'; import VueMeta from 'vue-meta'; Vue.use(VueMeta);

Now, you can define the title in your components:

// Your component is now a horseshoe to my foot… export default { metaInfo: { title: 'Dynamic Page Title', // Populate more meta tags as you please... } }

Vue Meta supports real-time updates and works well with SSR (Server-Side Rendering).

JavaScript: Native DIY Method

Need to change the title at runtime? Just use JavaScript in your component's mounted hook:

// Look mum, no external libraries! 💪 mounted() { document.title = 'Your Dynamic Title'; }

Static Title: Minimalist's Choice

Hardcoding: The Old School Style

You can set the title directly in the public/index.html file. It's as simple as:

<!-- I’m HTML5 and I was here first! --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Your Static Page Title</title> </head> <body> <div id="app"></div> </body> </html>

Webpack Chain: For the Inner Control Freak

Alternatively, you can talk directly to Webpack via the chainWebpack property:

// Chain me up, Scotty! 🛸 module.exports = { chainWebpack: config => { config.plugin('html') .tap(args => { args[0].title = 'Your Page Title'; return args; }); } }

Remember to restart your server after changes to vue.config.js, as these don't support hot reloading.

Environmental Wisdom: A Title for Every occasion

Environment Variables: The Swiss Army Knife Approach

Keeping title as an environment variable allows you to pivot it as per the respective environment. You need a .env file in your project root - like .env.development or .env.production. Set your titles like VUE_APP_TITLE=My Awesome App and then access them within your app:

// I see dead variables 👻 mounted() { document.title = process.env.VUE_APP_TITLE; }

It's like having unique headgear for every occasion!

Don’t Forget UX: Titles Matter!

Ensure your title reflects your app and makes sense for the user – remember, UX comes first!

Common Traps and Triumphs

Title Updates MIA

Changes not showing up after updates in vue.config.js? Restart your server – hot reloading doesn't cover these changes.

Missing Dynamic Title Updates

Not seeing live title updates? Make sure you're using Vue Meta or setting document.title in a mounted hook.

Title Not Reading Environment Variable

Ensure environment variables carry the VUE_APP_ prefix to be accessible in your Vue app.