Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:03,000 --> 00:00:06,421
We set a default title in the RootLayout,
2
00:00:06,421 --> 00:00:09,876
and a template that will add the website name
3
00:00:09,876 --> 00:00:12,310
to any page-specific title.
4
00:00:12,310 --> 00:00:14,499
Now, we should also set some
5
00:00:14,499 --> 00:00:16,609
metadata in the ReviewPage,
6
00:00:16,687 --> 00:00:19,034
so that each review will have
7
00:00:19,034 --> 00:00:21,220
its own SEO-friendly title.
8
00:00:21,300 --> 00:00:24,049
At the moment, it's always "Indie Gamer",
9
00:00:24,049 --> 00:00:25,905
because that's the default.
10
00:00:25,905 --> 00:00:28,330
But here we have a small problem,
11
00:00:28,330 --> 00:00:30,068
in that the title should be
12
00:00:30,068 --> 00:00:31,742
different for each review,
13
00:00:31,806 --> 00:00:35,200
based on the data we read from the Markdown file.
14
00:00:35,200 --> 00:00:38,858
So, we cannot simply export a constant object,
15
00:00:38,858 --> 00:00:40,939
like we did in the other pages.
16
00:00:40,939 --> 00:00:43,956
We need to load the review data first,
17
00:00:43,956 --> 00:00:47,234
and only then we'll know what the title should be.
18
00:00:47,234 --> 00:00:49,606
For these cases, Next.js lets
19
00:00:49,606 --> 00:00:51,978
us export a function instead,
20
00:00:52,059 --> 00:00:54,306
called "generateMetadata",
21
00:00:54,359 --> 00:00:56,680
where we can run some code before
22
00:00:56,680 --> 00:00:58,790
returning the metadata object.
23
00:00:58,860 --> 00:01:01,947
What we want to do here is call getReview
24
00:01:01,947 --> 00:01:03,679
to load the right data.
25
00:01:03,754 --> 00:01:06,688
But to do that we need to know the "slug".
26
00:01:06,688 --> 00:01:08,959
Even though it's not a component,
27
00:01:08,959 --> 00:01:11,145
the "generateMetadata" function
28
00:01:11,145 --> 00:01:12,696
receives some "props".
29
00:01:12,767 --> 00:01:14,622
Let's start by logging them,
30
00:01:14,622 --> 00:01:17,534
so you can see for yourself what they contain.
31
00:01:18,102 --> 00:01:20,669
If we save, and check the server logs,
32
00:01:20,669 --> 00:01:22,845
when we load this URL,
33
00:01:22,845 --> 00:01:26,438
Next.js renders the ReviewPage component,
34
00:01:26,438 --> 00:01:29,806
and also calls our "generateMetadata" function
35
00:01:29,806 --> 00:01:32,354
passing some props with the "params"
36
00:01:32,354 --> 00:01:34,052
that contain the "slug".
37
00:01:34,123 --> 00:01:36,563
So, they're the same props passed
38
00:01:36,563 --> 00:01:38,190
to our page component.
39
00:01:38,264 --> 00:01:41,727
And that's how we can know which review to load.
40
00:01:41,727 --> 00:01:43,670
We can destructure the "props"
41
00:01:43,670 --> 00:01:45,225
in exactly the same way.
42
00:01:45,289 --> 00:01:47,744
And then, instead of logging something,
43
00:01:47,744 --> 00:01:49,713
we can load the review data,
44
00:01:49,713 --> 00:01:52,109
just like we do in the component.
45
00:01:52,109 --> 00:01:55,410
At this point we can return the metadata object,
46
00:01:55,410 --> 00:01:59,030
setting the "title" to be simply "review.title".
47
00:01:59,030 --> 00:02:00,248
And that's it.
48
00:02:00,450 --> 00:02:02,479
If we reload this page now,
49
00:02:02,479 --> 00:02:05,031
actually it already updated automatically;
50
00:02:05,031 --> 00:02:08,006
but you can see that the title says "Hellblade",
51
00:02:08,006 --> 00:02:10,739
followed by "Indie Gamer" as usual,
52
00:02:10,739 --> 00:02:13,259
because it always uses the template.
53
00:02:13,259 --> 00:02:15,105
If we look at another review,
54
00:02:15,105 --> 00:02:18,320
this one shows "Hollow Knight" in the title.
55
00:02:18,320 --> 00:02:19,827
As you would expect.
56
00:02:19,980 --> 00:02:22,707
And if we open the Stardew Valley page
57
00:02:22,707 --> 00:02:24,756
of course it will have the same
58
00:02:24,756 --> 00:02:26,408
title in the browser tab.
59
00:02:26,474 --> 00:02:28,288
So this is how we can set
60
00:02:28,288 --> 00:02:30,102
the metadata dynamically,
61
00:02:30,174 --> 00:02:33,800
after loading the data specific to each route.
62
00:02:33,800 --> 00:02:36,614
We're only setting the "title" in this example,
63
00:02:36,614 --> 00:02:38,363
but you could do the same for
64
00:02:38,363 --> 00:02:39,932
other metadata properties,
65
00:02:39,992 --> 00:02:42,981
like "description", "keywords", and so on.
4720
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.