Skip to content

Commit

Permalink
Add blocking fallback to pages (vercel#357)
Browse files Browse the repository at this point in the history
* Update [...pages].tsx

* Fix provider config overwrite

* Shopify changes
  • Loading branch information
cond0r authored Jun 7, 2021
1 parent 0e804d0 commit 2d0c6e0
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 12 deletions.
2 changes: 1 addition & 1 deletion framework/commerce/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function withCommerceConfig(nextConfig = {}) {
}

const commerceNextConfig = require(path.join('../', name, 'next.config'))
const config = merge(commerceNextConfig, nextConfig)
const config = merge(nextConfig, commerceNextConfig)

config.env = config.env || {}

Expand Down
4 changes: 3 additions & 1 deletion framework/shopify/api/operations/get-all-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ export default function getAllPagesOperation({
preview?: boolean
query?: string
} = {}): Promise<T['data']> {
const { fetch, locale, locales = ['en-US'] } = commerce.getConfig(config)
const { fetch, locale, locales = ['en-US', 'es'] } = commerce.getConfig(
config
)

const { data } = await fetch<GetAllPagesQuery, GetAllPagesQueryVariables>(
query,
Expand Down
2 changes: 1 addition & 1 deletion framework/shopify/api/operations/get-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default function getPageOperation({
config?: Partial<ShopifyConfig>
preview?: boolean
}): Promise<T['data']> {
const { fetch, locale = 'en-US' } = commerce.getConfig(config)
const { fetch, locale } = commerce.getConfig(config)

const {
data: { node: page },
Expand Down
6 changes: 3 additions & 3 deletions framework/shopify/product/use-search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,18 @@ export const handler: SWRHook<SearchProductsHook> = {
})
// filter on client when brandId & categoryId are set since is not available on collection product query
products = brandId
? data.node.products.edges.filter(
? data.node?.products?.edges?.filter(
({ node: { vendor } }: ProductEdge) =>
vendor.replace(/\s+/g, '-').toLowerCase() === brandId
)
: data.node.products.edges
: data.node?.products?.edges
} else {
const data = await fetch<GetAllProductsQuery>({
query: options.query,
method,
variables,
})
products = data.products.edges
products = data.products?.edges
}

return {
Expand Down
4 changes: 2 additions & 2 deletions framework/shopify/utils/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,14 @@ function normalizeLineItem({

export const normalizePage = (
{ title: name, handle, ...page }: ShopifyPage,
locale: string
locale: string = 'en-US'
): Page => ({
...page,
url: `/${locale}/${handle}`,
name,
})

export const normalizePages = (edges: PageEdge[], locale: string): Page[] =>
export const normalizePages = (edges: PageEdge[], locale?: string): Page[] =>
edges?.map((edge) => normalizePage(edge.node, locale))

export const normalizeCategory = ({
Expand Down
12 changes: 8 additions & 4 deletions pages/[...pages].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Text } from '@components/ui'
import { Layout } from '@components/common'
import getSlug from '@lib/get-slug'
import { missingLocaleInPages } from '@lib/usage-warns'
import { useRouter } from 'next/router'

export async function getStaticProps({
preview,
Expand All @@ -28,6 +29,7 @@ export async function getStaticProps({
config,
preview,
}))

const page = data?.page

if (!page) {
Expand Down Expand Up @@ -58,16 +60,18 @@ export async function getStaticPaths({ locales }: GetStaticPathsContext) {

return {
paths,
// Fallback shouldn't be enabled here or otherwise this route
// will catch every page, even 404s, and we don't want that
fallback: false,
fallback: 'blocking',
}
}

export default function Pages({
page,
}: InferGetStaticPropsType<typeof getStaticProps>) {
return (
const router = useRouter()

return router.isFallback ? (
<h1>Loading...</h1> // TODO (BC) Add Skeleton Views
) : (
<div className="max-w-2xl mx-8 sm:mx-auto py-20">
{page?.body && <Text html={page.body} />}
</div>
Expand Down

0 comments on commit 2d0c6e0

Please sign in to comment.