From 3d2348cedf7cccb5449b56aa3bedcda916ec66bc Mon Sep 17 00:00:00 2001 From: Jake Cahill <45230295+JakeSCahill@users.noreply.github.com> Date: Tue, 27 Aug 2024 17:42:12 +0100 Subject: [PATCH] Add null redirect edge functions (#73) * Add null redirect edge functions * Apply suggestions from code review Co-authored-by: Paulo Borges --------- Co-authored-by: Paulo Borges --- .gitignore | 4 +++- netlify.toml | 4 ++++ netlify/edge-functions/redirect-null.js | 24 ++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 netlify/edge-functions/redirect-null.js diff --git a/.gitignore b/.gitignore index d2a26af..db84c76 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,6 @@ node_modules .idea .vscode /docs/ -.bundle \ No newline at end of file +.bundle +# Local Netlify folder +.netlify diff --git a/netlify.toml b/netlify.toml index 0bd04f0..18fdce7 100644 --- a/netlify.toml +++ b/netlify.toml @@ -1,6 +1,10 @@ [build.environment] NODE_VERSION = "18" +[dev] + publish = "docs/" + framework = "#static" + [[plugins]] package = "@netlify/plugin-lighthouse" diff --git a/netlify/edge-functions/redirect-null.js b/netlify/edge-functions/redirect-null.js new file mode 100644 index 0000000..12c49eb --- /dev/null +++ b/netlify/edge-functions/redirect-null.js @@ -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" };