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

Handle URL extension routes with redirects #1005

Merged
merged 5 commits into from
Jan 14, 2025
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
3 changes: 3 additions & 0 deletions netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
command = "npm run build"
publish = "build"

[[context.production.plugins]]
package = "/plugins/cache-bust"

[[plugins]]
package = "@netlify/plugin-lighthouse"
# https://github.com/netlify/netlify-plugin-lighthouse
Expand Down
43 changes: 43 additions & 0 deletions src/routes/(app)/documents/[id]-[slug].[format]/+page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// handle .html and other file extension routes from old versions of DocumentCloud

import { error, redirect } from "@sveltejs/kit";
import * as documents from "$lib/api/documents";

export const trailingSlash = "never";

export async function load({ params, fetch }) {
const { id, format } = params;

const { data: document, error: err } = await documents.get(id, fetch);

if (err) {
return error(err.status, err.message);
}

if (!document) {
return error(404, "Not found");
}

let url: URL;

switch (format) {
// for .html, redirect to embed
case "html":
url = documents.embedUrl(document);
return redirect(302, url);

case "txt":
url = documents.canonicalUrl(document);
url.searchParams.set("mode", "text");
return redirect(302, url);

// redirect to the PDF file, no-op for errors
case "pdf":
url = documents.pdfUrl(document);
return redirect(302, url);

// fallback: redirect to the canonical path, on the same host
Copy link
Member

Choose a reason for hiding this comment

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

This is a great approach! My only design question is whether .txt should redirect to the text mode in an embed viewer instead of the full-site viewer.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Historically, there wasn't a difference between embed and normal versions of the viewer (except in the very beginning), but the .html extension is baked into the WordPress plugin (by me, a long time ago, sorry) so it's still used in embeds.

This is the old documentation: https://web.archive.org/web/20180301171441/https://www.documentcloud.org/help/publishing#linking

All that said, I don't have much evidence of anyone using the .txt. or .pdf links, and it hasn't been supported in a few years, so I think we're ok like this, and we can adjust if needed.

default:
return redirect(302, documents.canonicalUrl(document));
}
}
2 changes: 1 addition & 1 deletion src/routes/(app)/documents/[id]-[slug]/+page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function load({

depends(`document:${document.id}`);

const canonical = new URL(document.canonical_url);
const canonical = documents.canonicalUrl(document);
if (document.slug !== params.slug) {
redirect(302, canonical.pathname);
}
Expand Down
8 changes: 4 additions & 4 deletions src/routes/(app)/projects/[id]/+page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import * as projects from "$lib/api/projects";
export async function load({ params, fetch }) {
const { data: project, error: err } = await projects.get(+params.id, fetch);

if (!project) {
return error(404, "Project not found");
}

if (err) {
return error(err.status, err.message);
}

if (!project) {
return error(404, "Project not found");
}

const url = projects.canonicalUrl(project);

return redirect(302, url);
Expand Down
Binary file added static/apple-touch-icon-120x120-precomposed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/apple-touch-icon-precomposed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/apple-touch-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading