-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# (Don't merge!) ## What's the purpose of this pull request? This POC is an alternative to indexing search pages using [server side rendering](https://nextjs.org/docs/pages/building-your-application/data-fetching/get-server-side-props). As the search page is dynamic and we need to set the title and description based on the search term, we chose to use server side rendering as it is the most suitable for this type of situation. ### Nextjs reference: **Like SSG,[ Server-Side Rendering](https://nextjs.org/docs/basic-features/pages#server-side-rendering) (SSR) is pre-rendered, which also makes it great for SEO. Instead of being generated at build time, as in SSG, SSR's HTML is generated at request time. This is great for when you have pages that are very dynamic.** https://nextjs.org/learn-pages-router/seo/rendering-and-ranking/rendering-strategies ## How it works? We use the getServerSideProps function instead of getStaticProps. We added a cache header to the response so that it is possible to cache the return in a CDN. ## How to test it? Click on: https://storeframework-cm652ufll028lmgv665a6xv0g-polemunmz.b.vtex.app/s?q=headphone
- Loading branch information
Showing
6 changed files
with
198 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
packages/core/src/experimental/searchServerSideFunctions/getServerSideProps.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, stale-if-error=31536000' | ||
) // 5 minutes of fresh content and 1 year of stale content | ||
|
||
return { | ||
props: { | ||
page, | ||
globalSections, | ||
searchTerm, | ||
}, | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
packages/core/src/experimental/searchServerSideFunctions/getStaticProps.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
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 | ||
} | ||
|
||
/* | ||
Depending on the value of the storeConfig.experimental.enableSearchSSR flag, the function used will be getServerSideProps (./getServerSideProps). | ||
Our CLI that does this process of converting from getStaticProps to getServerSideProps. | ||
*/ | ||
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, | ||
}, | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
packages/core/src/experimental/searchServerSideFunctions/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './getServerSideProps' | ||
export * from './getStaticProps' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters