-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rewrite): allow to rewrite 404 and take
base
into consideration (…
…#11272) * fix(rewrite): allow to rewrite 404 * add changesets * rebase * apply suggestion * Update .changeset/honest-shirts-trade.md Co-authored-by: Florian Lefebvre <[email protected]> --------- Co-authored-by: Florian Lefebvre <[email protected]>
- Loading branch information
1 parent
bc3c329
commit ea987d7
Showing
15 changed files
with
215 additions
and
197 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Fixes a case where rewriting `/` would cause an issue, when `trailingSlash` was set to `"never"`. |
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,5 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Reverts a logic where it wasn't possible to rewrite `/404` in static mode. It's **now possible** again |
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
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
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,55 @@ | ||
import type { AstroConfig, RewritePayload, RouteData } from '../../@types/astro.js'; | ||
import { shouldAppendForwardSlash } from '../build/util.js'; | ||
import { DEFAULT_404_ROUTE } from './astro-designed-error-pages.js'; | ||
import { appendForwardSlash, removeTrailingForwardSlash } from '../path.js'; | ||
|
||
export type FindRouteToRewrite = { | ||
payload: RewritePayload; | ||
routes: RouteData[]; | ||
request: Request; | ||
trailingSlash: AstroConfig['trailingSlash']; | ||
buildFormat: AstroConfig['build']['format']; | ||
base: AstroConfig['base']; | ||
}; | ||
|
||
export function findRouteToRewrite({ | ||
payload, | ||
routes, | ||
request, | ||
trailingSlash, | ||
buildFormat, | ||
base, | ||
}: FindRouteToRewrite): [RouteData, URL] { | ||
let finalUrl: URL | undefined = undefined; | ||
if (payload instanceof URL) { | ||
finalUrl = payload; | ||
} else if (payload instanceof Request) { | ||
finalUrl = new URL(payload.url); | ||
} else { | ||
finalUrl = new URL(payload, new URL(request.url).origin); | ||
} | ||
|
||
let foundRoute; | ||
for (const route of routes) { | ||
const pathname = shouldAppendForwardSlash(trailingSlash, buildFormat) | ||
? appendForwardSlash(finalUrl.pathname) | ||
: base !== '/' | ||
? removeTrailingForwardSlash(finalUrl.pathname) | ||
: finalUrl.pathname; | ||
if (route.pattern.test(decodeURI(pathname))) { | ||
foundRoute = route; | ||
break; | ||
} | ||
} | ||
|
||
if (foundRoute) { | ||
return [foundRoute, finalUrl]; | ||
} else { | ||
const custom404 = routes.find((route) => route.route === '/404'); | ||
if (custom404) { | ||
return [custom404, finalUrl]; | ||
} else { | ||
return [DEFAULT_404_ROUTE, finalUrl]; | ||
} | ||
} | ||
} |
Oops, something went wrong.