Skip to content

Instantly share code, notes, and snippets.

@troymcginnis
Last active March 15, 2023 22:03
Show Gist options
  • Save troymcginnis/004417eb9b545c1541f16c4ef32f01cc to your computer and use it in GitHub Desktop.
Save troymcginnis/004417eb9b545c1541f16c4ef32f01cc to your computer and use it in GitHub Desktop.
TOO_MANY_FILESYSTEM_CHECKS gist
import { NextResponse } from 'next/server'
import incrementalPaths from '@public/indices/incrementalPaths'
const proxyHost = process.env.LEADPAGES_BLOG_PROXY_HOST
const proxyPath = process.env.LEADPAGES_BLOG_PROXY_PATH
const patterns = incrementalPaths?.map(
(pathname) => new URLPattern({ pathname })
)
export async function middleware(request) {
const response = NextResponse.next()
const url = request.nextUrl.clone()
if (url.pathname.startsWith(proxyPath)) {
return NextResponse.rewrite(new URL(`${url.pathname}/`, proxyHost))
}
if (!incrementalPaths.length) return response
if (!request.cookies.get('__next_preview_data')) {
const pattern = patterns.find((p) => p.test(url))
url.pathname = !pattern ? url.pathname : `/_legacy${url.pathname}`
return NextResponse.rewrite(url)
}
return response
}
export const config = {
matcher: '/((?!api|_next/static|favicon).*)',
}
const path = require('path')
const findUp = require('find-up')
const adminRedirects = require('redirects')
const { filterRoutesFromSanity } = require('directoryToRoutes')
const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: process.env.ANALYZE === 'true',
})
require('dotenv').config({
path: findUp.sync([`.env.${process.env.NODE_ENV}`, '.env.local', '.env']),
})
const { init: buildJSON } = require('indices/buildJSON')
const {
SANITY_STUDIO_API_PROJECT_ID,
SANITY_STUDIO_API_DATASET,
SANITY_STUDIO_API_VERSION,
SANITY_STUDIO_PREVIEW_SECRET,
SANITY_STUDIO_APP_TOKEN,
NEXT_PUBLIC_URL,
VERCEL_ENV,
SENDGRID_API_KEY,
SENDGRID_TEMPLATE_ID,
SENDGRID_URL,
SENDGRID_VERIFIED_SENDER,
RECAPTCHA_SITE_KEY,
MC_API_KEY,
MC_AUDIENCE_ID,
MC_SERVER_PREFIX,
HCAPTCHA_SITEKEY,
STARGATE_API_HOST,
LEADPAGES_API_HOST,
LEADPAGES_TRIAL_HOST,
LEADPAGES_REACTIVATION_HOST,
LEADPAGES_BLOG_PROXY_HOST,
LEADPAGES_BLOG_PROXY_PATH,
GTM_CONTAINER_ID,
GTAG_TRACKING_ID,
} = process.env
module.exports = withBundleAnalyzer({
env: {
SANITY_STUDIO_API_PROJECT_ID,
SANITY_STUDIO_API_DATASET,
SANITY_STUDIO_API_VERSION,
SANITY_STUDIO_PREVIEW_SECRET,
SANITY_STUDIO_APP_TOKEN,
NEXT_PUBLIC_URL,
SENDGRID_API_KEY,
SENDGRID_TEMPLATE_ID,
SENDGRID_URL,
SENDGRID_VERIFIED_SENDER,
MC_API_KEY,
MC_AUDIENCE_ID,
MC_SERVER_PREFIX,
RECAPTCHA_SITE_KEY,
VERCEL_ENV,
HCAPTCHA_SITEKEY,
STARGATE_API_HOST,
LEADPAGES_API_HOST,
LEADPAGES_TRIAL_HOST,
LEADPAGES_REACTIVATION_HOST,
GTM_CONTAINER_ID,
GTAG_TRACKING_ID,
},
reactStrictMode: false,
poweredByHeader: false,
pageExtensions: ['js', 'jsx', 'ts', 'tsx'],
sassOptions: {
includePaths: [path.resolve(__dirname, 'node_modules')],
},
images: {
domains: ['cdn.sanity.io', 'assets.vercel.com'],
deviceSizes: [320, 640, 750, 828, 1080, 1200, 1600, 1920, 2048, 3840],
},
i18n: {
locales: ['en'],
defaultLocale: 'en',
},
redirects: async () => {
const redirects = await adminRedirects({
projectId: SANITY_STUDIO_API_PROJECT_ID,
dataset: SANITY_STUDIO_API_DATASET,
})
return [
{
source: '/story',
destination: '/story/index.html',
permanent: true,
},
{
source: '/podcast',
destination: 'https://lp.leadpages.com/podcast/',
permanent: true,
},
{
source: '/affiliates',
destination: 'https://lp.leadpages.com/affiliates/',
permanent: true,
},
{
source: '/why-leadpages',
destination: 'https://lp.leadpages.com/why-leadpages/',
permanent: true,
},
{
source: '/free-trial',
destination: 'https://lp.leadpages.com/free-trial/',
permanent: true,
},
{
source: '/webinars',
destination: 'https://lp.leadpages.com/webinars/',
permanent: true,
},
{
source: '/demo',
destination: 'https://lp.leadpages.com/demo/',
permanent: true,
},
{
source: '/webinars/:slug',
destination: 'https://lp.leadpages.com/:slug',
permanent: true,
},
...redirects,
]
},
rewrites: async () => {
const fallbackProxy = `${LEADPAGES_BLOG_PROXY_HOST}${LEADPAGES_BLOG_PROXY_PATH}`
const incrementalPaths = await filterRoutesFromSanity({
directory: './src/pages/_legacy',
projectId: SANITY_STUDIO_API_PROJECT_ID,
dataset: SANITY_STUDIO_API_DATASET,
})
if (incrementalPaths.length) {
const builtPaths = await buildJSON({
files: [
{
path: path.join('./public/indices/incrementalPaths.json'),
data: incrementalPaths,
},
],
})
if (builtPaths.every(({ status }) => status === 'fulfilled')) {
console.log(
`Built index of ${incrementalPaths.length} incremental paths.`
)
}
}
return {
beforeFiles: [
{
source: '/blog',
destination: `${fallbackProxy}/`,
},
{
source: '/blog/',
destination: `${fallbackProxy}/`,
},
{
source: '/blog/:path*',
destination: `${fallbackProxy}/:path*/`,
},
],
afterFiles: [
{
source: '/studio/:path*',
destination:
process.env.NODE_ENV === 'development'
? 'http://localhost:3333/studio/:path*'
: '/studio/index.html',
},
{
source: '/home',
destination: '/',
},
],
fallback: [
{
source: '/:path*',
destination: `/redirectHandler`,
},
],
}
},
eslint: {
ignoreDuringBuilds: true,
},
experimental: {
scrollRestoration: true,
},
})
import React from 'react'
import legacyRedirects from '@public/indices/legacySiteRedirects.json'
import blogRedirects from '@public/indices/legacyBlogRedirects.json'
const redirects = { ...blogRedirects, ...legacyRedirects }
const Page = ({ errorCode }) => <div>`${errorCode}`</div>
export const getServerSideProps = async ({ req, res, ...context }) => {
const { query } = context
const path = query?.path?.join('/')
if (Object.keys(redirects).includes(path)) {
const rule = redirects[path]
return {
redirect: {
destination: rule?.destination || rule,
statusCode: rule?.statusCode || 308,
},
}
}
return {
notFound: true,
}
}
export default Page
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment