1
00:00:03,000 --> 00:00:05,217
We rewrote two functions to fetch

2
00:00:05,217 --> 00:00:06,695
the data from the CMS:

3
00:00:06,762 --> 00:00:09,239
"getReview" and "getReviews".

4
00:00:09,239 --> 00:00:12,004
Now, there are two other functions in this file.

5
00:00:12,004 --> 00:00:14,325
We have "getFeaturedReview",

6
00:00:14,325 --> 00:00:16,970
but this one just calls "getReviews"

7
00:00:16,970 --> 00:00:19,542
and then returns the first element.

8
00:00:19,615 --> 00:00:21,340
So it should already work,

9
00:00:21,340 --> 00:00:23,066
if we go to the Home page.

10
00:00:23,132 --> 00:00:25,419
We do see "Hades", that's most

11
00:00:25,419 --> 00:00:27,324
recent review in the CMS.

12
00:00:27,400 --> 00:00:30,735
We could make "getFeaturedReview" more efficient,

13
00:00:30,735 --> 00:00:33,430
because at the moment "getReviews"

14
00:00:33,430 --> 00:00:35,333
always fetches 6 entries

15
00:00:35,412 --> 00:00:37,704
even though getFeaturedReview

16
00:00:37,704 --> 00:00:39,601
only uses the first one.

17
00:00:39,680 --> 00:00:43,245
But we'll revisit the Home page later on anyway.

18
00:00:43,245 --> 00:00:46,697
The other function we have here is "getSlugs".

19
00:00:46,697 --> 00:00:50,115
If you remember, we used this in the ReviewPage,

20
00:00:50,115 --> 00:00:52,570
in "generateStaticParams".

21
00:00:52,570 --> 00:00:54,810
Let's re-enable this code now,

22
00:00:54,810 --> 00:00:57,663
so Next.js will statically generate

23
00:00:57,663 --> 00:00:59,620
our dynamic route again.

24
00:00:59,702 --> 00:01:01,926
Let me add a log statement here,

25
00:01:01,926 --> 00:01:04,409
and print all the "slugs" received

26
00:01:04,409 --> 00:01:06,307
by "generateStaticParams",

27
00:01:06,380 --> 00:01:09,160
so we can see what happens during the build.

28
00:01:09,251 --> 00:01:11,372
By the way, we've been adding quite

29
00:01:11,372 --> 00:01:13,129
a few "console.log" messages.

30
00:01:13,190 --> 00:01:14,734
Let's do some cleanup,

31
00:01:14,734 --> 00:01:16,590
otherwise there will be too

32
00:01:16,590 --> 00:01:18,171
much noise in the logs.

33
00:01:18,240 --> 00:01:21,491
I'll comment out these "rendering" messages.

34
00:01:21,491 --> 00:01:23,904
No need to log the full "reviews"

35
00:01:23,904 --> 00:01:25,147
array every time.

36
00:01:25,221 --> 00:01:27,888
These messages are useful only if we're

37
00:01:27,888 --> 00:01:29,939
working on that piece of code.

38
00:01:30,008 --> 00:01:32,262
In fact, we can remove the button

39
00:01:32,262 --> 00:01:33,833
clicked one altogether.

40
00:01:33,901 --> 00:01:36,430
I'll keep the other ones commented out,

41
00:01:36,430 --> 00:01:39,420
so we can easily re-enable them if we need to.

42
00:01:39,420 --> 00:01:41,847
The last one is in "strapi-request",

43
00:01:41,847 --> 00:01:43,545
that's a separate script, so

44
00:01:43,545 --> 00:01:45,061
we can leave it as it is.

45
00:01:45,121 --> 00:01:47,597
Now, let's see what data "getSlugs"

46
00:01:47,597 --> 00:01:49,154
returns at the moment,

47
00:01:49,225 --> 00:01:51,044
if we stop the dev server,

48
00:01:51,044 --> 00:01:52,864
and do a production build,

49
00:01:52,934 --> 00:01:55,276
by running "npm run build".

50
00:01:57,434 --> 00:02:00,003
"generateStaticParams" receives

51
00:02:00,003 --> 00:02:01,991
an array with 3 strings,

52
00:02:02,074 --> 00:02:05,440
based on the Markdown files available locally.

