From 502bc3a4ff6cb499f05908823afc3bc46df663b5 Mon Sep 17 00:00:00 2001 From: Kevin Wang Date: Fri, 23 Jun 2023 17:57:49 -0400 Subject: [PATCH] chore: remove `App.getInitialProps` (#2021) * chore: remove `App.getInitialProps` * fix type * remove host --- src/components/head-metadata/index.tsx | 2 +- src/components/head-metadata/types.ts | 5 ---- src/lib/get-deployed-url.ts | 4 ++-- src/pages/_app.tsx | 32 ++++---------------------- src/types/_app.ts | 13 ++--------- 5 files changed, 9 insertions(+), 47 deletions(-) diff --git a/src/components/head-metadata/index.tsx b/src/components/head-metadata/index.tsx index 62979d2804..392f64423b 100644 --- a/src/components/head-metadata/index.tsx +++ b/src/components/head-metadata/index.tsx @@ -61,7 +61,7 @@ export default function HeadMetadata(props: HeadMetadataProps) { ) const ogImagePath = props.localOgImage || `${productSlug ?? 'base'}.jpg` - const ogImageUrl = `${getDeployedUrl(props.host)}/og-image/${ogImagePath}` + const ogImageUrl = `${getDeployedUrl()}/og-image/${ogImagePath}` return ( // TODO: OpenGraph image to be passed as the image prop here diff --git a/src/components/head-metadata/types.ts b/src/components/head-metadata/types.ts index 9557a2b1eb..db34b43061 100644 --- a/src/components/head-metadata/types.ts +++ b/src/components/head-metadata/types.ts @@ -14,11 +14,6 @@ export interface HeadMetadataProps { */ description?: string - /** - * Optional host value, such as 'localhost:3000', used during development - */ - host?: string - /** * Optional custom image path to use as the og-image, relative to * the `/public/og-image` folder. diff --git a/src/lib/get-deployed-url.ts b/src/lib/get-deployed-url.ts index 357bcf7b64..371791637a 100644 --- a/src/lib/get-deployed-url.ts +++ b/src/lib/get-deployed-url.ts @@ -9,14 +9,14 @@ * * Returns an empty string in development. */ -export default function getDeployedUrl(host?: string) { +export default function getDeployedUrl() { // preview deployments should derive the url from Vercel's env var if (process.env.HASHI_ENV === 'preview') { return `https://${process.env.NEXT_PUBLIC_VERCEL_URL}` } if (process.env.HASHI_ENV === 'development') { - return host ? `http://${host}` : '' + return process.env.HOST_NAME || `http://localhost:3000` } // use our canonical URL for production diff --git a/src/pages/_app.tsx b/src/pages/_app.tsx index 29b3eeb279..b865ef539a 100644 --- a/src/pages/_app.tsx +++ b/src/pages/_app.tsx @@ -4,7 +4,7 @@ */ // Third-party imports -import React, { useEffect, useState, ComponentType } from 'react' +import React, { useEffect, useState } from 'react' import dynamic from 'next/dynamic' import { SSRProvider } from '@react-aria/ssr' import { ErrorBoundary } from 'react-error-boundary' @@ -28,7 +28,7 @@ import useAnchorLinkAnalytics from '@hashicorp/platform-util/anchor-link-analyti import CodeTabsProvider from '@hashicorp/react-code-block/provider' // Global imports -import type { CustomAppProps, CustomAppContext } from 'types/_app' +import type { CustomAppProps } from 'types/_app' import { CurrentContentTypeProvider, CurrentProductProvider, @@ -69,8 +69,7 @@ addGlobalLinkHandler((destinationUrl: string) => { export default function App({ Component, pageProps: { session, ...pageProps }, - host, -}: CustomAppProps & Awaited>) { +}: CustomAppProps) { const flagBag = useFlags() useAnchorLinkAnalytics() useEffect(() => makeDevAnalyticsLogger(), []) @@ -113,7 +112,7 @@ export default function App({ - + import('lib/framer-motion-features').then( @@ -143,26 +142,3 @@ export default function App({ ) } - -App.getInitialProps = async ({ - Component, - ctx, -}: CustomAppContext<{ Component: ComponentType }>) => { - let pageProps = {} - - if (Component.getInitialProps) { - pageProps = await Component.getInitialProps(ctx) - } - - let host - if (ctx.req) { - host = ctx.req.headers.host - } else { - host = window.location.host - } - - return { - pageProps, - host, - } -} diff --git a/src/types/_app.ts b/src/types/_app.ts index 973d6d1ed8..3229be7e42 100644 --- a/src/types/_app.ts +++ b/src/types/_app.ts @@ -5,7 +5,7 @@ import { FC } from 'react' import type { NextPage } from 'next' -import type { AppProps, AppContext } from 'next/app' +import type { AppProps } from 'next/app' import { CurrentContentType } from 'contexts' import { GlobalThemeOption } from 'styles/themes/types' @@ -22,18 +22,9 @@ type ContextWithLayout = { Component: CustomPageComponent } -type AppContextNoComponent = Omit - -/** - * This is our custom param type for `App.getInitialProps` - */ -type CustomAppContext> = AppContextNoComponent & - ContextWithLayout & - T - /** * This is our custom type for our Next.js `App` component */ type CustomAppProps = AppProps<$TSFixMe> & ContextWithLayout -export type { CustomPageComponent, CustomAppContext, CustomAppProps } +export type { CustomPageComponent, CustomAppProps }