_app.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import type { ReactElement, ReactNode } from "react";
  2. import { SWRConfig } from "swr";
  3. import type { NextPage } from "next";
  4. import App, { AppContext } from "next/app";
  5. import type { AppProps } from "next/app";
  6. import Layout from "../components/common/Layout";
  7. import "../styles/globals.scss";
  8. import useGet from "../utils/hooks/useGet";
  9. import { GenreItem } from "../types/http";
  10. import { get } from "../utils/http";
  11. import { Context } from "../libs/context";
  12. export type NextPageWithLayout<P = {}, IP = P> = NextPage<P, IP> & {
  13. getLayout?: (page: ReactElement) => ReactNode;
  14. };
  15. type AppPropsWithLayout = AppProps & {
  16. Component: NextPageWithLayout;
  17. pageProps: {
  18. fallback?: {
  19. [key: string]: any;
  20. };
  21. [key: string]: any;
  22. };
  23. };
  24. const MyApp = ({ Component, pageProps }: AppPropsWithLayout) => {
  25. const { fallback, genre, ...otherProps } = pageProps;
  26. return (
  27. <Context.Provider value={{ genre }}>
  28. <SWRConfig value={{ fallback, revalidateIfStale: false }}>
  29. {Component.getLayout ? (
  30. Component.getLayout(<Component {...otherProps} />)
  31. ) : (
  32. <Layout>
  33. <Component {...otherProps} />
  34. </Layout>
  35. )}
  36. </SWRConfig>
  37. </Context.Provider>
  38. );
  39. };
  40. MyApp.getInitialProps = async function (context: AppContext) {
  41. App.getInitialProps(context);
  42. const { data } = await fetch("https://novels.yergoo.com/api/genre/list").then(
  43. (res) => res.json()
  44. );
  45. return {
  46. pageProps: { genre: data },
  47. };
  48. };
  49. export default MyApp;