Skip to content
This repository has been archived by the owner on Jul 26, 2023. It is now read-only.

Support Edge API Route and Edge Config as kv Store #881

Open
wants to merge 8 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
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,25 @@
"@fortawesome/react-fontawesome": "^0.2.0",
"@headlessui/react": "^1.7.8",
"@tailwindcss/line-clamp": "^0.4.2",
"@upstash/redis": "^1.20.0",
"@vercel/edge-config": "^0.1.1",
"awesome-debounce-promise": "^2.1.0",
"axios": "^1.2.6",
"cors": "^2.8.5",
"crypto-js": "^4.1.1",
"csstype": "^3.1.1",
"dayjs": "^1.11.7",
"emoji-regex": "^10.2.1",
"encoding": "^0.1.13",
"i18next": "^22.4.9",
"ioredis": "^5.3.0",
"jszip": "^3.10.1",
"mpegts.js": "^1.7.2",
"next": "^13.1.6",
"next-i18next": "^13.0.3",
"nextjs-progressbar": "^0.0.16",
"node-fetch": "^3.3.0",
"path-browserify": "^1.0.1",
"plyr-react": "^5.1.2",
"preview-office-docs": "^1.0.2",
"react": "^18.2.0",
Expand Down
124 changes: 123 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export async function generateStaticParams() {
return [{ lang: 'en-US' }, { lang: 'de' }]
}

export default function RootLayout({ children, params }: { children: React.ReactNode; params: { lang: string } }) {
return (
<html lang={params.lang}>
<body>{children}</body>
</html>
)
}
2 changes: 1 addition & 1 deletion src/components/Footer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import config from '../../config/site.config'
import config from '@cfg/site.config'

const createFooterMarkup = () => {
return {
Expand Down
2 changes: 1 addition & 1 deletion src/components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useRouter } from 'next/router'
import { Fragment, useEffect, useState } from 'react'
import { useTranslation } from 'next-i18next'

import siteConfig from '../../config/site.config'
import siteConfig from '@cfg/site.config'
import SearchModal from './SearchModal'
import SwitchLang from './SwitchLang'
import useDeviceOS from '../utils/useDeviceOS'
Expand Down
2 changes: 1 addition & 1 deletion src/components/SearchModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { LoadingIcon } from './Loading'

import { getFileIcon } from '../utils/getFileIcon'
import { fetcher } from '../utils/fetchWithSWR'
import siteConfig from '../../config/site.config'
import siteConfig from '@cfg/site.config'

/**
* Extract the searched item's path in field 'parentReference' and convert it to the
Expand Down
31 changes: 31 additions & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import { get } from '@vercel/edge-config'

const pattern = /^\/api\/\w+(?=\/|$)/

export async function middleware(request: NextRequest) {
const original = request.nextUrl

let { pathname } = original
if (pathname === '/api/') {
pathname = '/api/index/'
}

const edgeRuntimeEnabled = process.env.EDGE_CONFIG && (await get<boolean>('edge_runtime'))
if (!edgeRuntimeEnabled) {
pathname = pathname.replace(pattern, m => `${m}-v1`)
} else {
pathname = pathname.replace(pattern, m => `${m}-v2`)
}
const rewritten = new URL(pathname, original)
original.searchParams.forEach((value, key) => {
rewritten.searchParams.append(key, value)
})
return NextResponse.rewrite(rewritten)
}

// See "Matching Paths" below to learn more
export const config = {
matcher: '/api/:path*',
}
2 changes: 1 addition & 1 deletion src/pages/[...path].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Head from 'next/head'
import { useRouter } from 'next/router'
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'

import siteConfig from '../../config/site.config'
import siteConfig from '@cfg/site.config'
import Navbar from '../components/Navbar'
import FileListing from '../components/FileListing'
import Footer from '../components/Footer'
Expand Down
2 changes: 1 addition & 1 deletion src/pages/_document.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Document, { Head, Html, Main, NextScript } from 'next/document'
import siteConfig from '../../config/site.config'
import siteConfig from '@cfg/site.config'

class MyDocument extends Document {
render() {
Expand Down
5 changes: 5 additions & 0 deletions src/pages/api/index-v1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { getHandler } from '@/utils/api/common/v1'

import handle from '@/utils/api/index'

export default getHandler(handle)
Loading