findRouteToRewrite() — astro Function Reference
Architecture documentation for the findRouteToRewrite() function in rewrite.ts from the astro codebase.
Entity Profile
Dependency Diagram
graph TD c083efaa_e621_06cc_eb87_13e498644392["findRouteToRewrite()"] 67024343_d7f2_f928_d5b2_367cfa846343["rewrite.ts"] c083efaa_e621_06cc_eb87_13e498644392 -->|defined in| 67024343_d7f2_f928_d5b2_367cfa846343 style c083efaa_e621_06cc_eb87_13e498644392 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
packages/astro/src/core/routing/rewrite.ts lines 39–145
export function findRouteToRewrite({
payload,
routes,
request,
trailingSlash,
buildFormat,
base,
outDir,
}: FindRouteToRewrite): FindRouteToRewriteResult {
let newUrl: URL | undefined = undefined;
if (payload instanceof URL) {
newUrl = payload;
} else if (payload instanceof Request) {
newUrl = new URL(payload.url);
} else {
newUrl = new URL(payload, new URL(request.url).origin);
}
let pathname = newUrl.pathname;
const shouldAppendSlash = shouldAppendForwardSlash(trailingSlash, buildFormat);
// Special handling for base path
if (base !== '/') {
// Check if this is a request to the base path
const isBasePathRequest =
newUrl.pathname === base || newUrl.pathname === removeTrailingForwardSlash(base);
if (isBasePathRequest) {
// For root path requests at the base URL
// When trailingSlash is 'never', we should match '' (empty string pathname)
// When trailingSlash is 'always', we should match '/' pathname
pathname = shouldAppendSlash ? '/' : '';
} else if (newUrl.pathname.startsWith(base)) {
// For non-root paths under the base
pathname = shouldAppendSlash
? appendForwardSlash(newUrl.pathname)
: removeTrailingForwardSlash(newUrl.pathname);
pathname = pathname.slice(base.length);
}
}
// Ensure pathname starts with '/' when needed
if (!pathname.startsWith('/') && shouldAppendSlash && newUrl.pathname.endsWith('/')) {
pathname = prependForwardSlash(pathname);
}
// Convert '/' to '' for trailingSlash: 'never'
if (pathname === '/' && base !== '/' && !shouldAppendSlash) {
pathname = '';
}
// If config.build.format = 'file' then pathname includes .html, so we need to remove it
if (buildFormat === 'file') {
pathname = pathname.replace(/\.html$/, '');
}
// Set the final URL pathname
if (base !== '/' && (pathname === '' || pathname === '/') && !shouldAppendSlash) {
// Special case for root path at base URL with trailingSlash: 'never'
newUrl.pathname = removeTrailingForwardSlash(base);
} else {
newUrl.pathname = joinPaths(...[base, pathname].filter(Boolean));
}
const decodedPathname = decodeURI(pathname);
let foundRoute;
for (const route of routes) {
if (route.pattern.test(decodedPathname)) {
// If it's a dynamic route, make sure it actually generates the pathname
// Checking for params to make sure it's a dynamic route
if (
route.params &&
route.params.length !== 0 &&
route.distURL &&
route.distURL.length !== 0
) {
// Remove outDir from beginning of distURL
// Remove /index.html or .html from end of distURL and compare with pathname
// Use pathname (encoded) instead of decodedPathname because url.href is encoded
if (
!route.distURL.find(
(url) =>
url.href.replace(outDir.toString(), '').replace(/(?:\/index\.html|\.html)$/, '') ==
Domain
Subdomains
Defined In
Source
Frequently Asked Questions
What does findRouteToRewrite() do?
findRouteToRewrite() is a function in the astro codebase, defined in packages/astro/src/core/routing/rewrite.ts.
Where is findRouteToRewrite() defined?
findRouteToRewrite() is defined in packages/astro/src/core/routing/rewrite.ts at line 39.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free