This repository has been archived by the owner on Jan 17, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract rest calls pre render on index/dashboard, add error handling.
- Loading branch information
1 parent
609842c
commit bb4b522
Showing
7 changed files
with
120 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,32 @@ | ||
import React from "react"; | ||
import { Links } from "../components/links"; | ||
import { get } from "../services/rest_service"; | ||
|
||
function Page() { | ||
type Props = { | ||
message: string; | ||
} | ||
|
||
function Page({message}: Props) { | ||
return <> | ||
<Links/> | ||
<p>The following is a result of a server side api call pre-render. If you right click and view source, the response from the API call will be visible in the source. This is different than say... Inspect Element, which shows the client side rendered content.</p> | ||
<p>This means that search engines can scrape this page, and immediately see the content, without trusting that the search engines can render SPA's.</p> | ||
<h2><small style={{color: "grey"}}>API Call:</small> {message}</h2> | ||
</>; | ||
} | ||
|
||
Page.getInitialProps = async () => { | ||
const res: any = await get("/api/unrestricted"); | ||
|
||
let message = "Something unexpected happened!"; | ||
|
||
if (res.error) { | ||
message = res.error; | ||
} else if (res.data && res.data.message) { | ||
message = res.data.message | ||
} | ||
|
||
return { message }; | ||
}; | ||
|
||
export default Page; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import axios, { AxiosRequestConfig } from "axios"; | ||
import { catchAxiosError } from "./error"; | ||
|
||
|
||
const baseConfig: AxiosRequestConfig = { | ||
baseURL: "http://localhost:1323", | ||
}; | ||
|
||
export const post = (url: string, data: URLSearchParams) => { | ||
return axios.post(url, data, baseConfig).catch(catchAxiosError); | ||
}; | ||
|
||
export const get = async (url: string, config: AxiosRequestConfig = {}) => { | ||
const axiosConfig = { | ||
...baseConfig, | ||
...config, | ||
}; | ||
return await axios.get(url, axiosConfig).catch(catchAxiosError) | ||
}; |