Skip to content
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

Redirects for dev mode short links #465

Merged
merged 4 commits into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Elegant!

Especially like how this check handles the three different 301 cases.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, but... can we add a comment? :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment about how it's to flatten redirect chains so we don't actually serve a chain of them.

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,
]));