Skip to content

Commit

Permalink
Change canonical blog name via vercel middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentaTomas committed Sep 26, 2023
1 parent 01ed1ab commit 5e205ed
Show file tree
Hide file tree
Showing 9 changed files with 331 additions and 46 deletions.
34 changes: 34 additions & 0 deletions apps/reverse-proxy/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,35 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
33 changes: 33 additions & 0 deletions apps/reverse-proxy/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { NextRequest, NextResponse } from 'next/server'

export async function middleware(req: NextRequest): Promise<NextResponse> {
if (req.method !== 'GET') return NextResponse.next()

const url = new URL(req.nextUrl.toString())

url.protocol = 'https'
url.hostname = 'e2b-blog.framer.website'
url.port = ''

if (url.pathname === '/blog') {
url.pathname = '/'
}

console.log(url.toString())

const res = await fetch(url.toString(), { ...req })

const htmlBody = await res.text()

// Replace has intentionally not compelted quotes to catch the rest of the path
const modifiedHtmlBody = htmlBody.replaceAll(
'href="https://e2b-blog.framer.website',
'href="https://e2b.dev/blog',
)

return new NextResponse(modifiedHtmlBody, res)
}

export const config = {
matcher: '/blog/:path*',
}
6 changes: 6 additions & 0 deletions apps/reverse-proxy/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
}

module.exports = nextConfig
22 changes: 22 additions & 0 deletions apps/reverse-proxy/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "reverse-proxy",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"next": "latest",
"react": "latest",
"react-dom": "latest"
},
"devDependencies": {
"@types/node": "latest",
"@types/react": "latest",
"@types/react-dom": "latest",
"typescript": "latest"
}
}
Empty file.
36 changes: 36 additions & 0 deletions apps/reverse-proxy/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"paths": {
"@/*": [
"./*"
]
},
"forceConsistentCasingInFileNames": true
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
"middleware.ts"
],
"exclude": [
"node_modules"
]
}
22 changes: 1 addition & 21 deletions apps/reverse-proxy/vercel.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,6 @@
"value": "e2b.dev"
}
]
},
{
"source": "/blog(\\/)?",
"destination": "https://e2b-blog.framer.website",
"has": [
{
"type": "host",
"value": "e2b.dev"
}
]
},
{
"source": "/blog/:path*",
"destination": "https://e2b-blog.framer.website/blog/:path*",
"has": [
{
"type": "host",
"value": "e2b.dev"
}
]
}
],
"redirects": [
Expand Down Expand Up @@ -190,4 +170,4 @@
"permanent": true
}
]
}
}
Loading

0 comments on commit 5e205ed

Please sign in to comment.