Skip to content

Commit

Permalink
feat: add sitemap and all show routes to map
Browse files Browse the repository at this point in the history
This patch adds a sitemap that is dynamically generated by
`remix-sitemap`. This allows us to ensure that all the shows in our
database are properly added to it.

Ref: https://github.com/fedeya/remix-sitemap
Ref: https://stackoverflow.com/a/31354426
Ref: https://developers.google.com/search/docs/crawling-indexing/sitemaps/build-sitemap

Closes: NC-681
  • Loading branch information
nicholaschiang committed Jul 29, 2023
1 parent 6f892a9 commit c881173
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 46 deletions.
9 changes: 9 additions & 0 deletions app/entry.server.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import { RemixServer } from '@remix-run/react'
import type { EntryContext } from '@vercel/remix'
import { handleRequest } from '@vercel/remix'
import { createSitemapGenerator } from 'remix-sitemap'

import { BASE_URL } from 'utils'

const { isSitemapUrl, sitemap } = createSitemapGenerator({
siteUrl: BASE_URL,
generateRobotsTxt: true,
})

export default function entry(
request: Request,
responseStatusCode: number,
responseHeaders: Headers,
remixContext: EntryContext,
) {
if (isSitemapUrl(request)) return sitemap(request, remixContext)
responseHeaders.set('Cache-Control', 's-maxage=1, stale-while-revalidate=59')
return handleRequest(
request,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
type SerializeFrom,
type V2_MetaFunction,
} from '@vercel/remix'
import { type SitemapFunction } from 'remix-sitemap'

import { prisma } from 'db.server'
import { log } from 'log.server'
Expand Down Expand Up @@ -48,6 +49,17 @@ export const meta: V2_MetaFunction<typeof loader> = ({ data }) => {
]
}

export const sitemap: SitemapFunction = async () => {
const shows = await prisma.show.findMany({
include: { season: true, brand: true },
orderBy: { name: 'asc' },
})
return shows.map((show) => ({
loc: getShowPath(show),
lastmod: show.updatedAt.toISOString(),
}))
}

export const handle: Handle = {
breadcrumb: (match) => {
const data = match.data as SerializeFrom<typeof loader> | undefined
Expand Down
4 changes: 2 additions & 2 deletions app/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import type { User } from 'models/user.server'

import type { loader } from 'root'

const DEFAULT_REDIRECT = '/'
const BASE_URL = 'https://nicholas.engineering'
export const DEFAULT_REDIRECT = '/'
export const BASE_URL = 'https://nicholas.engineering'

export const clone = rfdc()

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-hotkeys-hook": "^4.4.1",
"remix-sitemap": "^2.2.0",
"rfdc": "^1.3.0",
"schema-dts": "^1.1.2",
"sharp": "^0.32.4",
Expand Down
Loading

1 comment on commit c881173

@vercel
Copy link

@vercel vercel bot commented on c881173 Jul 29, 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.