Skip to content

Commit

Permalink
Add null redirect edge functions (#73)
Browse files Browse the repository at this point in the history
* Add null redirect edge functions

* Apply suggestions from code review

Co-authored-by: Paulo Borges <[email protected]>

---------

Co-authored-by: Paulo Borges <[email protected]>
  • Loading branch information
JakeSCahill and Deflaimun authored Aug 27, 2024
1 parent c3d3707 commit 3d2348c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ node_modules
.idea
.vscode
/docs/
.bundle
.bundle
# Local Netlify folder
.netlify
4 changes: 4 additions & 0 deletions netlify.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
[build.environment]
NODE_VERSION = "18"

[dev]
publish = "docs/"
framework = "#static"

[[plugins]]
package = "@netlify/plugin-lighthouse"

Expand Down
24 changes: 24 additions & 0 deletions netlify/edge-functions/redirect-null.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export default async (request, context) => {
const url = new URL(request.url);
const pathname = url.pathname;

if (pathname.endsWith('/null') || pathname.endsWith('/null/')) {
// Remove the "/null" part from the URL
let newPathname = pathname.replace(/\/null\/?$/, '/');

// Ensure the URL ends with a "/" for local tests
if (!newPathname.endsWith('/')) {
newPathname += '/';
}

// Construct the new URL without "/null"
const newUrl = `${url.origin}${newPathname}${url.search}`;

return Response.redirect(newUrl, 301);
}

return context.next();
};

// This configures the Edge Function to trigger on paths ending with "/null"
export const config = { path: "*/null" };

0 comments on commit 3d2348c

Please sign in to comment.