Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Footer adjustments #596

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions api/sitemap.xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NextApiRequest, NextApiResponse } from 'next'

import { SiteMap } from '../lib/types'
import { host } from '../lib/config'
import { getSiteMaps } from '../lib/get-site-maps'
import { getSiteMap } from '../lib/get-site-map'

export default async (
req: NextApiRequest,
Expand All @@ -12,15 +12,15 @@ export default async (
return res.status(405).send({ error: 'method not allowed' })
}

const siteMaps = await getSiteMaps()
const siteMap = await getSiteMap()

// cache sitemap for up to one hour
res.setHeader(
'Cache-Control',
'public, s-maxage=3600, max-age=3600, stale-while-revalidate=3600'
)
res.setHeader('Content-Type', 'text/xml')
res.write(createSitemap(siteMaps[0]))
res.write(createSitemap(siteMap))
res.end()
}

Expand Down
3 changes: 1 addition & 2 deletions components/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ export const Footer: React.FC<{

return (
<footer className={styles.footer}>
<div className={styles.copyright}>Copyright 2021 {config.author}</div>

<div className={styles.copyright}>Copyright {new Date().getFullYear()} {config.author}</div>
{hasMounted ? (
<div className={styles.settings}>
<a
Expand Down
12 changes: 6 additions & 6 deletions components/NotionPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { ReactUtterances } from './ReactUtterances'

import styles from './styles.module.css'

// NOTE: if your site doesn't use these, then we recommend switching them to load lazily
// const Code = dynamic(() =>
// import('react-notion-x').then((notion) => notion.Code)
// )
Expand All @@ -59,10 +60,6 @@ const Equation = dynamic(() =>
import('react-notion-x').then((notion) => notion.Equation)
)

// we're now using a much lighter-weight tweet renderer react-static-tweets
// instead of the official iframe-based embed widget from twitter
// const Tweet = dynamic(() => import('react-tweet-embed'))

const Modal = dynamic(
() => import('react-notion-x').then((notion) => notion.Modal),
{ ssr: false }
Expand Down Expand Up @@ -108,7 +105,7 @@ export const NotionPage: React.FC<types.PageProps> = ({
})

if (!config.isServer) {
// add important objects to the window global for easy debugging
// add important variables to the global window object for easy debugging
const g = window as any
g.pageId = pageId
g.recordMap = recordMap
Expand All @@ -122,6 +119,9 @@ export const NotionPage: React.FC<types.PageProps> = ({

// const isRootPage =
// parsePageId(block.id) === parsePageId(site.rootNotionPageId)

// this is all very customizable logic depending on the contents and
// structure of your site
const isBlogPost =
block.type === 'page' && block.parent_table === 'collection'
const showTableOfContents = !!isBlogPost
Expand Down Expand Up @@ -170,7 +170,7 @@ export const NotionPage: React.FC<types.PageProps> = ({
}
}}
>
<PageHead site={site} />
<PageHead />

<Head>
<meta property='og:title' content={title} />
Expand Down
4 changes: 2 additions & 2 deletions components/Page404.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { PageHead } from './PageHead'
import styles from './styles.module.css'

export const Page404: React.FC<types.PageProps> = ({ site, pageId, error }) => {
const title = site?.name || 'Notion Page Not Found'
const title = site?.name ? `${site.name} Page Not Found` : 'Page Not Found'

return (
<>
<PageHead site={site} />
<PageHead />

<Head>
<meta property='og:site_name' content={title} />
Expand Down
17 changes: 2 additions & 15 deletions components/PageHead.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import React from 'react'
import Head from 'next/head'
import * as React from 'react'
import * as types from 'lib/types'

// TODO: remove duplication between PageHead and NotionPage Head

export const PageHead: React.FC<types.PageProps> = ({ site }) => {
export const PageHead = () => {
return (
<Head>
<meta charSet='utf-8' />
Expand All @@ -13,16 +10,6 @@ export const PageHead: React.FC<types.PageProps> = ({ site }) => {
name='viewport'
content='width=device-width, initial-scale=1, shrink-to-fit=no'
/>

{site?.description && (
<>
<meta name='description' content={site.description} />
<meta property='og:description' content={site.description} />
</>
)}

<meta name='theme-color' content='#EB625A' />
<meta property='og:type' content='website' />
</Head>
)
}
5 changes: 5 additions & 0 deletions lib/canonical-page-map-cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { CanonicalPageMap } from './types'

export const getCanonicalPageMapCache = async (): Promise<CanonicalPageMap> => {
// TODO
}
99 changes: 99 additions & 0 deletions lib/cloudflare-kv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import fetch from 'node-fetch'

export class CloudflareKV {
accountId: string
apiKey: string
namespaceId: string
_headers: any

constructor({
accountId,
apiKey,
namespaceId
}: {
accountId: string
apiKey: string
namespaceId: string
}) {
this.accountId = accountId
this.apiKey = apiKey
this.namespaceId = namespaceId

this._headers = {
Authorization: `Bearer ${this.apiKey}`
}
}

async get(key: string) {
const url = this._getUrl(key)
const headers = this._headers

const res = await fetch(url, {
headers
})

if (res.ok) {
return res.text()
} else if (res.status === 404) {
return undefined
} else {
const body = await res.text()

throw new Error(`Error ${res.status} ${body}`)
}
}

async put(
key: string,
value,
{
expiration,
expirationTtl
}: {
expiration?: number
expirationTtl?: number
} = {}
) {
const url = new URL(this._getUrl(key))
const headers = this._headers
const query: any = {}

if (expiration) {
query.expiration = expiration
}

if (expirationTtl) {
query.expiration_ttl = Math.max(60, expirationTtl)
}

url.search = new URLSearchParams(query).toString()
const res = await fetch(url.toString(), {
method: 'PUT',
body: value,
headers
})

console.log('CLOUDFLARE PUT', { key, value, ok: res.ok })
if (!res.ok) {
console.log(await res.text())
}
return res.ok
}

async delete(key: string): Promise<boolean> {
const url = this._getUrl(key)
const headers = this._headers

const res = await fetch(url, {
method: 'DELETE',
headers
})

return res.ok
}

_getUrl(key: string) {
const { accountId, namespaceId } = this
return `https://api.cloudflare.com/client/v4/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${key}`
}
}
31 changes: 27 additions & 4 deletions lib/get-all-pages.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pMemoize from 'p-memoize'
import { getAllPagesInSpace } from 'notion-utils'
import stringify from 'fast-json-stable-stringify'

import * as types from './types'
import { includeNotionIdInUrls } from './config'
Expand All @@ -8,16 +9,38 @@ import { getCanonicalPageId } from './get-canonical-page-id'

const uuid = !!includeNotionIdInUrls

export const getAllPages = pMemoize(getAllPagesImpl, { maxAge: 60000 * 5 })
export const getAllPages = pMemoize(getAllPagesImpl, {
maxAge: 60000 * 5,
cacheKey: (args) => stringify(args)
})

export async function getAllPagesImpl(
rootNotionPageId: string,
rootNotionSpaceId: string
): Promise<Partial<types.SiteMap>> {
rootNotionSpaceId: string,
{
concurrency = 4,
pageConcurrency = 3,
full = false,
targetPageId = null
}: {
concurrency?: number
pageConcurrency?: number
full?: boolean
targetPageId?: string
} = {}
): Promise<types.PartialSiteMap> {
const pageMap = await getAllPagesInSpace(
rootNotionPageId,
rootNotionSpaceId,
notion.getPage.bind(notion)
(pageId: string) =>
notion.getPage(pageId, {
signFileUrls: full,
concurrency: pageConcurrency
}),
{
concurrency,
targetPageId
}
)

const canonicalPageMap = Object.keys(pageMap).reduce(
Expand Down
34 changes: 20 additions & 14 deletions lib/get-preview-images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,28 @@ export async function getPreviewImages(
}

const imageDocs = await db.db.getAll(...imageDocRefs)
const results = await pMap(imageDocs, async (model, index) => {
if (model.exists) {
return model.data() as types.PreviewImage
} else {
const json = {
url: images[index],
id: model.id
}
console.log('createPreviewImage server-side', json)
const results = await pMap(
imageDocs,
async (model, index) => {
if (model.exists) {
return model.data() as types.PreviewImage
} else {
const json = {
url: images[index],
id: model.id
}
console.log('createPreviewImage server-side', json)

// TODO: should we fire and forget here to speed up builds?
return got
.post(api.createPreviewImage, { json })
.json() as Promise<types.PreviewImage>
// TODO: should we fire and forget here to speed up builds?
return got
.post(api.createPreviewImage, { json })
.json() as Promise<types.PreviewImage>
}
},
{
concurrency: 16
}
})
)

return results
.filter(Boolean)
Expand Down
31 changes: 31 additions & 0 deletions lib/get-site-map.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { getAllPages } from './get-all-pages'
import { getSiteForDomain } from './get-site-for-domain'
import * as config from './config'
import * as types from './types'

export async function getSiteMap({
concurrency = 4,
pageConcurrency = 3,
full = false
}: {
concurrency?: number
pageConcurrency?: number
full?: boolean
} = {}): Promise<types.SiteMap> {
const site = await getSiteForDomain(config.domain)

const siteMap = await getAllPages(
site.rootNotionPageId,
site.rootNotionSpaceId,
{
concurrency,
pageConcurrency,
full
}
)

return {
site,
...siteMap
}
}
35 changes: 0 additions & 35 deletions lib/get-site-maps.ts

This file was deleted.

Loading