This repository was archived by the owner on May 10, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 24 files changed +1221
-1
lines changed Expand file tree Collapse file tree 24 files changed +1221
-1
lines changed Original file line number Diff line number Diff line change
1
+ export default async function preview ( req , res ) {
2
+ const { query } = req ;
3
+ const { id } = query ;
4
+
5
+ // Enable Preview Mode by setting the cookies
6
+ res . setPreviewData ( { } ) ;
7
+
8
+ // Redirect to the path from the fetched post
9
+ // We don't redirect to req.query.slug as that might lead to open redirect vulnerabilities
10
+ res . writeHead ( 307 , { Location : `/previewTest/${ id } ` } ) ;
11
+ res . end ( ) ;
12
+ }
Original file line number Diff line number Diff line change
1
+ export default async function preview ( req , res ) {
2
+ const { query } = req ;
3
+ const { id } = query ;
4
+
5
+ // Enable Preview Mode by setting the cookies
6
+ res . setPreviewData ( { } ) ;
7
+
8
+ // Redirect to the path from the fetched post
9
+ // We don't redirect to req.query.slug as that might lead to open redirect vulnerabilities
10
+ res . writeHead ( 307 , { Location : `/previewTest/static` } ) ;
11
+ res . end ( ) ;
12
+ }
Original file line number Diff line number Diff line change
1
+ export default async function exit ( _ , res ) {
2
+ // Exit the current user from "Preview Mode". This function accepts no args.
3
+ res . clearPreviewData ( ) ;
4
+
5
+ // Redirect the user back to the index page.
6
+ res . writeHead ( 307 , { Location : "/" } ) ;
7
+ res . end ( ) ;
8
+ }
Original file line number Diff line number Diff line change
1
+ export default async function preview ( req , res ) {
2
+ const { query } = req ;
3
+ const { to } = query ;
4
+
5
+ res . redirect ( `/shows/${ to } ` ) ;
6
+ }
Original file line number Diff line number Diff line change
1
+ export default async ( req , res ) => {
2
+ // Get the params and query string parameters
3
+ const { query } = req ;
4
+ const { params, ...queryStringParams } = query ;
5
+
6
+ // Get the ID of the show
7
+ const id = params [ 0 ] ;
8
+
9
+ // Get the data
10
+ const fetchRes = await fetch ( `https://api.tvmaze.com/shows/${ id } ` ) ;
11
+ const data = await fetchRes . json ( ) ;
12
+
13
+ // If show was found, return it
14
+ if ( fetchRes . status == 200 ) {
15
+ res . status ( 200 ) ;
16
+ res . json ( { params, queryStringParams, show : data } ) ;
17
+ }
18
+ // If show was not found, return error
19
+ else {
20
+ res . status ( 404 ) ;
21
+ res . json ( { error : "Show not found" } ) ;
22
+ }
23
+ } ;
Original file line number Diff line number Diff line change
1
+ export default async ( req , res ) => {
2
+ // Get the ID of the show
3
+ const { query } = req ;
4
+ const { id } = query ;
5
+
6
+ // Get the data
7
+ const fetchRes = await fetch ( `https://api.tvmaze.com/shows/${ id } ` ) ;
8
+ const data = await fetchRes . json ( ) ;
9
+
10
+ // If show was found, return it
11
+ if ( fetchRes . status == 200 ) {
12
+ res . status ( 200 ) ;
13
+ res . json ( { show : data } ) ;
14
+ }
15
+ // If show was not found, return error
16
+ else {
17
+ res . status ( 404 ) ;
18
+ res . json ( { error : "Show not found" } ) ;
19
+ }
20
+ } ;
Original file line number Diff line number Diff line change
1
+ export default ( req , res ) => {
2
+ // We can set custom headers
3
+ res . setHeader ( "My-Custom-Header" , "header123" ) ;
4
+
5
+ res . status ( 200 ) ;
6
+ res . json ( { message : "hello world :)" } ) ;
7
+ } ;
Original file line number Diff line number Diff line change
1
+ import Error from "next/error" ;
2
+ import Link from "next/link" ;
3
+
4
+ const Show = ( { errorCode, show } ) => {
5
+ // If show item was not found, render 404 page
6
+ if ( errorCode ) {
7
+ return < Error statusCode = { errorCode } /> ;
8
+ }
9
+
10
+ // Otherwise, render show
11
+ return (
12
+ < div >
13
+ < p >
14
+ This page uses getInitialProps() to fetch the show with the ID provided
15
+ in the URL: /shows/:id
16
+ < br />
17
+ Refresh the page to see server-side rendering in action.
18
+ < br />
19
+ You can also try changing the ID to any other number between 1-10000.
20
+ </ p >
21
+
22
+ < hr />
23
+
24
+ < h1 > Show #{ show . id } </ h1 >
25
+ < p > { show . name } </ p >
26
+
27
+ < hr />
28
+
29
+ < Link href = "/" >
30
+ < a > Go back home</ a >
31
+ </ Link >
32
+ </ div >
33
+ ) ;
34
+ } ;
35
+
36
+ export const getServerSideProps = async ( { params } ) => {
37
+ // The ID to render
38
+ const { id } = params ;
39
+
40
+ const res = await fetch ( `https://api.tvmaze.com/shows/${ id } ` ) ;
41
+ const data = await res . json ( ) ;
42
+
43
+ // Set error code if show item could not be found
44
+ const errorCode = res . status > 200 ? res . status : false ;
45
+
46
+ return {
47
+ props : {
48
+ errorCode,
49
+ show : data ,
50
+ } ,
51
+ } ;
52
+ } ;
53
+
54
+ export default Show ;
Original file line number Diff line number Diff line change
1
+ import Error from "next/error" ;
2
+ import Link from "next/link" ;
3
+
4
+ const Show = ( { errorCode, show } ) => {
5
+ // If show item was not found, render 404 page
6
+ if ( errorCode ) {
7
+ return < Error statusCode = { errorCode } /> ;
8
+ }
9
+
10
+ // Otherwise, render show
11
+ return (
12
+ < div >
13
+ < p >
14
+ This page uses getInitialProps() to fetch the show with the ID provided
15
+ in the URL: /shows/:id
16
+ < br />
17
+ Refresh the page to see server-side rendering in action.
18
+ < br />
19
+ You can also try changing the ID to any other number between 1-10000.
20
+ </ p >
21
+
22
+ < hr />
23
+
24
+ < h1 > Show #{ show . id } </ h1 >
25
+ < p > { show . name } </ p >
26
+
27
+ < hr />
28
+
29
+ < Link href = "/" >
30
+ < a > Go back home</ a >
31
+ </ Link >
32
+ </ div >
33
+ ) ;
34
+ } ;
35
+
36
+ export const getServerSideProps = async ( { params } ) => {
37
+ // The ID to render
38
+ const { slug } = params ;
39
+ const id = slug [ 0 ] ;
40
+
41
+ const res = await fetch ( `https://api.tvmaze.com/shows/${ id } ` ) ;
42
+ const data = await res . json ( ) ;
43
+
44
+ // Set error code if show item could not be found
45
+ const errorCode = res . status > 200 ? res . status : false ;
46
+
47
+ return {
48
+ props : {
49
+ errorCode,
50
+ show : data ,
51
+ } ,
52
+ } ;
53
+ } ;
54
+
55
+ export default Show ;
Original file line number Diff line number Diff line change
1
+ import Link from "next/link" ;
2
+
3
+ const Show = ( { show } ) => (
4
+ < div >
5
+ < p >
6
+ This page uses getInitialProps() to fetch the show with the ID provided in
7
+ the URL: /shows/:id
8
+ < br />
9
+ Refresh the page to see server-side rendering in action.
10
+ < br />
11
+ You can also try changing the ID to any other number between 1-10000.
12
+ </ p >
13
+
14
+ < hr />
15
+
16
+ < h1 > Show #{ show . id } </ h1 >
17
+ < p > { show . name } </ p >
18
+
19
+ < hr />
20
+
21
+ < Link href = "/" >
22
+ < a > Go back home</ a >
23
+ </ Link >
24
+ </ div >
25
+ ) ;
26
+
27
+ export const getServerSideProps = async ( { params } ) => {
28
+ const res = await fetch ( "https://api.tvmaze.com/shows/42" ) ;
29
+ const data = await res . json ( ) ;
30
+
31
+ return {
32
+ props : {
33
+ show : data ,
34
+ } ,
35
+ } ;
36
+ } ;
37
+
38
+ export default Show ;
Original file line number Diff line number Diff line change
1
+ import Link from "next/link" ;
2
+
3
+ const Show = ( { show } ) => (
4
+ < div >
5
+ < p > This page uses getStaticProps() to pre-fetch a TV show.</ p >
6
+
7
+ < hr />
8
+
9
+ < h1 > Show #{ show . id } </ h1 >
10
+ < p > { show . name } </ p >
11
+
12
+ < hr />
13
+
14
+ < Link href = "/" >
15
+ < a > Go back home</ a >
16
+ </ Link >
17
+ </ div >
18
+ ) ;
19
+
20
+ export async function getStaticPaths ( ) {
21
+ // Set the paths we want to pre-render
22
+ const paths = [ { params : { id : "1" } } , { params : { id : "2" } } ] ;
23
+
24
+ // We'll pre-render only these paths at build time.
25
+ // { fallback: false } means other routes should 404.
26
+ return { paths, fallback : false } ;
27
+ }
28
+
29
+ export async function getStaticProps ( { params } ) {
30
+ // The ID to render
31
+ const { id } = params ;
32
+
33
+ const res = await fetch ( `https://api.tvmaze.com/shows/${ id } ` ) ;
34
+ const data = await res . json ( ) ;
35
+
36
+ return {
37
+ props : {
38
+ show : data ,
39
+ } ,
40
+ } ;
41
+ }
42
+
43
+ export default Show ;
Original file line number Diff line number Diff line change
1
+ import Link from "next/link" ;
2
+
3
+ const Show = ( { show } ) => (
4
+ < div >
5
+ < p > This page uses getStaticProps() to pre-fetch a TV show.</ p >
6
+
7
+ < hr />
8
+
9
+ < h1 > Show #{ show . id } </ h1 >
10
+ < p > { show . name } </ p >
11
+
12
+ < hr />
13
+
14
+ < Link href = "/" >
15
+ < a > Go back home</ a >
16
+ </ Link >
17
+ </ div >
18
+ ) ;
19
+
20
+ export async function getStaticProps ( context ) {
21
+ const res = await fetch ( `https://api.tvmaze.com/shows/71` ) ;
22
+ const data = await res . json ( ) ;
23
+
24
+ return {
25
+ props : {
26
+ show : data ,
27
+ } ,
28
+ } ;
29
+ }
30
+
31
+ export default Show ;
Original file line number Diff line number Diff line change
1
+ import Link from "next/link" ;
2
+
3
+ const Show = ( { show } ) => (
4
+ < div >
5
+ < p > This page uses getStaticProps() to pre-fetch a TV show.</ p >
6
+
7
+ < hr />
8
+
9
+ < h1 > Show #{ show . id } </ h1 >
10
+ < p > { show . name } </ p >
11
+
12
+ < hr />
13
+
14
+ < Link href = "/" >
15
+ < a > Go back home</ a >
16
+ </ Link >
17
+ </ div >
18
+ ) ;
19
+
20
+ export async function getStaticProps ( context ) {
21
+ const res = await fetch ( `https://api.tvmaze.com/shows/71` ) ;
22
+ const data = await res . json ( ) ;
23
+
24
+ return {
25
+ props : {
26
+ show : data ,
27
+ } ,
28
+ revalidate : 1 ,
29
+ } ;
30
+ }
31
+
32
+ export default Show ;
You can’t perform that action at this time.
0 commit comments