-
Notifications
You must be signed in to change notification settings - Fork 165
Fix: react resolution for interception, parallel and group routes #155
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
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b4631be
fix: fix react resolution for interception, parallel and group routes
conico974 2a4c32f
fix: group & parallel routes should be removed first
conico974 2b6908f
fix: interception use rewrites
conico974 bfb42bc
fix: rewrites could be empty
conico974 dd5d60f
fix: handle rewrites internally
conico974 aa44214
fix: handle redirects
conico974 cf74c1f
fix: rewrite route for dynamic params
conico974 57284a4
fix: escape interception regex
conico974 023f707
feat: add external url redirect & rewrite
conico974 1fc4ec5
fix: add afterFiles and fallback
conico974 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,177 @@ | ||
import { compile, match } from "path-to-regexp"; | ||
|
||
import { InternalEvent } from "./event-mapper.js"; | ||
import { debug } from "./logger.js"; | ||
import { | ||
RedirectDefinition, | ||
RewriteDefinition, | ||
RewriteMatcher, | ||
} from "./next-types.js"; | ||
import { IncomingMessage } from "./request.js"; | ||
import { ServerResponse } from "./response.js"; | ||
import { escapeRegex, unescapeRegex } from "./util.js"; | ||
|
||
const redirectMatcher = | ||
( | ||
headers: Record<string, string>, | ||
cookies: Record<string, string>, | ||
query: Record<string, string | string[]>, | ||
) => | ||
(redirect: RewriteMatcher) => { | ||
switch (redirect.type) { | ||
case "header": | ||
return ( | ||
headers?.[redirect.key.toLowerCase()] && | ||
new RegExp(redirect.value ?? "").test( | ||
headers[redirect.key.toLowerCase()] ?? "", | ||
) | ||
); | ||
case "cookie": | ||
return ( | ||
cookies?.[redirect.key] && | ||
new RegExp(redirect.value ?? "").test(cookies[redirect.key] ?? "") | ||
); | ||
case "query": | ||
return query[redirect.key] && Array.isArray(redirect.value) | ||
? redirect.value.reduce( | ||
(prev, current) => | ||
prev || new RegExp(current).test(query[redirect.key] as string), | ||
false, | ||
) | ||
: new RegExp(redirect.value ?? "").test( | ||
(query[redirect.key] as string | undefined) ?? "", | ||
); | ||
case "host": | ||
return ( | ||
headers?.host && new RegExp(redirect.value ?? "").test(headers.host) | ||
); | ||
default: | ||
return false; | ||
} | ||
}; | ||
|
||
function isExternal(url?: string) { | ||
if (!url) return false; | ||
const pattern = /^https?:\/\//; | ||
return pattern.test(url); | ||
} | ||
|
||
function getUrlParts(url: string, isExternal: boolean) { | ||
if (!isExternal) { | ||
return { | ||
hostname: "", | ||
pathname: url, | ||
protocol: "", | ||
}; | ||
} | ||
const { hostname, pathname, protocol } = new URL(url); | ||
return { | ||
hostname, | ||
pathname, | ||
protocol, | ||
}; | ||
} | ||
|
||
export function handleRewrites<T extends RewriteDefinition>( | ||
internalEvent: InternalEvent, | ||
rewrites: T[], | ||
) { | ||
const { rawPath, headers, query, cookies } = internalEvent; | ||
const matcher = redirectMatcher(headers, cookies, query); | ||
const rewrite = rewrites.find( | ||
(route) => | ||
new RegExp(route.regex).test(rawPath) && | ||
(route.has | ||
? route.has.reduce((acc, cur) => { | ||
if (acc === false) return false; | ||
return matcher(cur); | ||
}, true) | ||
: true) && | ||
(route.missing | ||
? route.missing.reduce((acc, cur) => { | ||
if (acc === false) return false; | ||
return !matcher(cur); | ||
}, true) | ||
: true), | ||
); | ||
|
||
const urlQueryString = new URLSearchParams(query).toString(); | ||
let rewrittenUrl = rawPath; | ||
const isExternalRewrite = isExternal(rewrite?.destination); | ||
debug("isExternalRewrite", isExternalRewrite); | ||
if (rewrite) { | ||
const { pathname, protocol, hostname } = getUrlParts( | ||
rewrite.destination, | ||
isExternalRewrite, | ||
); | ||
const toDestination = compile(escapeRegex(pathname ?? "") ?? ""); | ||
const fromSource = match(escapeRegex(rewrite?.source) ?? ""); | ||
const _match = fromSource(rawPath); | ||
if (_match) { | ||
const { params } = _match; | ||
const isUsingParams = Object.keys(params).length > 0; | ||
if (isUsingParams) { | ||
const rewrittenPath = unescapeRegex(toDestination(params)); | ||
rewrittenUrl = isExternalRewrite | ||
? `${protocol}//${hostname}${rewrittenPath}` | ||
: `/${rewrittenPath}`; | ||
} else { | ||
rewrittenUrl = rewrite.destination; | ||
} | ||
debug("rewrittenUrl", rewrittenUrl); | ||
} | ||
} | ||
|
||
return { | ||
internalEvent: { | ||
...internalEvent, | ||
rawPath: rewrittenUrl, | ||
url: `${rewrittenUrl}${urlQueryString ? `?${urlQueryString}` : ""}`, | ||
}, | ||
__rewrite: rewrite, | ||
isExternalRewrite, | ||
}; | ||
} | ||
|
||
export function handleRedirects( | ||
internalEvent: InternalEvent, | ||
redirects: RedirectDefinition[], | ||
) { | ||
const { internalEvent: _internalEvent, __rewrite } = handleRewrites( | ||
internalEvent, | ||
redirects, | ||
); | ||
if (__rewrite && !__rewrite.internal) { | ||
return { | ||
statusCode: __rewrite.statusCode ?? 308, | ||
headers: { | ||
Location: _internalEvent.url, | ||
}, | ||
}; | ||
} | ||
} | ||
|
||
export async function proxyRequest(req: IncomingMessage, res: ServerResponse) { | ||
const HttpProxy = require("next/dist/compiled/http-proxy") as any; | ||
|
||
const proxy = new HttpProxy({ | ||
changeOrigin: true, | ||
ignorePath: true, | ||
xfwd: true, | ||
}); | ||
|
||
await new Promise<void>((resolve, reject) => { | ||
proxy.on("proxyRes", () => { | ||
resolve(); | ||
}); | ||
|
||
proxy.on("error", (err: any) => { | ||
reject(err); | ||
}); | ||
|
||
proxy.web(req, res, { | ||
target: req.url, | ||
headers: req.headers, | ||
}); | ||
}); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.