koa-path-canonicalizer is a Koa middleware designed to automatically redirect incoming requests to a canonicalized path. This typically involves normalizing URL structures, such as ensuring consistent trailing slashes or removing duplicate slashes, which helps improve SEO by preventing duplicate content issues and maintaining clean URLs. The package is currently at version 0.2.0, indicating it's still in early development stages, with a relatively stable and slow release cadence, having had only two minor releases since its initial launch. Its key differentiator is its focused and straightforward integration into Koa applications for basic path normalization, offering a minimalist solution without extensive configuration overhead compared to broader routing libraries.
npm install koa-path-canonicalizerVerified import paths — ran on the pinned version, not inferred.
Demonstrates setting up a basic Koa application with `koa-path-canonicalizer` to automatically redirect requests to their canonicalized paths, showing how to remove trailing slashes and normalize multiple slashes.
Initialize the middleware with `pathCanonicalizer({ trailingSlash: true })` if you want to enforce trailing slashes, or `false` to enforce no trailing slashes.Ensure your Koa application is configured to trust proxy headers using `app.proxy = true;` and that your proxy correctly sets `X-Forwarded-For`, `X-Forwarded-Host`, and `X-Forwarded-Proto`.
Place `koa-path-canonicalizer` early in your middleware chain and thoroughly test interactions with any other middleware that performs redirects or path rewrites. Configure options like `trailingSlash` to prevent conflicts with other components.
Ensure `koa-path-canonicalizer` is mounted early in your Koa middleware chain, typically before any middleware that might send a response or perform its own redirects based on the original path.
Change your import statement from `import pathCanonicalizer from 'koa-path-canonicalizer';` to `import { pathCanonicalizer } from 'koa-path-canonicalizer';`.Review all components in your request path (nginx, Apache, other Koa middleware) that might modify the URL. Configure `koa-path-canonicalizer`'s `trailingSlash` option explicitly (`true` or `false`) to match the desired canonical form and avoid conflicts.