-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into ncn-ssw-link-asp-net-core-migration-rules
- Loading branch information
Showing
8 changed files
with
237 additions
and
18 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
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 |
---|---|---|
|
@@ -12,6 +12,7 @@ index: | |
- bring-water-to-guests | ||
- cultural-exchange | ||
- desk-setups | ||
- google-maps-profile | ||
|
||
--- | ||
|
||
|
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
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,165 @@ | ||
--- | ||
type: rule | ||
archivedreason: | ||
title: Do you how to fetch data in Next.js? | ||
guid: df355ce6-47ab-461d-9ddc-d3216dc433b5 | ||
uri: fetch-data-nextjs | ||
created: 2023-07-28T06:34:51Z | ||
authors: | ||
- title: Harry Ross | ||
url: https://www.ssw.com.au/people/harry-ross | ||
related: | ||
- use-nextjs | ||
--- | ||
|
||
Next.js is great, as it gives you the ability to run code on the server-side. This means there are now new ways to fetch data via the server to be passed to Next.js app. Next.js also handles the automatic splitting of code that runs on the server and the client, meaning you don't have to worry about bloating your JavaScript bundle when you add code that only runs on the server. | ||
|
||
<!--endintro--> | ||
|
||
## Server Side Fetching | ||
|
||
There are three primary ways with the Next.js Pages Router to fetch data on the server: | ||
|
||
1. Static site generation (SSG) with `getStaticProps` | ||
2. Hybrid static site generation with incremental static regeneration (ISR) enabled in `getStaticProps` | ||
3. Server-side data fetching with `getServerSideProps` | ||
|
||
### getStaticProps | ||
|
||
We can develop a staticly generated site in Next.js by using `getStaticProps`. Having a statically generated site is great for SEO, as it makes it much easier for Google to index your site compared to a site with complex JavaScript logic, which is harder for web crawlers to understand. Next.js will run the code inside the `getStaticProps` method when you run `npm build`, and generate associated static HTML or JSON data. | ||
|
||
For example, using dynamic routing we can create a static page to show post data based on the URL: | ||
|
||
```tsx | ||
// pages/[slug].tsx | ||
|
||
export const getStaticProps = ({ params }) => { | ||
const id = params.slug; | ||
const res = await fetch(`https://jsonplaceholder.typicode.com/posts/${id}`); | ||
const post = await res.json(); | ||
|
||
return { | ||
props: { post } | ||
}; | ||
} | ||
|
||
export default function Page(props) { | ||
return ( | ||
<div> | ||
<h2>{props.post.title}</h2> | ||
<p>{props.post.body}</p> | ||
</div> | ||
) | ||
} | ||
|
||
``` | ||
|
||
### Incremental Static Regeneration (Hybrid) | ||
|
||
Server-side generation is great for SEO, however if we have a data source that may change between builds, we may need to regenerate the static data generated at build time. With incremental static regeneration (ISR), we can add an expiry time for static data, and Next.js will automatically refetch the data if the expiry time has been reached. However, this will not block the current request where it has noticed that the data has expired with a long loading time - it will fetch the data only for the next request. | ||
|
||
```tsx | ||
|
||
export const getStaticProps = () => { | ||
const res = await fetch(`https://jsonplaceholder.typicode.com/comments`); | ||
const comments = await res.json(); | ||
|
||
return { | ||
props: { comments }, | ||
revalidate: 60 | ||
}; | ||
} | ||
|
||
``` | ||
|
||
This means that if 60 seconds or more has passed after the last time `getStaticProps` was run and the user makes a request to the page, it will rerun the code inside getStaticProps, and render the newly fetched data for the next page visitor. | ||
|
||
### getServerSideProps | ||
|
||
`getServerSideProps` allows for server-side fetching of data on each request from the client, which makes it great for fetching of dynamic data. It can also be used for secured data, as the code within the function only runs on the server. | ||
|
||
The below example shows an example of how we can use `getServerSideProps` to fetch data. Upon each user's request, the server will fetch the list of posts and pass it as props to the page. | ||
|
||
```tsx | ||
// pages/index.tsx | ||
|
||
export const getServerSideProps = async (context) => { | ||
const res = await fetch("https://jsonplaceholder.typicode.com/posts"); | ||
const posts = await res.json(); | ||
|
||
return { props: { posts } }; | ||
} | ||
|
||
export default function Page(props) { | ||
return ( | ||
<div> | ||
{props.posts.map(post => ( | ||
<div> | ||
<h2>{post.title}</h2> | ||
<p>{post.body}</p> | ||
</div> | ||
))} | ||
</div> | ||
) | ||
} | ||
``` | ||
|
||
This is great for dynamic data that may not be best suited for `getStaticProps` such as fetching from a database or an API route with data that changes often. | ||
|
||
The `context` parameter also has a lot of useful information about the request, including the request path, cookies sent from the client, and more that can be found on the [official Next.js documentation](https://nextjs.org/docs/pages/api-reference/functions/get-server-side-props#context-parameter). | ||
|
||
You can use [https://next-code-elimination.vercel.app/](https://next-code-elimination.vercel.app/) to verify what code is sent to the client when using `getServerSideProps`. | ||
|
||
|
||
## Client Side Fetching | ||
|
||
If you want to fetch secured data from a component (not a page) without exposing confidential information to the user (e.g. keys, IDs), the best way to do this is to create a basic API route to fetch this data, which allows for storage of sensitive information on the server, unable to be exposed to the client. | ||
|
||
This would be written in the component like so: | ||
|
||
```tsx | ||
|
||
const Component = () => { | ||
const [data, setData] = useState(null) | ||
|
||
useEffect(() => { | ||
fetch("/api/your-api-route") | ||
.then(res => res.json()) | ||
.then(data => { | ||
setData(data) | ||
}) | ||
}, []) | ||
|
||
return ( | ||
<> ... </> | ||
) | ||
} | ||
|
||
|
||
``` | ||
|
||
Then place a file in the /pages/api directory named with the required API route path (i.e. `pages/api/{{ API_ROUTE_HERE }}.ts`): | ||
|
||
```ts | ||
// pages/api/your-api-route.ts | ||
|
||
import { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) { | ||
if (req.method == "GET") { | ||
const res = await fetch("https://jsonplaceholder.typicode.com/posts"); | ||
const data = await res.json(); | ||
|
||
res.status(200).send(data); | ||
} else { | ||
res.status(405).json({ error: "Unsupported method" }); | ||
} | ||
} | ||
``` | ||
|
||
This is a great workaround for the limitation of only being able to use the above server-side fetching functions at a page-level - as it allows for server-side fetching from components. However, keep in mind that this may result in performance impacts from blocking calls to API routes. | ||
|
||
This is also a great way to reduce the occurrence of CORS errors, as you can proxy API data through a simple Next.js API route. |
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,35 @@ | ||
--- | ||
type: rule | ||
title: Do you make your Google Maps profile look awesome? | ||
uri: google-maps-profile | ||
authors: | ||
- title: Seth Daily | ||
url: https://www.ssw.com.au/people/seth-daily | ||
related: null | ||
created: 2023-07-30 | ||
archivedreason: null | ||
guid: d60a52fb-9b90-4abf-812e-0696ec4697c3 | ||
--- | ||
|
||
Ever landed on a business's Google Maps profile only to find scant information, low quality images, or outdated contact details? If so, you'll have probably felt some distaste. You'll also understand the importance of creating an impressive and informative Google Maps profile. So, what's the best way to do that? | ||
|
||
<!--endintro--> | ||
|
||
### Compelling Images | ||
Images are the first thing that someone looks at when clicking on a Google Maps listing. High-resolution photos make your profile appealing, and can be the difference between someone contacting you or bouncing from your page. These could include images of your products, services, team, or business premises. | ||
|
||
1. Take high quality pictures - This is the most important part. If your images are blurry or low resolution, viewers immediately start off on the wrong foot with you. | ||
2. Take pictures of the building - Google Maps is full of fake businesses and outdated listings. Having images of the exterior of the building confirms quickly to somebody clicking on your listing that you are legitimate. | ||
3. Add images of interesting features - The cherry on top is to add pictures of interesting features of the office that make it seem personable. | ||
|
||
|
||
### Profile Information | ||
Make sure your business information is up to date. The important fields are: | ||
|
||
* Business name | ||
* Address | ||
* Contact information | ||
* Business hours | ||
|
||
### Respond to Reviews | ||
It shows a commitment to customer satisfaction if you respond to reviews (especially negative ones). It's important to periodically respond to reviews so that anybody scanning your profile sees that activity. |