1
00:00:00,000 --> 00:00:03,000
So we have these messages here, which are loaded

2
00:00:03,000 --> 00:00:07,000
from that dummy backend, which is a separate process,

3
00:00:07,000 --> 00:00:11,000
a separate server running in the background.

4
00:00:11,000 --> 00:00:14,000
Now, if you open the terminal

5
00:00:14,000 --> 00:00:16,000
and you go to that window where you start

6
00:00:16,000 --> 00:00:18,000
at the backend server

7
00:00:18,000 --> 00:00:21,000
and you reload this messages page

8
00:00:21,000 --> 00:00:23,000
where we see those messages,

9
00:00:23,000 --> 00:00:26,000
you should see some log messages here.

10
00:00:26,000 --> 00:00:30,000
If you don't, delete this .next folder

11
00:00:30,000 --> 00:00:32,000
in your NextJS project,

12
00:00:33,000 --> 00:00:35,000
then quit that backend server

13
00:00:36,000 --> 00:00:37,000
and restart it.

14
00:00:37,000 --> 00:00:40,000
And most importantly, quit the NextJS

15
00:00:40,000 --> 00:00:43,000
development server and also restart that.

16
00:00:43,000 --> 00:00:46,000
And then thereafter, in the terminal

17
00:00:46,000 --> 00:00:48,000
where you start at the backend server,

18
00:00:48,000 --> 00:00:51,000
if you then reload, you should definitely

19
00:00:51,000 --> 00:00:52,000
see those log messages.

20
00:00:54,000 --> 00:00:57,000
And you should see two log messages here,

21
00:00:57,000 --> 00:01:00,000
because in my NextJS application,

22
00:01:00,000 --> 00:01:04,000
I actually got two requests to that backend.

23
00:01:04,000 --> 00:01:09,000
In the messages folder, I have that layout JS file.

24
00:01:09,000 --> 00:01:13,000
And in there, I have one request to the backend

25
00:01:13,000 --> 00:01:15,000
where I fetched the messages.

26
00:01:15,000 --> 00:01:19,000
And in the page JS file, I got one request.

27
00:01:19,000 --> 00:01:22,000
So two requests sent to the backend.

28
00:01:23,000 --> 00:01:26,000
And as you see, these two requests lead

29
00:01:26,000 --> 00:01:29,000
to two request logs.

30
00:01:29,000 --> 00:01:31,000
Now, if you paid close attention,

31
00:01:31,000 --> 00:01:32,000
this might sound a bit weird

32
00:01:32,000 --> 00:01:36,000
because I did mention this request memorization,

33
00:01:36,000 --> 00:01:40,000
which was a mechanism built into NextJS

34
00:01:40,000 --> 00:01:44,000
to avoid duplicate requests to the same data source.

35
00:01:45,000 --> 00:01:49,000
And indeed, here I have one and the same data source,

36
00:01:49,000 --> 00:01:52,000
one and the same backend server,

37
00:01:52,000 --> 00:01:54,000
and I'm sending a request to that server

38
00:01:54,000 --> 00:01:56,000
from two different places.

39
00:01:56,000 --> 00:01:59,000
And that seems to lead to two different requests,

40
00:01:59,000 --> 00:02:02,000
as we can tell by these backend logs.

41
00:02:02,000 --> 00:02:04,000
And that's exactly what should be avoided.

42
00:02:06,000 --> 00:02:08,000
And indeed NextJS has a mechanism

43
00:02:08,000 --> 00:02:12,000
that avoids duplicate requests as we see it here.

44
00:02:12,000 --> 00:02:15,000
But that mechanism is only active

45
00:02:15,000 --> 00:02:18,000
if those requests are sent with exactly

46
00:02:18,000 --> 00:02:21,000
the same configuration.

47
00:02:21,000 --> 00:02:23,000
And here, I'm talking about requests sent

48
00:02:23,000 --> 00:02:25,000
with the fetch function.

49
00:02:25,000 --> 00:02:28,000
Later, I will also show you how to avoid

50
00:02:28,000 --> 00:02:31,000
duplicate requests to other data sources

51
00:02:31,000 --> 00:02:33,000
like your own database

52
00:02:33,000 --> 00:02:35,000
to which you reach out directly from

53
00:02:35,000 --> 00:02:38,000
inside the NextJS application.

54
00:02:38,000 --> 00:02:41,000
We can also avoid duplicate requests to that,

55
00:02:41,000 --> 00:02:43,000
as you will see later.

56
00:02:43,000 --> 00:02:45,000
But for now, it's about duplicate requests

