Skip to content

Commit

Permalink
feat: cli new approach
Browse files Browse the repository at this point in the history
  • Loading branch information
pedromtec committed Jan 27, 2025
1 parent 268e272 commit 235056a
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 110 deletions.
59 changes: 1 addition & 58 deletions packages/cli/src/utils/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,56 +488,6 @@ function enableRedirectsMiddleware(basePath: string) {
}
}

const GET_SERVER_SIDE_PROPS = `
export const getServerSideProps: GetServerSideProps<
Props,
Record<string, string>,
Locator
> = async (context) => {
const { previewData, query, res } = context
const searchTerm = (query.q as string)?.split('+').join(' ')
const globalSections = await getGlobalSectionsData(previewData)
if (storeConfig.cms.data) {
const cmsData = JSON.parse(storeConfig.cms.data)
const page = cmsData['search'][0]
if (page) {
const pageData = await getPage<SearchContentType>({
contentType: 'search',
documentId: page.documentId,
versionId: page.versionId,
})
return {
props: { page: pageData, globalSections, searchTerm },
}
}
}
const page = await getPage<SearchContentType>({
...(previewData?.contentType === 'search' ? previewData : null),
contentType: 'search',
})
res.setHeader(
'Cache-Control',
'public, s-maxage=300, stale-while-revalidate=31536000'
) // 5 minutes of fresh content and 1 year of stale content
return {
props: {
page,
globalSections,
searchTerm,
},
}
}
`

const GET_STATIC_PROPS_REGEX = /export const getStaticProps: GetStaticProps<[\s\S]*?>\s*= async \(context\) => \{[\s\S]*?\}/


function enableSearchSSR(basePath: string) {
Expand All @@ -552,19 +502,12 @@ function enableSearchSSR(basePath: string) {
}

const { tmpDir } = withBasePath(basePath)

const searchPagePath = path.join(tmpDir, 'src', 'pages', 's.tsx')

const searchPageData = String(readFileSync(searchPagePath))
let searchPageWithSSR = searchPageData.replace(
GET_STATIC_PROPS_REGEX,
GET_SERVER_SIDE_PROPS
)

searchPageWithSSR.replace("import type { GetStaticProps } from 'next'", "import type { GetServerSideProps } from 'next'")
const searchPageWithSSR = searchPageData.replaceAll('getStaticProps', 'getServerSideProps')

writeFileSync(searchPagePath, searchPageWithSSR)

}

export async function generate(options: GenerateOptions) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { GetServerSideProps } from 'next'
import { SearchPageProps } from './getStaticProps'

import { getGlobalSectionsData } from 'src/components/cms/GlobalSections'
import { SearchContentType, getPage } from 'src/server/cms'
import { Locator } from '@vtex/client-cms'
import storeConfig from 'discovery.config'

export const getServerSideProps: GetServerSideProps<
SearchPageProps,
Record<string, string>,
Locator
> = async (context) => {
const { previewData, query, res } = context
const searchTerm = (query.q as string)?.split('+').join(' ')

const globalSections = await getGlobalSectionsData(previewData)

if (storeConfig.cms.data) {
const cmsData = JSON.parse(storeConfig.cms.data)
const page = cmsData['search'][0]
if (page) {
const pageData = await getPage<SearchContentType>({
contentType: 'search',
documentId: page.documentId,
versionId: page.versionId,
})
return {
props: { page: pageData, globalSections, searchTerm },
}
}
}

const page = await getPage<SearchContentType>({
...(previewData?.contentType === 'search' ? previewData : null),
contentType: 'search',
})

res.setHeader(
'Cache-Control',
'public, s-maxage=300, stale-while-revalidate=31536000'
) // 5 minutes of fresh content and 1 year of stale content

return {
props: {
page,
globalSections,
searchTerm,
},
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { GetStaticProps } from 'next'
import {
getGlobalSectionsData,
GlobalSectionsData,
} from 'src/components/cms/GlobalSections'
import { SearchContentType, getPage } from 'src/server/cms'
import { Locator } from '@vtex/client-cms'
import storeConfig from 'discovery.config'

export type SearchPageProps = {
page: SearchContentType
globalSections: GlobalSectionsData
searchTerm?: string
}

export const getStaticProps: GetStaticProps<
SearchPageProps,
Record<string, string>,
Locator
> = async (context) => {
const { previewData } = context

const globalSections = await getGlobalSectionsData(previewData)

if (storeConfig.cms.data) {
const cmsData = JSON.parse(storeConfig.cms.data)
const page = cmsData['search'][0]

if (page) {
const pageData = await getPage<SearchContentType>({
contentType: 'search',
documentId: page.documentId,
versionId: page.versionId,
})

return {
props: { page: pageData, globalSections },
}
}
}

const page = await getPage<SearchContentType>({
...(previewData?.contentType === 'search' ? previewData : null),
contentType: 'search',
})

return {
props: {
page,
globalSections,
},
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './getServerSideProps'
export * from './getStaticProps'
63 changes: 11 additions & 52 deletions packages/core/src/pages/s.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { GetStaticProps } from 'next'
import { NextSeo } from 'next-seo'
import { useRouter } from 'next/router'
import { useMemo } from 'react'
Expand All @@ -14,20 +13,13 @@ import { SROnly as UISROnly } from '@faststore/ui'
import { ITEMS_PER_PAGE } from 'src/constants'
import { useApplySearchState } from 'src/sdk/search/state'

import { Locator } from '@vtex/client-cms'
import storeConfig from 'discovery.config'
import {
getGlobalSectionsData,
GlobalSectionsData,
} from 'src/components/cms/GlobalSections'
import { SearchWrapper } from 'src/components/templates/SearchPage'
import { getPage, SearchContentType } from 'src/server/cms'

type Props = {
page: SearchContentType
globalSections: GlobalSectionsData
searchTerm?: string
}
import { SearchWrapper } from 'src/components/templates/SearchPage'
import {
getStaticProps,
SearchPageProps,
} from 'src/experimental/searchServerSideFunctions'

export interface SearchPageContextType {
title: string
Expand Down Expand Up @@ -55,7 +47,11 @@ const useSearchParams = ({
}, [asPath, defaultSort])
}

function Page({ page: searchContentType, globalSections, searchTerm }: Props) {
function Page({
page: searchContentType,
globalSections,
searchTerm,
}: SearchPageProps) {
const { settings } = searchContentType
const applySearchState = useApplySearchState()
const searchParams = useSearchParams({
Expand Down Expand Up @@ -129,43 +125,6 @@ function Page({ page: searchContentType, globalSections, searchTerm }: Props) {
)
}

export const getStaticProps: GetStaticProps<
Props,
Record<string, string>,
Locator
> = async (context) => {
const { previewData } = context

const globalSections = await getGlobalSectionsData(previewData)

if (storeConfig.cms.data) {
const cmsData = JSON.parse(storeConfig.cms.data)
const page = cmsData['search'][0]

if (page) {
const pageData = await getPage<SearchContentType>({
contentType: 'search',
documentId: page.documentId,
versionId: page.versionId,
})

return {
props: { page: pageData, globalSections },
}
}
}

const page = await getPage<SearchContentType>({
...(previewData?.contentType === 'search' ? previewData : null),
contentType: 'search',
})

return {
props: {
page,
globalSections,
},
}
}
export { getStaticProps }

export default Page

0 comments on commit 235056a

Please sign in to comment.