-
Notifications
You must be signed in to change notification settings - Fork 220
Factor out redirect middleware and run in dev server #513
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f88cf3a
dd67c1c
63e3c2c
fcad085
d390a8c
3909e4f
5f3a03a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* @license | ||
* Copyright 2021 Google LLC | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
import {pageRedirects} from '../redirects.js'; | ||
import type Koa from 'koa'; | ||
|
||
/** | ||
* Creates a Koa middleware that performs lit.dev redirection logic. | ||
*/ | ||
export const redirectMiddleware = (): Koa.Middleware => async (ctx, next) => { | ||
// If there would be multiple redirects, resolve them all here so that we | ||
// serve just one HTTP redirect instead of a chain. | ||
let path = ctx.path; | ||
if (path.match(/\/[^\/\.]+$/)) { | ||
// Canonicalize paths to have a trailing slash, except for files with | ||
// extensions. | ||
path += '/'; | ||
} | ||
if (path.endsWith('//')) { | ||
// Koa static doesn't care if there are any number of trailing slashes. | ||
// Normalize this too. | ||
path = path.replace(/\/+$/, '/'); | ||
} | ||
path = pageRedirects.get(path) ?? path; | ||
if (path !== ctx.path) { | ||
ctx.status = 301; | ||
ctx.redirect(path + (ctx.querystring ? '?' + ctx.querystring : '')); | ||
} else { | ||
await next(); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
{ | ||
"compilerOptions": { | ||
"incremental": true, | ||
"tsBuildInfoFile": "./tsconfig.tsbuildinfo", | ||
"tsBuildInfoFile": "./lib/.tsbuildinfo", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tiny nit: Other packages kept this as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm interesting. The default is actually There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I might go through and standardize these in a followup. |
||
"target": "es2020", | ||
"module": "esnext", | ||
"declaration": true, | ||
"declarationMap": true, | ||
"sourceMap": true, | ||
"outDir": "./", | ||
"outDir": "./lib", | ||
"rootDir": "./src", | ||
"composite": true, | ||
"strict": true, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice refactor