53
00:02:05,440 --> 00:02:07,152
The build then fails,

54
00:02:07,152 --> 00:02:10,383
because it tries to fetch a review from the

55
00:02:10,383 --> 00:02:13,090
CMS using "hellblade" as the "slug",

56
00:02:13,165 --> 00:02:15,171
but that's only available locally.

57
00:02:15,171 --> 00:02:17,203
That's why we need to rewrite

58
00:02:17,203 --> 00:02:18,886
the "getSlugs" function,

59
00:02:18,956 --> 00:02:21,880
and fetch the "slugs" from the CMS instead,

60
00:02:21,880 --> 00:02:24,841
similarly to what we did in "getReviews".

61
00:02:24,841 --> 00:02:26,558
In fact, the "fetch" request

62
00:02:26,558 --> 00:02:27,968
will be pretty similar,

63
00:02:28,368 --> 00:02:31,565
except that we only want the "slug" field.

64
00:02:31,565 --> 00:02:34,673
We don't need to populate the "image" either.

65
00:02:34,673 --> 00:02:37,037
We can keep sorting the reviews

66
00:02:37,037 --> 00:02:38,639
by most recent first,

67
00:02:38,715 --> 00:02:41,054
because the API always returns

68
00:02:41,054 --> 00:02:43,236
a limited number of entries.

69
00:02:43,314 --> 00:02:46,357
Ideally we'd like to get all the slugs,

70
00:02:46,357 --> 00:02:49,404
but if we don't specify a "pageSize"

71
00:02:49,404 --> 00:02:53,365
Strapi will only return 25 entries by default.

72
00:02:53,365 --> 00:02:57,652
What we could do is set a high number, like 100.

73
00:02:57,652 --> 00:03:01,194
If we end up having more than 100 reviews

74
00:03:01,194 --> 00:03:05,056
it might make sense to only generate static pages

75
00:03:05,056 --> 00:03:07,908
for the most recent 100 entries.

76
00:03:07,908 --> 00:03:10,842
Anyway, we'll see in another section what happens

77
00:03:10,842 --> 00:03:12,903
if you request a review that

78
00:03:12,903 --> 00:03:14,964
wasn't statically generated.

79
00:03:15,037 --> 00:03:18,273
For now let's just say that 100 is more

80
00:03:18,273 --> 00:03:20,846
than enough to get all of them.

81
00:03:20,929 --> 00:03:23,099
Now, here we want to return an

82
00:03:23,099 --> 00:03:24,908
array with all the slugs,

83
00:03:24,980 --> 00:03:27,627
so for each item we want to extract

84
00:03:27,627 --> 00:03:29,594
only the "slug" attribute.

85
00:03:29,670 --> 00:03:33,234
And this way we should return an array of strings.

86
00:03:33,234 --> 00:03:35,413
At this point we can also remove

87
00:03:35,413 --> 00:03:36,911
the "node:fs" imports,

88
00:03:36,979 --> 00:03:40,266
because we no longer read any local files.

89
00:03:40,266 --> 00:03:43,418
We should be fetching all the data from the CMS.

90
00:03:43,418 --> 00:03:46,356
Let's test this by running another build,

91
00:03:46,356 --> 00:03:49,187
and see which pages it generates.

92
00:03:49,296 --> 00:03:51,777
We can see all the slugs received

93
00:03:51,777 --> 00:03:53,731
by "generateStaticParams".

94
00:03:53,806 --> 00:03:57,861
These are all the 25 reviews available in Strapi.

95
00:03:57,861 --> 00:04:00,794
And in fact under the "/reviews/[slug]"

96
00:04:00,794 --> 00:04:01,771
dynamic route

97
00:04:01,847 --> 00:04:04,872
it says it generated "hades-2018",

98
00:04:04,872 --> 00:04:07,186
"fall-guys", "black-mesa",

99
00:04:07,275 --> 00:04:09,519
plus 22 more paths.

100
00:04:09,519 --> 00:04:13,398
So it generated a static page for each review.

101
00:04:13,398 --> 00:04:16,051
And this is the "getSlugs" function

102
00:04:16,051 --> 00:04:18,627
reimplemented to work with Strapi.