57
00:02:45,000 --> 00:02:48,000
to a separate backend, which we clearly have here.

58
00:02:48,000 --> 00:02:50,000
And we have those duplicate requests

59
00:02:50,000 --> 00:02:52,000
because the request I sent here

60
00:02:52,000 --> 00:02:54,000
in the messages page component

61
00:02:54,000 --> 00:02:57,000
and the request I send in the messages layout

62
00:02:57,000 --> 00:02:58,000
are not exactly the same.

63
00:02:58,000 --> 00:03:02,000
They have a different headers configuration,

64
00:03:02,000 --> 00:03:05,000
and I set up this different configuration

65
00:03:05,000 --> 00:03:07,000
so that on the backend,

66
00:03:07,000 --> 00:03:09,000
I can log which exact

67
00:03:09,000 --> 00:03:13,000
place in the front end did send the request.

68
00:03:13,000 --> 00:03:15,000
I'm extracting that headers data here,

69
00:03:15,000 --> 00:03:18,000
and I'm logging it here, but therefore,

70
00:03:18,000 --> 00:03:21,000
I have different requests with different configurations.

71
00:03:22,000 --> 00:03:25,000
Now, what you will see is that if I get rid

72
00:03:25,000 --> 00:03:29,000
of this headers configuration in my NextJS application

73
00:03:29,000 --> 00:03:32,000
here in the messages page,

74
00:03:32,000 --> 00:03:35,000
and then also, in the messages layout,

75
00:03:35,000 --> 00:03:36,000
and I then save the code,

76
00:03:36,000 --> 00:03:38,000
I have exactly the same

77
00:03:38,000 --> 00:03:42,000
request signature in both places.

78
00:03:42,000 --> 00:03:45,000
And if I do that and I then go to my backend code,

79
00:03:45,000 --> 00:03:48,000
again, you can already see that only one

80
00:03:48,000 --> 00:03:50,000
new request was added to it here.

81
00:03:51,000 --> 00:03:54,000
Now, if I reload this messages page,

82
00:03:54,000 --> 00:03:58,000
you will see that actually no new request is sent here.

83
00:03:58,000 --> 00:04:00,000
And I'll get back to why that's the case later.

84
00:04:01,000 --> 00:04:05,000
You can always delete this .next folder though

85
00:04:05,000 --> 00:04:08,000
to reset the NextJS application essentially,

86
00:04:08,000 --> 00:04:11,000
because that .next folder holds some temporary data,

87
00:04:11,000 --> 00:04:15,000
some cache data that's used by the next application.

88
00:04:15,000 --> 00:04:17,000
And if you delete that folder

89
00:04:17,000 --> 00:04:20,000
and then restart the next development server,

90
00:04:20,000 --> 00:04:23,000
it will be recreated with fresh data.

91
00:04:23,000 --> 00:04:24,000
And if you do that

92
00:04:24,000 --> 00:04:26,000
and you go to that backend page again,

93
00:04:26,000 --> 00:04:28,000
you will see that now,

94
00:04:28,000 --> 00:04:32,000
if I maybe also restart that to clear the logs here,

95
00:04:32,000 --> 00:04:36,000
that now, if I reload that messages page,

96
00:04:36,000 --> 00:04:39,000
only one new request is added here.

97
00:04:40,000 --> 00:04:44,000
And keep in mind that earlier we had two requests,

98
00:04:44,000 --> 00:04:46,000
one from the layout, one from the page.

99
00:04:46,000 --> 00:04:49,000
Now, since I'm not sending those extra headers,

100
00:04:49,000 --> 00:04:52,000
which were different anymore, I can't tell

101
00:04:52,000 --> 00:04:55,000
where this request is coming from, but I can tell

102
00:04:55,000 --> 00:04:58,000
that it's only one request instead of two.

103
00:04:58,000 --> 00:05:02,000
So that's that request memorization in action here.

104
00:05:02,000 --> 00:05:06,000
And that's one key mechanism built into NextJS,

105
00:05:06,000 --> 00:05:08,000
which you get for free.

106
00:05:08,000 --> 00:05:10,000
Whenever you have two requests

107
00:05:10,000 --> 00:05:13,000
with exactly the same configuration.

108
00:05:13,000 --> 00:05:18,000
In this case here, fetch requests sent to a backend,

109
00:05:18,000 --> 00:05:21,000
NextJS avoids unnecessary requests,

110
00:05:21,000 --> 00:05:23,000
and instead just sends one request

111
00:05:23,000 --> 00:05:26,000
and then reuses the response in all the places

112
00:05:26,000 --> 00:05:29,000
of the application that needs the response.

