Skip to content

Commit

Permalink
Merge pull request #1005 from MuckRock/997-redirect-html
Browse files Browse the repository at this point in the history
Handle URL extension routes with redirects
  • Loading branch information
eyeseast authored Jan 14, 2025
2 parents 83473f6 + 77aaeac commit b939621
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 5 deletions.
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
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.

0 comments on commit b939621

Please sign in to comment.