Skip to content

Commit

Permalink
chore: remove App.getInitialProps (#2021)
Browse files Browse the repository at this point in the history
* chore: remove `App.getInitialProps`

* fix type

* remove host
  • Loading branch information
thiskevinwang authored Jun 23, 2023
1 parent 38d01a2 commit 502bc3a
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/components/head-metadata/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions src/components/head-metadata/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/lib/get-deployed-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 4 additions & 28 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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,
Expand Down Expand Up @@ -69,8 +69,7 @@ addGlobalLinkHandler((destinationUrl: string) => {
export default function App({
Component,
pageProps: { session, ...pageProps },
host,
}: CustomAppProps & Awaited<ReturnType<(typeof App)['getInitialProps']>>) {
}: CustomAppProps) {
const flagBag = useFlags()
useAnchorLinkAnalytics()
useEffect(() => makeDevAnalyticsLogger(), [])
Expand Down Expand Up @@ -113,7 +112,7 @@ export default function App({
<DeviceSizeProvider>
<CurrentProductProvider currentProduct={currentProduct}>
<CodeTabsProvider>
<HeadMetadata {...pageProps.metadata} host={host} />
<HeadMetadata {...pageProps.metadata} />
<LazyMotion
features={() =>
import('lib/framer-motion-features').then(
Expand Down Expand Up @@ -143,26 +142,3 @@ export default function App({
</QueryClientProvider>
)
}

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,
}
}
13 changes: 2 additions & 11 deletions src/types/_app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -22,18 +22,9 @@ type ContextWithLayout = {
Component: CustomPageComponent
}

type AppContextNoComponent = Omit<AppContext, 'Component'>

/**
* This is our custom param type for `App.getInitialProps`
*/
type CustomAppContext<T = Record<string, unknown>> = 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 }

1 comment on commit 502bc3a

@vercel
Copy link

@vercel vercel bot commented on 502bc3a Jun 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.