-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add astro middleware route blocker (#45)
- Loading branch information
1 parent
9a43b56
commit 650bb66
Showing
3 changed files
with
23 additions
and
9 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
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,19 @@ | ||
const host_url = import.meta.env.HOST_URL; | ||
const blocked_urls = import.meta.env.BLOCKED_URLS.split(","); | ||
|
||
export function onRequest({ locals, request }, next) { | ||
for (const blockedPath of blocked_urls) { | ||
let path = host_url + blockedPath; | ||
if (normalizePath(request.url) === path) { | ||
return Response.redirect(`${host_url}/404`, 302); | ||
} | ||
} | ||
|
||
return next(); | ||
} | ||
|
||
// Utility function to remove trailing slash | ||
function normalizePath(path) { | ||
// This removes the trailing slash if it exists | ||
return path.endsWith("/") ? path.slice(0, -1) : path; | ||
} |