Skip to content

Commit

Permalink
fix: allow deploy preview urls in middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinwolfcr committed Sep 6, 2023
1 parent 5c793a1 commit 7c5da54
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ import type { NextRequest } from "next/server"

import { NextResponse } from "next/server"

import { siteUrl } from "./utils/seo"

export const config = {
matcher: ["/((?!api/|_next/|_static/|_vercel|[\\w-]+\\.\\w+).*)"],
}

export function middleware(req: NextRequest) {
const currentUrl = siteUrl().replace(/^https:\/\//, "")
console.log("currentUrl", currentUrl)
const baseUrl = "kevinwolf.cr"

const docsProject =
req.headers
.get("host")!
.replace(`localhost:${process.env.PORT}`, baseUrl)
.replace(new RegExp(`^(?:([^.]+)\\.)?${baseUrl.replace(/\./g, "\\.")}$`), "$1") || ""
const url = req.headers.get("host")!.replace(currentUrl, baseUrl)
console.log("url", url)
const docsProject = url.replace(new RegExp(`^(?:([^.]+)\\.)?${baseUrl.replace(/\./g, "\\.")}$`), "$1") || ""
console.log("docsProject", docsProject)

if (docsProject) return NextResponse.rewrite(new URL(`/docs/${docsProject}${req.nextUrl.pathname}`, req.url))
}

1 comment on commit 7c5da54

@vercel
Copy link

@vercel vercel bot commented on 7c5da54 Sep 6, 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.