Skip to content

Commit

Permalink
Redirects for dev mode short links (#465)
Browse files Browse the repository at this point in the history
Adds a https://lit.dev/msg/<code> redirect for all warnings logged by Lit's dev mode.

Half of lit/lit#2078. Other half is at lit/lit#2119.
  • Loading branch information
aomarks committed Sep 1, 2021
1 parent 8f249b9 commit c90e4e3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
29 changes: 12 additions & 17 deletions packages/lit-dev-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,23 @@ if (mode === 'playground') {
}

app.use(async (ctx, next) => {
if (ctx.path.match(/\/[^\/\.]+$/)) {
// 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.
ctx.status = 301;
ctx.redirect(
ctx.path + '/' + (ctx.querystring ? '?' + ctx.querystring : '')
);
} else if (ctx.path.endsWith('//')) {
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(
ctx.path.replace(/\/+$/, '/') +
(ctx.querystring ? '?' + ctx.querystring : '')
);
} else if (pageRedirects.has(ctx.path)) {
// Handle any 1:1 page redirects
ctx.status = 301;
ctx.redirect(
pageRedirects.get(ctx.path) +
(ctx.querystring ? '?' + ctx.querystring : '')
);
ctx.redirect(path + (ctx.querystring ? '?' + ctx.querystring : ''));
} else {
await next();
}
Expand Down
28 changes: 23 additions & 5 deletions packages/lit-dev-server/src/redirects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,26 @@
* SPDX-License-Identifier: BSD-3-Clause
*/

export const pageRedirects = new Map(
[
['/slack-invite/', 'https://join.slack.com/t/lit-and-friends/shared_invite/zt-llwznvsy-LZwT13R66gOgnrg12PUGqw'],
]
)
// prettier-ignore
export const pageRedirects = new Map([
['/slack-invite', 'https://join.slack.com/t/lit-and-friends/shared_invite/zt-llwznvsy-LZwT13R66gOgnrg12PUGqw'],

['/msg/dev-mode', '/docs/tools/development/#development-and-production-builds'],
// TODO(sorvell) https://github.com/lit/lit.dev/issues/455
['/msg/multiple-versions', '/docs/tools/requirements/'],
['/msg/polyfill-support-missing', '/docs/tools/requirements/#polyfills'],
// TODO(sorvell) https://github.com/lit/lit.dev/issues/462
['/msg/class-field-shadowing', '/docs/components/properties/#declare'],
// TODO(aomarks) Should we add something specifically about this issue?
['/msg/change-in-update', '/docs/components/properties/#when-properties-change'],
['/msg/deprecated-import-path', '/docs/releases/upgrade/#update-packages-and-import-paths'],
['/msg/removed-api', '/docs/releases/upgrade/#litelement'],
['/msg/renamed-api', '/docs/releases/upgrade/#update-to-renamed-apis'],
['/msg/undefined-attribute-value', '/docs/releases/upgrade/#litelement'],
['/msg/request-update-promise', '/docs/releases/upgrade/#litelement'],
].map(([path, redir]) => [
// Trailing slashes are required because this redirect map is consulted after
// standard lit.dev path canonicalization.
path.match(/\/[^\/\.]+$/) ? path + '/' : path,
redir,
]));

0 comments on commit c90e4e3

Please sign in to comment.