Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,190 --> 00:00:03,960
Now we made good progress
2
00:00:03,960 --> 00:00:07,120
before we explored as change Password feature
3
00:00:07,120 --> 00:00:08,130
and all of that.
4
00:00:08,130 --> 00:00:10,880
There is a little optimization, which you could
5
00:00:10,880 --> 00:00:12,980
consider implementing, especially if you had
6
00:00:12,980 --> 00:00:14,960
a bigger Application.
7
00:00:14,960 --> 00:00:18,070
At the moment, we're using this session
8
00:00:18,070 --> 00:00:21,410
functionality in a couple of different components.
9
00:00:21,410 --> 00:00:22,243
To be precise we use use session
10
00:00:22,243 --> 00:00:26,380
in the Main Navigation component.
11
00:00:26,380 --> 00:00:27,823
Now, one problem we have
12
00:00:27,823 --> 00:00:32,823
with that is that if we are authenticated
13
00:00:33,240 --> 00:00:37,240
and I reload whilst on this profile page
14
00:00:37,240 --> 00:00:40,670
then we figure out whether the user is authenticated
15
00:00:40,670 --> 00:00:45,670
or not in that get service side props function of that page,
16
00:00:45,940 --> 00:00:48,840
here, this function runs,
17
00:00:48,840 --> 00:00:53,750
and then after the page was loaded, does use session hook
18
00:00:53,750 --> 00:00:56,020
which we use and Main Navigation.
19
00:00:56,020 --> 00:00:59,390
We'll technically also, again, run some logic to find
20
00:00:59,390 --> 00:01:02,080
out whether we have a session or not.
21
00:01:02,080 --> 00:01:05,170
And we see this here in the Network tab.
22
00:01:05,170 --> 00:01:08,600
If I reload, you see there is a request being
23
00:01:08,600 --> 00:01:12,826
sent to /api/auth/session.
24
00:01:12,826 --> 00:01:17,826
That request is actually sent by that use session hook.
25
00:01:18,500 --> 00:01:21,470
And it's sent to check Whether that session cookie
26
00:01:21,470 --> 00:01:23,440
which had found is valid,
27
00:01:23,440 --> 00:01:25,440
because we can't validate that
28
00:01:25,440 --> 00:01:29,020
with client site JavaScript for security reasons.
29
00:01:29,020 --> 00:01:31,820
Hence that cookie needs to be sent to an end point
30
00:01:31,820 --> 00:01:35,530
which then determines whether it is valid or not.
31
00:01:35,530 --> 00:01:38,010
So that's why this request is being sent.
32
00:01:38,010 --> 00:01:40,720
There is nothing wrong with this request, but
33
00:01:40,720 --> 00:01:44,745
since we already checked our session here in
34
00:01:44,745 --> 00:01:46,160
getServerSideProps.
35
00:01:46,160 --> 00:01:48,850
when we loaded slash profile
36
00:01:48,850 --> 00:01:51,470
we already know that we are authenticated.
37
00:01:51,470 --> 00:01:54,300
So this is actually a redundant request
38
00:01:54,300 --> 00:01:56,320
which is being sent here.
39
00:01:56,320 --> 00:01:58,160
And we can avoid it,
40
00:01:58,160 --> 00:02:00,950
by using a extra component offered
41
00:02:00,950 --> 00:02:05,210
by next off in our _app.JS file.
42
00:02:05,210 --> 00:02:08,759
So in this file which wraps all our page components,
43
00:02:08,759 --> 00:02:10,430
which are being rendered.
44
00:02:10,430 --> 00:02:15,430
We can import the special Provider component from
45
00:02:15,740 --> 00:02:18,003
next-auth/client.
46
00:02:19,480 --> 00:02:20,540
This is a wrapper
47
00:02:20,540 --> 00:02:24,330
which we can wrap around all our other components.
48
00:02:24,330 --> 00:02:27,300
So around the layout and de nested components
49
00:02:29,050 --> 00:02:30,950
and then Provider
50
00:02:30,950 --> 00:02:35,950
once a session prop, and here we simply set,
51
00:02:36,430 --> 00:02:39,810
any session data, which we might already have.
52
00:02:39,810 --> 00:02:41,530
Now, the interesting thing is
53
00:02:41,530 --> 00:02:44,600
that here in this root app component
54
00:02:44,600 --> 00:02:48,011
we don't just get the page component that should be loaded
55
00:02:48,011 --> 00:02:51,010
but all of the Props for that page.
56
00:02:51,010 --> 00:02:54,070
So the Props determined by get static Props
57
00:02:54,070 --> 00:02:56,497
or getServerSideProps,
58
00:02:56,497 --> 00:02:59,410
and at least for our profile page
59
00:02:59,410 --> 00:03:02,270
these Props will contain a session key
60
00:03:02,270 --> 00:03:07,270
with the session we validated inside of getServerSideProps.
61
00:03:08,920 --> 00:03:11,490
So we are setting this session Prop
62
00:03:11,490 --> 00:03:15,790
on this profile page and we are setting it to this session
63
00:03:15,790 --> 00:03:17,370
which we already loaded
64
00:03:17,370 --> 00:03:20,323
and validated with that service side code.
65
00:03:21,410 --> 00:03:25,440
That means that not all our pages, but some pages
66
00:03:25,440 --> 00:03:28,800
in this case, the Profile page we'll have a session
67
00:03:28,800 --> 00:03:31,720
Prop with session data.
68
00:03:31,720 --> 00:03:33,280
So we can set the session
69
00:03:33,280 --> 00:03:38,280
for this Provider component equal to pageProps.session.
70
00:03:38,800 --> 00:03:41,240
In most cases, this will be undefined
71
00:03:41,240 --> 00:03:44,079
because most pages don't have this Prop
72
00:03:44,079 --> 00:03:48,110
but our profile page, for example, does have it.
73
00:03:48,110 --> 00:03:51,530
And then the session is already set to this already
74
00:03:51,530 --> 00:03:53,310
loaded session.
75
00:03:53,310 --> 00:03:58,310
And that then allows next off to skip the extra session
76
00:03:58,330 --> 00:04:01,010
check performed by use session.
77
00:04:01,010 --> 00:04:01,843
If we already have the session from our
78
00:04:01,843 --> 00:04:05,453
getServeSideProp function.
79
00:04:06,620 --> 00:04:09,630
Now, if we loaded number of component, where does this not
80
00:04:09,630 --> 00:04:12,960
set session is on the find and use session
81
00:04:12,960 --> 00:04:16,120
We'll still do its thing and check it manually.
82
00:04:16,120 --> 00:04:19,899
But in cases where we already know that there is a session
83
00:04:19,899 --> 00:04:22,840
we can save some performance and a wide
84
00:04:22,840 --> 00:04:25,700
some redundant HTTP requests.
85
00:04:25,700 --> 00:04:28,780
And that's never a bad thing simply by wrapping our
86
00:04:28,780 --> 00:04:32,240
Application with this extra provider component
87
00:04:32,240 --> 00:04:36,030
and utilizing the information which we already have,
88
00:04:36,030 --> 00:04:39,660
now here in my case, I actually still see this extra session
89
00:04:39,660 --> 00:04:42,350
request being made here thereafter.
90
00:04:42,350 --> 00:04:44,730
Next off will have its reasons.
91
00:04:44,730 --> 00:04:49,130
Nonetheless, using this provider wrapper, is a recommended
92
00:04:49,130 --> 00:04:53,344
pattern because it theoretically or practically allows next
93
00:04:53,344 --> 00:04:57,600
off to optimize itself internally,
94
00:04:57,600 --> 00:04:59,360
and skip certain checks
95
00:04:59,360 --> 00:05:01,990
and possibly HTTP requests.
96
00:05:01,990 --> 00:05:04,198
Even though we unfortunately don't see that here
97
00:05:04,198 --> 00:05:06,894
nonetheless, adding this extra wrapper is
98
00:05:06,894 --> 00:05:09,160
a recommended optimization
99
00:05:09,160 --> 00:05:12,423
which idea for, of course also a wanted to show to You.
7750
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.