_app.tsx 1.4 KB

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