Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,150 --> 00:00:03,091
So now that we understand
2
00:00:03,091 --> 00:00:05,689
this concept of having a fallback
3
00:00:05,689 --> 00:00:10,610
and of generating certain pages for certain IDs,
4
00:00:10,610 --> 00:00:13,850
let's take another look at the not found case
5
00:00:13,850 --> 00:00:18,030
that we're trying to request a page which doesn't exist.
6
00:00:18,030 --> 00:00:21,850
And let's start by simply visiting a page
7
00:00:21,850 --> 00:00:23,753
with a product ID of P4.
8
00:00:24,910 --> 00:00:28,340
Now we get a 404 error page here.
9
00:00:28,340 --> 00:00:32,330
So a not found page because in our data
10
00:00:32,330 --> 00:00:36,130
we only have the IDs P1, P2 and P3.
11
00:00:36,130 --> 00:00:39,690
There is no product with an ID of P4.
12
00:00:39,690 --> 00:00:42,310
Now we get this not found error
13
00:00:42,310 --> 00:00:44,890
because in getStaticPaths,
14
00:00:44,890 --> 00:00:49,160
we load and build our array of IDs
15
00:00:49,160 --> 00:00:52,640
our array of routes or of parameters
16
00:00:52,640 --> 00:00:55,150
for which pages should be generated
17
00:00:55,150 --> 00:00:59,040
from that dummy-backend.json file.
18
00:00:59,040 --> 00:01:03,150
So paths with params here in the end is an array
19
00:01:03,150 --> 00:01:05,820
where we only configure param pairs
20
00:01:07,437 --> 00:01:12,437
So pid value pairs for the values P1, P2, P3
21
00:01:13,610 --> 00:01:16,430
since these are the only IDs that exist
22
00:01:16,430 --> 00:01:19,890
in that dummy-backend.json file.
23
00:01:19,890 --> 00:01:24,290
And automatically if we then try to load this page
24
00:01:24,290 --> 00:01:27,541
for an ID which was not pre-generated,
25
00:01:27,541 --> 00:01:31,460
we do get this 404 error.
26
00:01:31,460 --> 00:01:33,410
Now that makes sense.
27
00:01:33,410 --> 00:01:36,910
What if we use fallback true though?
28
00:01:36,910 --> 00:01:41,050
What if we for example assume that the products
29
00:01:41,050 --> 00:01:45,890
stored in dummy-backend.json might not be all the products
30
00:01:45,890 --> 00:01:49,010
for which we are able to fetch data.
31
00:01:49,010 --> 00:01:53,670
So we only generate pages for the three product IDs
32
00:01:53,670 --> 00:01:57,490
we find in that file, because we go through that file here
33
00:01:57,490 --> 00:01:59,580
in getStaticPaths
34
00:01:59,580 --> 00:02:02,450
but by setting fallback to true,
35
00:02:02,450 --> 00:02:05,800
we then also tell Next.js
36
00:02:05,800 --> 00:02:10,800
that even if an ID value is not found here
37
00:02:11,140 --> 00:02:14,700
we still might be able to render a page.
38
00:02:14,700 --> 00:02:16,950
That's what the idea behind fallback is
39
00:02:16,950 --> 00:02:20,720
that we don't have to predefine all possible pages,
40
00:02:20,720 --> 00:02:24,203
all possible values for the dynamic segment.
41
00:02:25,180 --> 00:02:29,280
So therefore, now if I set fallback to true here
42
00:02:29,280 --> 00:02:33,590
we will see that if I visit p4
43
00:02:33,590 --> 00:02:36,140
I get this error that
44
00:02:36,140 --> 00:02:39,500
we cannot read the property title of undefined.
45
00:02:39,500 --> 00:02:42,320
Now we saw that before in the course already
46
00:02:42,320 --> 00:02:44,220
we get this because we need
47
00:02:44,220 --> 00:02:47,440
to have this fallback scenario here
48
00:02:47,440 --> 00:02:50,570
that we're still waiting for a product
49
00:02:50,570 --> 00:02:54,410
because when we set fallback to true and not to blocking
50
00:02:54,410 --> 00:02:58,060
then Next.js will instantly return a page
51
00:02:58,060 --> 00:03:00,310
which does not yet have the data.
52
00:03:00,310 --> 00:03:03,120
And it will load the data in the background
53
00:03:03,120 --> 00:03:06,880
and give it to us on the page and re render the page
54
00:03:06,880 --> 00:03:08,950
once the data is there.
55
00:03:08,950 --> 00:03:12,460
So we need to render this fallback for the time
56
00:03:12,460 --> 00:03:15,273
we're waiting for the product to load.
57
00:03:16,530 --> 00:03:21,260
But if I now do that and I reload, you see
58
00:03:21,260 --> 00:03:23,200
we see loading here,
59
00:03:23,200 --> 00:03:26,010
but then after a while we get another error
60
00:03:26,010 --> 00:03:28,850
failed to load static props.
61
00:03:28,850 --> 00:03:32,050
And this hopefully also makes sense.
62
00:03:32,050 --> 00:03:36,530
I'm trying to load this product page for an ID of P4
63
00:03:37,440 --> 00:03:40,700
for which we just don't have any data
64
00:03:40,700 --> 00:03:43,160
because in getStaticProps
65
00:03:43,160 --> 00:03:46,850
we are also reaching out to dummy-backend.json
66
00:03:46,850 --> 00:03:51,220
and we're trying to find our product by ID in that file.
67
00:03:51,220 --> 00:03:56,220
And we won't find a product with ID P4 in that file.
68
00:03:56,220 --> 00:04:00,240
So we see loading initially as a fallback,
69
00:04:00,240 --> 00:04:03,410
but then Next.js tries to load the actual data
70
00:04:03,410 --> 00:04:06,380
for this page and it just fails there
71
00:04:06,380 --> 00:04:09,040
because we don't have such a product.
72
00:04:09,040 --> 00:04:12,060
So that's why we get this error eventually here then
73
00:04:12,060 --> 00:04:15,540
because it failed to load the actual data.
74
00:04:15,540 --> 00:04:17,899
Now that is a perfect example
75
00:04:17,899 --> 00:04:21,589
for setting the not found property on the object
76
00:04:21,589 --> 00:04:24,360
we return in getStaticProps.
77
00:04:24,360 --> 00:04:27,290
Here when we try to get our product,
78
00:04:27,290 --> 00:04:31,280
we also wanna check if we don't have a product
79
00:04:31,280 --> 00:04:33,150
and if we don't have a product.
80
00:04:33,150 --> 00:04:37,610
So if we failed to find a product for the given ID,
81
00:04:37,610 --> 00:04:40,120
then we want to return an object
82
00:04:40,120 --> 00:04:42,950
where not found is set to true.
83
00:04:42,950 --> 00:04:46,930
By setting this, we are able to use fallback true
84
00:04:46,930 --> 00:04:50,360
and try to find a product for a parameter value
85
00:04:50,360 --> 00:04:52,179
which was not predefined here
86
00:04:52,179 --> 00:04:56,230
because maybe we do have that maybe dummy-backend.json
87
00:04:56,230 --> 00:04:58,420
is a more dynamic data source
88
00:04:58,420 --> 00:05:01,370
which could yield that product.
89
00:05:01,370 --> 00:05:03,930
But if we then still fail to fetch it
90
00:05:03,930 --> 00:05:06,470
we don't want to return to regular page
91
00:05:06,470 --> 00:05:09,600
with the missing data, which causes an error.
92
00:05:09,600 --> 00:05:12,330
But we then wanna show the not found
93
00:05:12,330 --> 00:05:15,690
the 404 error page instead.
94
00:05:15,690 --> 00:05:18,040
So with that if we save this
95
00:05:18,040 --> 00:05:23,040
now you see for P4 I get loading and then the 404 page.
96
00:05:23,870 --> 00:05:28,840
And for P3 on the other hand it still works as before.
97
00:05:28,840 --> 00:05:30,920
So that's how we can then utilize
98
00:05:30,920 --> 00:05:32,750
the not found property here
99
00:05:32,750 --> 00:05:37,150
in getStaticProps together with fallback true.
100
00:05:37,150 --> 00:05:38,180
And of course, it's up to you
101
00:05:38,180 --> 00:05:40,370
which combination makes the most sense
102
00:05:40,370 --> 00:05:43,890
for your exact scenario and use case.
103
00:05:43,890 --> 00:05:46,090
This is just one example
104
00:05:46,090 --> 00:05:51,090
and one other idea of using Next.js and its features.
8067
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.