Browse Source

add comment

main
shigeo.shinohara 2 years ago
parent
commit
1e2c9b844a
3 changed files with 26 additions and 5 deletions
  1. +12
    -5
      lib/strapi.ts
  2. +7
    -0
      pages/ssg.tsx
  3. +7
    -0
      pages/ssr.tsx

+ 12
- 5
lib/strapi.ts View File

import {Guitar} from "../type/guitars";
import { Guitar } from "../type/guitars";


/**
* Guitar情報取得処理
*/
export const fetchGuitars = async (): Promise<Guitar[]> => { export const fetchGuitars = async (): Promise<Guitar[]> => {
const response = await fetch(`${process.env.NEXT_PUBLIC_STRAPI_URL}/api/guitars?populate=*`)
const guitars:Guitar[] =await response.json().then((json) => json.data)
return guitars
}
// StrapiのAPIを呼び出し
const response = await fetch(
`${process.env.NEXT_PUBLIC_STRAPI_URL}/api/guitars?populate=*`
);
// apiのresponseからdata部のみ取り出し
const guitars: Guitar[] = await response.json().then((json) => json.data);
return guitars;
};

+ 7
- 0
pages/ssg.tsx View File

import { GetStaticProps } from "next"; import { GetStaticProps } from "next";
import { Guitar, GuitarResponse } from "../type/guitars"; import { Guitar, GuitarResponse } from "../type/guitars";


// ===========================
// getStaticPropsのpropsに設定したデータを引数で受け取れる
// ==========================
const Ssg: FC<GuitarResponse> = ({ guitars }) => { const Ssg: FC<GuitarResponse> = ({ guitars }) => {
return ( return (
<div> <div>
); );
}; };


// ===============================
// 静的サイト作成時にはgetStaticPropsの中で
// 表示データを作成する
// ===============================
export const getStaticProps: GetStaticProps = async () => { export const getStaticProps: GetStaticProps = async () => {
const guitars = await fetchGuitars(); const guitars = await fetchGuitars();
return { return {

+ 7
- 0
pages/ssr.tsx View File

import { GetServerSideProps } from "next"; import { GetServerSideProps } from "next";
import { Guitar, GuitarResponse } from "../type/guitars"; import { Guitar, GuitarResponse } from "../type/guitars";


// ===========================
// getServerSidePropsのpropsに設定したデータを引数で受け取れる
// ==========================
const Ssr: FC<GuitarResponse> = ({ guitars }) => { const Ssr: FC<GuitarResponse> = ({ guitars }) => {
return ( return (
<div> <div>
); );
}; };


// ===============================
// サーバサイドレンダリング時にはgetServerSidePropsの中で
// 表示データを作成する
// ===============================
export const getServerSideProps: GetServerSideProps = async () => { export const getServerSideProps: GetServerSideProps = async () => {
const guitars = await fetchGuitars(); const guitars = await fetchGuitars();
return { return {

Loading…
Cancel
Save