00:02
just have slash AdonisJS being used as our page title. So if we click between our login and register, you can see it just maintains slash AdonisJS. But if we go to our homepage,
00:10
it does actually use homepage slash AdonisJS. So our goal in this lesson is to talk about how that's happening. So let's go ahead and hide our browser back away. Now let's start by taking a look at our homepage
00:19
because that's the one that's slightly different. And you'll see that it has added a head component to our page title. So let's go ahead and click on that. And you'll see that it has added a head component
00:30
within the template for the page. And it has the title homepage applied to it. This head component comes from InertiaJS. And this head component is how we can actually apply
00:38
metadata and page titles to our document on a per-page basis. We just specify the head component. We can provide in a title,
00:46
as well as any other metadata as child content to the head component. And it will automatically swap that out inside of our document's head. And this is being set up, if we scroll down and go into our resources,
00:56
InertiaLayout by that Inertia head tag that we have inside of our edge page. Going back to our homepage, it's using the shorthand to set the title, but we could also switch this
01:06
to provide it as a direct child content. So we can provide in our head there with title homepage, and it will be provided just the same as it did with that same shorthand
01:16
that we saw a moment ago right here. Now that takes care of why we see homepage on this one, but what about the hyphen AdonisJS that we see on our login and register pages?
01:25
Well, that comes back to our app.ts file. Within here, we have that title property, which provides a callback function, accepting in the title, and there is the hyphen app name,
01:34
which is defaulting to AdonisJS. It will first attempt to find a vapp name from our environment variables, and if it does, it will use that instead of the AdonisJS. So if we wanted to,
01:44
we could scroll down to our .env file and provide in a vapp name equals .mycourse, jump back into our browser, and now you'll see that that just replaced
01:54
hyphen AdonisJS with hyphen .mycourse, because during build, Vite will automatically swap out at that build time, anything that is prefixed with Vite
02:04
inside of our environment throughout our client side application whenever we make reference to it via import meta.env. And since we have this prefixed with Vite, it is being replaced at build time.
02:13
Okay, let's go ahead and now apply an actual title value inside of our login and register pages. So the first thing that we're gonna wanna do is import head.
02:21
So we'll import head from at inner attribute three. Now, just like our link component, you can also register this globally. We're not gonna do that here because we're actually going to create an alternative at the end of this lesson,
02:31
but that's totally an option if you'd prefer to go that way. And we just need to add that head component into our template, and then we can apply a title or you can use the shorthand like we saw on our homepage
02:41
to say that this is our login page. And if we wanted to apply a meta description to here as well, we can do that too. So meta name description, and then apply the content
02:51
as login to your plot my course accounts, and then end that meta tag. If we give that a save, jump back into our browser, our login page refreshed.
03:01
Let's go ahead and refresh it to get that fixed. And now we can see up in our title bar that we have login hyphen plot my course being applied. Furthermore, if we take a look at our inspect panel and take a look at our head,
03:10
we can see our title right here, which is working a-okay. But if we look up a little bit further at our meta tags, there's our meta description being applied there too.
03:18
And Inertia is adding in an Inertia attribute so that it knows that it can be swapped out via Inertia itself, because Inertia will not overwrite anything
03:28
that you write inside of your actual document outside of your Inertia application. For example, if we were to go into our Inertia layout page
03:36
and apply in meta name description content, this is a description, jump back into our browser, give it a refresh just for sanity sake here
03:46
and take a look at our head again. You'll see that we now have two meta descriptions in here. We have our meta name description, this is my description and meta name description login to your plot my course account. Since we're setting this meta description
03:56
at our server level inside of our root template for Inertia.js, it has not applied an Inertia attribute to it telling itself that it cannot overwrite that meta description,
04:06
but it has a meta description to apply. Therefore it is applying that second one right up here, which does have an Inertia tag. So it's saying that as you request subsequent pages,
04:15
it can swap this particular meta tag out. So do keep that in mind. If you set a meta description inside of your Inertia layout here, Inertia will not be able to change it
04:25
and it may add a duplicate to your document. So we don't want it there. You wanna maintain it on a per page basis. So we'll leave it in our view pages rather than at the Inertia layout level.
04:35
Okay, so last thing to do here, in most applications, it's pretty common that you might have a number of different tags that you would want some kind of normalization for and applied throughout most of your pages,
04:45
maybe not all, but most. So we can extract this out and simplify that use case for us into a component rather than using the head component in each and every one of our pages.
04:53
So we can scroll up here to our Inertia directory, go into our components, right-click it, add new file, and we'll call this component maybe app-head.vue.
05:02
Inside of here, we'll add our script setup lang of TS. And we can go ahead and import the head component from at Inertia view three, and then apply in our template,
05:12
making use of that head component. And we're gonna keep things simple for our application. We're not gonna have too much inside of the head to worry about. So we're just gonna apply a title,
05:19
and then we'll also add in a meta name description as well. What we wanna do is define props here. So define props and allow ourselves to provide values for these in.
05:29
So we'll have a title of type string and a description of type string there as well. For the title, we just provide it into the title tag. And for the description,
05:37
we need to just provide it into the content attribute, just like so. And with that, now we can make use of our head component in any one of our pages. In our Unplugin view components, we'll automatically import the head component
05:47
registered globally for us as we make use of it throughout our pages. And then we can do any global normalizations that we want to our meta information directly inside of this head component at any point in time.
05:57
So jump back into our login page. We can get rid of our head import right up there, and we can replace our head component with the app head component and provide in a title of login
06:07
and a description of login to your BotMyCourse account. And that component and get rid of the head altogether. Let's also go ahead and give that line there a copy
06:17
and jump over to our register page. And let's paste it into our template here as well. This one will want to change the text to register and we'll change the description here to something like start plotting
06:27
and planning your courses with ease by creating your BotMyCourse account today. Give that a save and it should break it down into separate lines. There we go.
06:37
And let's go ahead and jump back into our browser and check everything out. So we can see, we still see login here for our login page. For register, we see register. If we take a look at our head element,
06:45
there's start plotting and planning, yada, yada, yada for our meta description. And if we jump back to our login, we see that swaps out with login to your PlotMyCourse account. Awesome, so that's all working okay.