-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1005 from MuckRock/997-redirect-html
Handle URL extension routes with redirects
- Loading branch information
Showing
7 changed files
with
51 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.