deno.land / x / lume@v2.1.4 / middlewares / www.ts
12345678910111213141516171819202122232425262728293031323334353637383940414243import type { Middleware } from "../core/server.ts";
export interface Options { add: boolean; code: 301 | 302 | 307 | 308;}
export const defaults: Options = { add: false, code: 301,};
/** Middleware to add/remove the www. domain */export default function www(userOptions?: Partial<Options>): Middleware { const options = { ...defaults, ...userOptions };
return async (request, next) => { const url = new URL(request.url);
if (url.hostname.startsWith("www.")) { if (!options.add) { url.hostname = url.hostname.replace("www.", ""); return new Response(null, { status: options.code, headers: { location: url.toString(), }, }); } } else if (options.add) { url.hostname = `www.${url.hostname}`; return new Response(null, { status: options.code, headers: { location: url.toString(), }, }); }
return await next(request); };}
Version Info