-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(preview): use absolute redirect url for draft api routes (#504)
- Loading branch information
Showing
4 changed files
with
26 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import { draftMode } from "next/headers"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import { absoluteUrlFromNextRequest } from "src/utils/url"; | ||
|
||
export function GET(request: NextRequest) { | ||
draftMode().disable(); | ||
const url = new URL(request.nextUrl); | ||
return NextResponse.redirect(new URL("/", url.origin)); | ||
return NextResponse.redirect(absoluteUrlFromNextRequest(request, "/")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { NextRequest } from "next/server"; | ||
|
||
export function absoluteUrlFromNextRequest( | ||
request: NextRequest, | ||
pathname?: string, | ||
) { | ||
/** | ||
* useful for middleware redirects where absolute url is required | ||
* https://nextjs.org/docs/messages/middleware-relative-urls#possible-ways-to-fix-it | ||
*/ | ||
const absoluteUrl = request.nextUrl.clone(); | ||
if (pathname !== undefined) { | ||
absoluteUrl.pathname = pathname; | ||
} | ||
return absoluteUrl; | ||
} |