-
Notifications
You must be signed in to change notification settings - Fork 86
fix: handle absolute rewrite URLs #1325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { NextResponse } from 'next/server' | ||
import { NextFetchEvent, NextRequest } from 'next/server' | ||
|
||
export function middleware(req: NextRequest, ev: NextFetchEvent) { | ||
const res = NextResponse.rewrite(new URL('/shows/100', req.url)) | ||
res.headers.set('x-modified-in-rewrite', 'true') | ||
return res | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const Show = () => { | ||
return ( | ||
<div> | ||
<p>This should have been rewritten</p> | ||
</div> | ||
) | ||
} | ||
|
||
export default Show |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { NextResponse } from 'next/server' | ||
import { NextFetchEvent, NextRequest } from 'next/server' | ||
|
||
export function middleware(req: NextRequest, ev: NextFetchEvent) { | ||
const res = NextResponse.rewrite('http://example.com/') | ||
res.headers.set('x-modified-in-rewrite', 'true') | ||
return res | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const Show = () => { | ||
return ( | ||
<div> | ||
<p>This should have been rewritten</p> | ||
</div> | ||
) | ||
} | ||
|
||
export default Show |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -5,6 +5,19 @@ export interface FetchEventResult { | |||||
waitUntil: Promise<any> | ||||||
} | ||||||
|
||||||
/** | ||||||
* This is how Next handles rewritten URLs. | ||||||
*/ | ||||||
export function relativizeURL(url: string | string, base: string | URL) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [boulder] Is it a mistake that the
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, missed this before adding automerge! That's copied directly from the Next.js source and certainly looks like a mistake! |
||||||
const baseURL = typeof base === 'string' ? new URL(base) : base | ||||||
const relative = new URL(url, base) | ||||||
const origin = `${baseURL.protocol}//${baseURL.host}` | ||||||
return `${relative.protocol}//${relative.host}` === origin | ||||||
? relative.toString().replace(origin, '') | ||||||
: relative.toString() | ||||||
} | ||||||
|
||||||
|
||||||
export const addMiddlewareHeaders = async ( | ||||||
originResponse: Promise<Response> | Response, | ||||||
middlewareResponse: Response, | ||||||
|
@@ -14,7 +27,8 @@ export const addMiddlewareHeaders = async ( | |||||
return originResponse | ||||||
} | ||||||
// We need to await the response to get the origin headers, then we can add the ones from middleware. | ||||||
const response = await originResponse | ||||||
const res = await originResponse | ||||||
const response = new Response(res.body, res) | ||||||
middlewareResponse.headers.forEach((value, key) => { | ||||||
response.headers.set(key, value) | ||||||
}) | ||||||
|
@@ -33,6 +47,14 @@ export const buildResponse = async ({ | |||||
request.headers.set('x-nf-next-middleware', 'skip') | ||||||
const rewrite = res.headers.get('x-middleware-rewrite') | ||||||
if (rewrite) { | ||||||
const rewriteUrl = new URL(rewrite, request.url) | ||||||
const baseUrl = new URL(request.url) | ||||||
if(rewriteUrl.hostname !== baseUrl.hostname) { | ||||||
// Netlify Edge Functions don't support proxying to external domains, but Next middleware does | ||||||
const proxied = fetch(new Request(rewriteUrl.toString(), request)) | ||||||
return addMiddlewareHeaders(proxied, res) | ||||||
} | ||||||
res.headers.set('x-middleware-rewrite', relativizeURL(rewrite, request.url)) | ||||||
return addMiddlewareHeaders(context.rewrite(rewrite), res) | ||||||
} | ||||||
if (res.headers.get('x-middleware-next') === '1') { | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[dust] Might be handy to include the same link that was provided in the pull request summary here so it makes it easier to folks to find the relevant code that's being referred to here